@@ -2,23 +2,25 @@ package com.runnect.runnect.presentation.storage.mydrawdetail
22
33import androidx.lifecycle.LiveData
44import androidx.lifecycle.MutableLiveData
5- import androidx.lifecycle.ViewModel
6- import androidx.lifecycle.viewModelScope
75import com.runnect.runnect.data.dto.CourseData
86import com.runnect.runnect.data.dto.request.RequestPatchMyDrawCourseTitle
97import com.runnect.runnect.data.dto.request.RequestPutMyDrawCourse
8+ import com.runnect.runnect.domain.common.toLog
109import com.runnect.runnect.domain.entity.EditableMyDrawCourseDetail
1110import com.runnect.runnect.domain.entity.MyDrawCourseDetail
1211import com.runnect.runnect.domain.repository.CourseRepository
12+ import com.runnect.runnect.presentation.base.BaseViewModel
1313import com.runnect.runnect.presentation.state.UiStateV2
14+ import com.runnect.runnect.util.extension.collectResult
1415import dagger.hilt.android.lifecycle.HiltViewModel
15- import kotlinx.coroutines.launch
1616import timber.log.Timber
1717import javax.inject.Inject
1818
1919@HiltViewModel
20- class MyDrawDetailViewModel @Inject constructor(private val courseRepository : CourseRepository ) :
21- ViewModel () {
20+ class MyDrawDetailViewModel @Inject constructor(
21+ private val courseRepository : CourseRepository
22+ ) : BaseViewModel() {
23+
2224 private val _courseGetState = MutableLiveData <UiStateV2 <MyDrawCourseDetail >>()
2325 val courseGetState: LiveData <UiStateV2 <MyDrawCourseDetail >>
2426 get() = _courseGetState
@@ -36,61 +38,51 @@ class MyDrawDetailViewModel @Inject constructor(private val courseRepository: Co
3638 private var _courseTitle = " "
3739 val courseTitle get() = _courseTitle
3840
39- fun getMyDrawDetail (courseId : Int ) {
40- viewModelScope.launch {
41- courseRepository.getMyDrawDetail(
42- courseId = courseId
43- ).onSuccess { response ->
44- if (response == null ) {
45- _courseGetState .value = UiStateV2 .Failure (" MyDrawCourseDetail is null" )
46- return @launch
47- }
48-
41+ fun getMyDrawDetail (courseId : Int ) = launchWithHandler {
42+ courseRepository.getMyDrawDetail(
43+ courseId = courseId
44+ ).collectResult(
45+ onSuccess = { response ->
4946 Timber .d(" SUCCESS GET MY DRAW COURSE" )
5047 _courseGetState .value = UiStateV2 .Success (response)
5148 _courseTitle = response.title
52- }.onFailure { t ->
53- Timber .e(" FAIL GET MY DRAW COURSE: ${t.message} " )
54- _courseGetState .value = UiStateV2 .Failure (t.message.toString())
49+ },
50+ onFailure = { t ->
51+ Timber .e(" FAIL GET MY DRAW COURSE: ${t.toLog()} " )
52+ _courseGetState .value = UiStateV2 .Failure (t.toLog())
5553 }
56- }
54+ )
5755 }
5856
59- fun deleteMyDrawCourse (deleteList : MutableList <Int >) {
60- viewModelScope.launch {
61- runCatching {
62- courseRepository.deleteMyDrawCourse(
63- RequestPutMyDrawCourse (
64- courseIdList = deleteList
65- )
66- )
67- }.onSuccess { response ->
68- Timber .d(" SUCCESS DELETE MY DRAW COURSE: ${response.body()} " )
57+ fun deleteMyDrawCourse (deleteList : MutableList <Int >) = launchWithHandler {
58+ courseRepository.deleteMyDrawCourse(
59+ RequestPutMyDrawCourse (deleteList)
60+ ).collectResult(
61+ onSuccess = {
62+ Timber .d(" SUCCESS DELETE MY DRAW COURSE" )
6963 _courseDeleteState .value = UiStateV2 .Success (Unit )
70- }.onFailure { t ->
71- Timber .e(" FAIL DELETE MY DRAW COURSE: ${t.message} " )
72- _courseDeleteState .value = UiStateV2 .Failure (t.message.toString())
64+ },
65+ onFailure = { t ->
66+ Timber .e(" FAIL DELETE MY DRAW COURSE: ${t.toLog()} " )
67+ _courseDeleteState .value = UiStateV2 .Failure (t.toLog())
7368 }
74- }
69+ )
7570 }
7671
77- fun patchCourseTitle (courseId : Int ) {
78- viewModelScope.launch {
79- courseRepository.patchMyDrawCourseTitle(courseId, RequestPatchMyDrawCourseTitle (courseTitle))
80- .onSuccess { response ->
81- if (response == null ){
82- _coursePatchState .value = UiStateV2 .Failure (" PATCH MY DRAW COURSE RESPONSE IS NULL" )
83- return @launch
84- }
85-
86- Timber .d(" SUCCESS PATCH MY DRAW COURSE TITLE" )
87- _coursePatchState .value = UiStateV2 .Success (response)
88- }
89- .onFailure { t ->
90- Timber .e(" FAIL PATCH MY DRAW COURSE TITLE: ${t.message} " )
91- _coursePatchState .value = UiStateV2 .Failure (t.message.toString())
92- }
93- }
72+ fun patchCourseTitle (courseId : Int ) = launchWithHandler {
73+ courseRepository.patchMyDrawCourseTitle(
74+ courseId,
75+ RequestPatchMyDrawCourseTitle (courseTitle)
76+ ).collectResult(
77+ onSuccess = { response ->
78+ Timber .d(" SUCCESS PATCH MY DRAW COURSE TITLE" )
79+ _coursePatchState .value = UiStateV2 .Success (response)
80+ },
81+ onFailure = { t ->
82+ Timber .e(" FAIL PATCH MY DRAW COURSE TITLE: ${t.toLog()} " )
83+ _coursePatchState .value = UiStateV2 .Failure (t.toLog())
84+ }
85+ )
9486 }
9587
9688 fun updateCourseTitle (title : String ) {
0 commit comments