Skip to content

Commit 4a7598d

Browse files
committed
Merge branch 'dev'
2 parents ba79a7b + baddd7c commit 4a7598d

File tree

5 files changed

+58
-39
lines changed

5 files changed

+58
-39
lines changed

app/src/main/java/com/ddd/attendance/check/vm/LoginViewModel.kt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@ package com.ddd.attendance.check.vm
22

33
import androidx.databinding.ObservableField
44
import androidx.lifecycle.LiveData
5+
import androidx.lifecycle.MutableLiveData
56
import androidx.lifecycle.ViewModel
67
import com.ddd.attendance.check.common.NetworkHelper
7-
import com.ddd.attendance.check.common.NetworkHelper.SUCCESS
88
import com.ddd.attendance.check.data.repository.LoginRepository
99
import com.ddd.attendance.check.data.repository.UserRepository
1010
import com.ddd.attendance.check.db.entity.User
1111
import com.ddd.attendance.check.ui.MainActivity
1212
import com.ddd.attendance.check.utill.SingleLiveEvent
1313
import kotlinx.coroutines.GlobalScope
1414
import kotlinx.coroutines.launch
15+
import org.json.JSONObject
1516
import javax.inject.Inject
1617

1718
class LoginViewModel @Inject constructor(
1819
private val loginRepository: LoginRepository,
1920
private val userRepository: UserRepository
2021
) : ViewModel() {
21-
private val _progressbar = SingleLiveEvent<Boolean>()
22-
private val _error = SingleLiveEvent<String>()
22+
private val _progressbar = MutableLiveData<Boolean>()
23+
private val _error = MutableLiveData<String>()
2324
private val _btnEnableLogin = SingleLiveEvent<Boolean>()
24-
private val _startActivity = SingleLiveEvent<Class<MainActivity>>()
25+
private val _startActivity = MutableLiveData<Class<MainActivity>>()
2526

2627
val editID = ObservableField<String>()
2728
val editPW = ObservableField<String>()
@@ -51,8 +52,8 @@ class LoginViewModel @Inject constructor(
5152
_progressbar.postValue(false)
5253
val response = loginRepository.login(editID.get() ?: EMPTY_ID, editPW.get()
5354
?: EMPTY_PW)
54-
response.body()?.let { result ->
55-
if (result.status == SUCCESS) {
55+
if (response.isSuccessful) {
56+
response.body()?.let { result ->
5657
userRepository.saveUsers(
5758
User(
5859
result.user.id,
@@ -63,10 +64,9 @@ class LoginViewModel @Inject constructor(
6364
result.refreshToken
6465
)
6566
)
66-
_startActivity.postValue(MainActivity::class.java)
67-
} else {
68-
_error.postValue(result.message ?: NetworkHelper.ERROR_MSG)
69-
}
67+
}.run { _startActivity.postValue(MainActivity::class.java) }
68+
} else {
69+
errorParsingDialog(response.errorBody()?.string())
7070
}
7171
} catch (throwable: Throwable) {
7272
_progressbar.postValue(false)
@@ -75,6 +75,12 @@ class LoginViewModel @Inject constructor(
7575
}
7676
}
7777

78+
private fun errorParsingDialog(msg: String?) {
79+
JSONObject(msg).optString(MainViewModel.KEY_MSG_OBJ_JSON, NetworkHelper.ERROR_MSG)
80+
.run {
81+
_error.postValue(this)
82+
}
83+
}
7884
companion object {
7985
const val EMPTY_ID = ""
8086
const val EMPTY_PW = ""

app/src/main/java/com/ddd/attendance/check/vm/MainViewModel.kt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ class MainViewModel @Inject constructor(
2929
private val _isAttendanceStart = MutableLiveData<Boolean>()
3030
private val _btnEnableLogin = MutableLiveData<Boolean>()
3131
private val _showToastError = MutableLiveData<String>()
32+
private val _isProgressbar = MutableLiveData<Boolean>()
3233

3334
val isAdmin: LiveData<Boolean> = _isAdmin
3435
val isAttendanceNumber: LiveData<Int> = _isAttendanceNumber
3536
val isAttendanceStart: LiveData<Boolean> = _isAttendanceStart
3637
val editNumberAttendance = ObservableField<String>()
3738
val btnEnableAttendance: LiveData<Boolean> get() = _btnEnableLogin
3839
val showToastError: LiveData<String> get() = _showToastError
40+
val progressbar: LiveData<Boolean> = _isProgressbar
3941

4042
val showDDDDialog = SingleLiveEvent<Pair<UserType, String>>()
4143
val expireToken = SingleLiveEvent<String>()
@@ -98,46 +100,45 @@ class MainViewModel @Inject constructor(
98100

99101
// 출석 버튼 클릭
100102
fun attendance() {
103+
_isProgressbar.value = true
101104
GlobalScope.launch {
102105
try {
103106
if (isAdmin.value == true) {
104107
if (_isAttendanceStart.value == null || _isAttendanceStart.value == false) attendanceStart()
105108
else attendanceEnd()
106109
} else attendanceCheck()
107110
} catch (e: IOException) {
111+
_isProgressbar.postValue(false)
108112
_showToastError.postValue(MSG_ATTENDANCE_END)
109113
}
110114
}
111115
}
112116
// 관리자 출첵 시작
113117
private suspend fun attendanceStart() {
114118
val response = attendanceRepository.attendanceStart()
119+
_isProgressbar.postValue(false)
115120
if (response.isSuccessful) attendanceStartUI(response.body())
116121
else errorParsingDialog(response.errorBody()?.string())
122+
117123
}
118124

119125
// 관리자 출첵 종료
120126
private suspend fun attendanceEnd() {
121127
val response = attendanceRepository.attendsEnd()
128+
_isProgressbar.postValue(false)
122129
if (response.isSuccessful) {
123130
attendanceEndUI()
124131
} else _showToastError.postValue(MSG_ALREADY_START)
125132
}
126133

127134
//일반 팀원 출첵
128135
private suspend fun attendanceCheck() {
129-
GlobalScope.launch {
130-
try {
131-
val response = attendanceRepository.attendanceCheck(
132-
userRepository.getUsers()[0].id.toString(),
133-
editNumberAttendance.get() ?: ""
134-
)
135-
if (response.isSuccessful) showDDDDialog(MSG_ATTENDANCE_SUCCESS)
136-
else errorParsingDialog(response.errorBody()?.string())
137-
} catch (e: Exception) {
138-
e.printStackTrace()
139-
}
140-
}
136+
val response = attendanceRepository.attendanceCheck(
137+
userRepository.getUsers()[0].id.toString(), editNumberAttendance.get() ?: ""
138+
)
139+
_isProgressbar.postValue(false)
140+
if (response.isSuccessful) showDDDDialog(MSG_ATTENDANCE_SUCCESS)
141+
else errorParsingDialog(response.errorBody()?.string())
141142
}
142143

143144
//토큰 리프레시
@@ -146,6 +147,7 @@ class MainViewModel @Inject constructor(
146147
try {
147148
val user = userRepository.getUsers()[0]
148149
val response = userRepository.refreshToken(user.refreshToken)
150+
_isProgressbar.postValue(false)
149151
if (response.isSuccessful) {
150152
val body = response.body()
151153
userRepository.saveUsers(
@@ -163,6 +165,7 @@ class MainViewModel @Inject constructor(
163165
expireToken.postValue(MSG_LOG_OUT)
164166
}
165167
} catch (e: Exception) {
168+
_isProgressbar.postValue(false)
166169
e.printStackTrace()
167170
}
168171
}

app/src/main/res/layout/activity_login.xml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,7 @@
1414
android:paddingStart="35dp"
1515
android:paddingEnd="35dp"
1616
tools:context=".ui.LoginActivity">
17-
<ProgressBar
18-
android:visibility="@{safeUnbox(loginViewModel.progressbar)? View.VISIBLE : View.GONE}"
19-
android:layout_width="wrap_content"
20-
app:layout_constraintBottom_toBottomOf="parent"
21-
app:layout_constraintTop_toTopOf="parent"
22-
app:layout_constraintRight_toRightOf="parent"
23-
app:layout_constraintLeft_toLeftOf="parent"
24-
android:layout_height="wrap_content"/>
17+
2518
<TextView
2619
android:id="@+id/tvSpecialTitle"
2720
android:layout_width="wrap_content"
@@ -62,10 +55,8 @@
6255
tools:ignore="SpUsage" />
6356
<com.google.android.material.textfield.TextInputLayout
6457
android:id="@+id/editIDLayout"
65-
style="@style/LoginEditTheme"
6658
android:layout_width="match_parent"
6759
android:layout_height="wrap_content"
68-
android:background="@null"
6960
android:hint="@string/login_input_id_hint"
7061
android:textColor="@android:color/black"
7162
android:textColorHint="@color/login_edit_hint"
@@ -80,7 +71,6 @@
8071
android:onTextChanged="@{(text, start, before, count) -> loginViewModel.onIDTextChanged(text)}"
8172
android:text="@={loginViewModel.editID}"
8273
android:layout_height="wrap_content"
83-
android:background="@null"
8474
android:textSize="12dp"
8575
tools:ignore="SpUsage" />
8676
</com.google.android.material.textfield.TextInputLayout>
@@ -93,8 +83,6 @@
9383
android:textColorHint="@color/login_edit_hint"
9484
app:layout_constraintLeft_toLeftOf="parent"
9585
app:layout_constraintRight_toRightOf="parent"
96-
android:background="@null"
97-
style="@style/LoginEditTheme"
9886
android:theme="@style/LoginEditTheme"
9987
app:layout_constraintTop_toBottomOf="@+id/editIDLayout"
10088
android:layout_height="wrap_content">
@@ -104,7 +92,6 @@
10492
android:text="@={loginViewModel.editPW}"
10593
android:layout_width="match_parent"
10694
android:layout_height="wrap_content"
107-
android:background="@null"
10895
android:textSize="12dp"
10996
tools:ignore="SpUsage" />
11097
</com.google.android.material.textfield.TextInputLayout>
@@ -120,8 +107,16 @@
120107
android:text="@string/login"
121108
android:textColor="@android:color/white"
122109
android:textSize="16dp"
123-
app:layout_constraintBottom_toBottomOf="parent"
124110
app:layout_constraintTop_toBottomOf="@+id/editPWLayout"
125111
tools:ignore="SpUsage"/>
112+
113+
<ProgressBar
114+
android:visibility="@{safeUnbox(loginViewModel.progressbar)? View.VISIBLE : View.GONE}"
115+
android:layout_width="wrap_content"
116+
app:layout_constraintBottom_toBottomOf="parent"
117+
app:layout_constraintTop_toTopOf="parent"
118+
app:layout_constraintRight_toRightOf="parent"
119+
app:layout_constraintLeft_toLeftOf="parent"
120+
android:layout_height="wrap_content"/>
126121
</androidx.constraintlayout.widget.ConstraintLayout>
127122
</layout>

app/src/main/res/layout/activity_main.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
android:layout_width="wrap_content"
5656
android:layout_height="wrap_content"
5757
android:visibility="@{safeUnbox(mainViewModel.isAdmin)? View.VISIBLE:View.GONE}"
58-
android:text='@{safeUnbox(mainViewModel.isAttendanceNumber>0)?String.valueOf(safeUnbox(mainViewModel.isAttendanceNumber)):""}'
58+
android:text='@{safeUnbox(mainViewModel.isAttendanceNumber)>0?String.valueOf(safeUnbox(mainViewModel.isAttendanceNumber)):""}'
5959
android:textColor="@android:color/black"
6060
android:textSize="84dp"
6161
android:textStyle="bold"
@@ -138,5 +138,14 @@
138138
android:text="@string/attendance"
139139
app:layout_constraintBottom_toBottomOf="parent"
140140
tools:ignore="SpUsage"/>
141+
142+
<ProgressBar
143+
android:visibility="@{safeUnbox(mainViewModel.progressbar) ? View.VISIBLE : View.GONE}"
144+
app:layout_constraintBottom_toBottomOf="parent"
145+
app:layout_constraintRight_toRightOf="parent"
146+
app:layout_constraintLeft_toLeftOf="parent"
147+
app:layout_constraintTop_toTopOf="parent"
148+
android:layout_width="wrap_content"
149+
android:layout_height="wrap_content"/>
141150
</androidx.constraintlayout.widget.ConstraintLayout>
142151
</layout>

app/src/main/res/values/styles.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@
2222
<item name="colorPrimaryDark">@color/attendance_admin_background_color</item>
2323
</style>
2424

25-
<style name="LoginEditTheme" parent="Widget.AppCompat.EditText">
25+
<style name="LoginEditTheme" parent="AppTheme">
2626
<item name="android:textColor">@android:color/white</item>
2727
<item name="android:textColorHint">@android:color/darker_gray</item>
2828
<item name="colorAccent">@color/black</item>
2929
<item name="colorControlNormal">@color/login_edit_hint</item>
3030
<item name="colorControlActivated">@color/black</item>
31+
32+
<!--<item name="colorControlNormal">@color/login_edit_hint</item>-->
33+
<!--<item name="colorControlActivated">@color/black</item>-->
34+
<!--<item name="android:textCursorDrawable">@color/login_edit_hint</item>-->
35+
<!--<item name="android:textColorHighlight">>@color/login_edit_hint</item>-->
36+
<!--<item name="colorAccent">@color/black</item>-->
3137
</style>
3238
</resources>

0 commit comments

Comments
 (0)