generated from NOW-SOPT-ANDROID/now-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
67 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
app/src/main/java/com/sopt/now/compose/ui/myPage/MyPageViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.sopt.now.compose.ui.myPage | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.ViewModel | ||
import com.sopt.now.compose.data.model.ResponseInfoDto | ||
import com.sopt.now.compose.data.model.UserInfo | ||
import com.sopt.now.compose.data.module.ServicePool | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
|
||
class MyPageViewModel : ViewModel() { | ||
private val authService by lazy { ServicePool.authService } | ||
|
||
private val _infoState = | ||
MutableStateFlow( | ||
ResponseInfoDto( | ||
code = 0, | ||
message = "", | ||
data = UserInfo(authenticationId = "", nickname = "", phone = "") | ||
) | ||
) | ||
val infoState = _infoState.asStateFlow() | ||
|
||
init { | ||
fetchInfo() | ||
} | ||
|
||
private fun fetchInfo() { | ||
authService.memberInfo().enqueue(object : Callback<ResponseInfoDto> { | ||
override fun onResponse( | ||
call: Call<ResponseInfoDto>, | ||
response: Response<ResponseInfoDto>, | ||
) { | ||
if (response.isSuccessful) { | ||
val data = response.body() | ||
if (data != null) { | ||
_infoState.update { | ||
data | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<ResponseInfoDto>, t: Throwable) { | ||
Log.e("MyPageError", "${t.message}") | ||
} | ||
}) | ||
} | ||
} |