@@ -4,30 +4,34 @@ import androidx.databinding.ObservableField
44import androidx.lifecycle.LiveData
55import androidx.lifecycle.MutableLiveData
66import androidx.lifecycle.ViewModel
7+ import com.ddd.attendance.check.DDDApplication
78import com.ddd.attendance.check.common.NetworkHelper
89import com.ddd.attendance.check.common.NetworkHelper.ERROR_MSG
910import com.ddd.attendance.check.common.UserType
1011import com.ddd.attendance.check.data.repository.AttendanceRepository
1112import com.ddd.attendance.check.data.repository.UserRepository
1213import com.ddd.attendance.check.model.Attendance
14+ import com.ddd.attendance.check.utill.SharedPreferences
1315import com.ddd.attendance.check.utill.SingleLiveEvent
14- import kotlinx.coroutines.GlobalScope
15- import kotlinx.coroutines.launch
16+ import kotlinx.coroutines.*
1617import java.io.IOException
1718import javax.inject.Inject
1819
1920class MainViewModel @Inject constructor(
20- private val userRepository : UserRepository ,
21- private val attendanceRepository : AttendanceRepository
21+ private val userRepository : UserRepository ,
22+ private val attendanceRepository : AttendanceRepository ,
23+ private val dddApplication : DDDApplication
2224) : ViewModel() {
2325
2426 private val _isAdmin = MutableLiveData <Boolean >()
27+ private val _isAttendanceNumberCount = MutableLiveData <Int >()
2528 private val _isAttendanceNumber = MutableLiveData <Int >()
2629 private val _isAttendanceStart = MutableLiveData <Boolean >()
2730 private val _btnEnableLogin = MutableLiveData <Boolean >()
2831 private val _showToastError = MutableLiveData <String >()
2932
3033 val isAdmin: LiveData <Boolean > = _isAdmin
34+ val isAttendanceNumberCount: LiveData <Int > = _isAttendanceNumberCount
3135 val isAttendanceNumber: LiveData <Int > = _isAttendanceNumber
3236 val isAttendanceStart: LiveData <Boolean > = _isAttendanceStart
3337 val editNumberAttendance = ObservableField <String >()
@@ -36,6 +40,11 @@ class MainViewModel @Inject constructor(
3640
3741 val showDDDDialog = SingleLiveEvent <Pair <UserType , String >>()
3842
43+
44+ init {
45+ _isAttendanceStart .value = SharedPreferences .getStatus(dddApplication)
46+ }
47+
3948 fun onInputNumberTextChanged (input : CharSequence ) {
4049 editNumberAttendance.set(input.toString())
4150 _btnEnableLogin .value = ! editNumberAttendance.get().isNullOrEmpty()
@@ -73,6 +82,7 @@ class MainViewModel @Inject constructor(
7382
7483 private fun attendanceEndUI () {
7584 _isAttendanceNumber .postValue(EMPTY_NUMBER )
85+ _isAttendanceNumberCount .postValue(EMPTY_NUMBER )
7686 _isAttendanceStart .postValue(false )
7787 showDDDDialog(MSG_ATTENDANCE_END )
7888 }
@@ -93,27 +103,55 @@ class MainViewModel @Inject constructor(
93103
94104 private suspend fun attendanceStart () {
95105 val response = attendanceRepository.attendanceStart()
96- if (response.isSuccessful) attendanceStartUI(response.body())
97- else _showToastError .postValue(response.body()?.message)
106+ if (response.isSuccessful) {
107+ SharedPreferences .saveStatus(dddApplication, true )
108+ attendanceStartUI(response.body())
109+ startTimer()
110+ } else {
111+ _showToastError .postValue(MSG_ALREADY_START )
112+ }
113+ }
114+
115+ private var job: Job ? = null
116+ private suspend fun startTimer () {
117+ CoroutineScope (Dispatchers .Default ).launch {
118+ job = launch(Dispatchers .Main ) {
119+ 60 .countDown()
120+ }
121+ job?.join()
122+ }
98123 }
99124
100125 private suspend fun attendanceEnd () {
101126 val response = attendanceRepository.attendsEnd()
102- if (response.isSuccessful) attendanceEndUI()
103- else _showToastError .postValue(response.body()?.message)
127+ if (response.isSuccessful) {
128+ SharedPreferences .saveStatus(dddApplication, false )
129+ attendanceEndUI()
130+ if (job != null ) job!! .cancel()
131+ } else _showToastError .postValue(MSG_ALREADY_START )
104132 }
105133
134+ // 일반 팀원 출첵 함수
106135 private fun attendanceCheck () {
107136
108137 }
109138
139+ private suspend fun Int.countDown () {
140+ for (index in this downTo 1 ) {
141+ _isAttendanceNumberCount .postValue(index)
142+ delay(COUNT_DELAY )
143+ }
144+ }
145+
110146 private fun showDDDDialog (message : String ) {
111147 showDDDDialog.postValue(Pair (if (_isAdmin .value == true ) UserType .ADMIN else UserType .BASIC , message))
112148 }
113149
114150 companion object {
115151 const val MSG_ATTENDANCE_START = " 출석체크가 시작되었습니다."
116152 const val MSG_ATTENDANCE_END = " 출석체크가 종료되었습니다."
153+ const val MSG_ALREADY_START = " 이미 출석 체크가 시작되었습니다.\n 잠시후 다시 시도 해주세요."
117154 const val EMPTY_NUMBER = 0
155+ const val COUNT_DELAY : Long = 1000
118156 }
119157}
0 commit comments