generated from GO-SOPT-ANDROID/android-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature/#9] getUserList 기능에 viewModel 적용
- Loading branch information
Showing
3 changed files
with
25 additions
and
46 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
41 changes: 16 additions & 25 deletions
41
app/src/main/java/org/android/go/sopt/present/viewModel/MainPageViewModel.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 |
---|---|---|
@@ -1,35 +1,26 @@ | ||
package org.android.go.sopt.present.viewModel | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import org.android.go.sopt.remote.ServicePool | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.launch | ||
import org.android.go.sopt.remote.remoteData.model.ResponseListUsersDto | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
import org.android.go.sopt.remote.remoteData.repoImpl.MainPageRepoImpl | ||
|
||
class MainPageViewModel : ViewModel() { | ||
private val mainPageService = ServicePool.mainPageService | ||
class MainPageViewModel(private val mainPageRepoImpl: MainPageRepoImpl) : ViewModel() { | ||
|
||
private val _userList = MutableLiveData<ResponseListUsersDto>() | ||
val userList: LiveData<ResponseListUsersDto> get() = _userList | ||
private val _userList = MutableLiveData<List<ResponseListUsersDto.Data>>() | ||
val userList: LiveData<List<ResponseListUsersDto.Data>> get() = _userList | ||
|
||
fun gerUserList() { | ||
mainPageService.getListUsers().enqueue(object : Callback<ResponseListUsersDto> { | ||
override fun onResponse( | ||
call: Call<ResponseListUsersDto>, | ||
response: Response<ResponseListUsersDto> | ||
) { | ||
if (response.isSuccessful){ | ||
_userList.value = response.body() | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<ResponseListUsersDto>, t: Throwable) { | ||
|
||
} | ||
}) | ||
fun getUserList() = viewModelScope.launch { | ||
kotlin.runCatching { | ||
mainPageRepoImpl.getUserList() | ||
}.onSuccess { response -> | ||
_userList.value = response.data | ||
}.onFailure { | ||
Log.d("mainPageViewModel", "서버 에러 발생") | ||
} | ||
} | ||
|
||
} | ||
} |
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