Bläddra i källkod

+ add logout function

Dola TENG 2 år sedan
förälder
incheckning
a0afb8e6ec

+ 9 - 0
app/src/main/java/com/khmer9/cock8888main/remote/LoginDataModel.kt

@@ -27,6 +27,15 @@ data class LoginResponse(
     val data: LoginData?
 )
 
+@JsonClass(generateAdapter = true)
+data class LogoutResponse(
+    @Json(name = "code")
+    val resultCode: String,
+
+    @Json(name = "message")
+    val message: LoginMessage,
+)
+
 @JsonClass(generateAdapter = true)
 data class LoginMessage(
     @Json(name = "code")

+ 3 - 0
app/src/main/java/com/khmer9/cock8888main/remote/service/ApiService.kt

@@ -13,6 +13,9 @@ interface ApiService {
     @Headers("No-Authentication: true")
     fun login(@Body loginRequest: LoginRequest): Single<LoginResponse>
 
+    @GET("/v1/api/user/logout")
+    fun logout(): Single<LogoutResponse>
+
     @GET("/v1/api/user")
     fun remoteUser(): Single<UserResponse>
 

+ 10 - 22
app/src/main/java/com/khmer9/cock8888main/screen/setting/SettingFragment.kt

@@ -19,6 +19,8 @@ import com.khmer9.cock8888main.screen.login.LoginActivity
 import com.mazenrashed.printooth.Printooth
 import com.mazenrashed.printooth.ui.ScanningActivity
 import kotlinx.android.synthetic.main.fragment_setting.*
+import java.util.*
+import kotlin.concurrent.timerTask
 
 /**
  * A simple [Fragment] subclass as the second destination in the navigation.
@@ -130,37 +132,29 @@ class SettingFragment : BaseFragment(R.layout.fragment_setting) {
         v_logout.setOnClickListener {
             if (!sharePref.getIsMute()) betTouch.start()
             it.btnClick().subscribe {
-                sharePref.logout()
-
-                val intent = Intent(requireContext(), LoginActivity::class.java)
-                startActivity(intent)
-                requireActivity().finishAffinity()
-
+                settingViewModel.logout()
+
+                Timer().schedule(timerTask {
+                    sharePref.logout()
+                    val intent = Intent(requireContext(), LoginActivity::class.java)
+                    startActivity(intent)
+                    requireActivity().finishAffinity()
+                }, 2000)
             }
         }
-
     }
 
     private fun getResult(state: UpdatePwdViewState) {
-
-        // if (state.isProgress){
-        // Toast.makeText(requireContext(), "Loading", Toast.LENGTH_LONG).show()
-
-        //}
         if (state.isUpdateSuccess) {
             //Toast.makeText(requireContext(), "Update Sucess", Toast.LENGTH_LONG).show()
             val intent = Intent(requireContext(), LoginActivity::class.java)
             startActivity(intent)
             requireActivity().finishAffinity()
-
         }
         if (state.error != null) {
             //Toast.makeText(requireContext(), "Error", Toast.LENGTH_LONG).show()
             tv_error.text = state.error
-
-
         }
-
     }
 
     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
@@ -170,12 +164,6 @@ class SettingFragment : BaseFragment(R.layout.fragment_setting) {
             val pair = Printooth.getPairedPrinter()
             v_print.setImageViewIcon(R.drawable.blue_text_dc)
             text_connect.text = pair?.name
-
-
-            //Printooth.removeCurrentPrinter()
-//https://github.com/mazenrashed/Printooth
-
         }
-        //Printer is ready now
     }
 }

+ 25 - 1
app/src/main/java/com/khmer9/cock8888main/screen/setting/SettingViewModel.kt

@@ -13,6 +13,7 @@ import java.util.concurrent.TimeUnit
 class SettingViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
     private val _state = MutableLiveData<UpdatePwdViewState>(UpdatePwdViewState())
     val state: LiveData<UpdatePwdViewState> = _state
+    private fun prev() = _state.value!!
 
     fun updatePassword(oldPassword: String, newPassword: String) {
         if (_state.value!!.isProgress) return
@@ -41,7 +42,30 @@ class SettingViewModel(val apiService: ApiService, val prefHelper: PrefHelper) :
         )
     }
 
-    private fun prev() = _state.value!!
+    private val _stateLogout = MutableLiveData<LogoutViewState>(LogoutViewState())
+    val stateLogout: LiveData<LogoutViewState> = _stateLogout
+    private fun prevLogout() = _stateLogout.value!!
 
+    fun logout() {
+        if (_stateLogout.value!!.isProgress) return
+        _stateLogout.value = prevLogout().copy(isProgress = true)
 
+        disposables.add(
+            apiService.logout()
+                .timeout(60, TimeUnit.SECONDS)
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe({
+                    if (it.resultCode == "1") {
+                        _stateLogout.value = prevLogout().copy(isProgress = false, isSuccess = true)
+                        prefHelper.logout()
+                    } else {
+                        _stateLogout.value = prevLogout().copy(isProgress = false, error = "[${it.message.description}]")
+                    }
+                }, {
+                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ\n" + it.getErrorCode()
+                    _stateLogout.value = prevLogout().copy(isProgress = false, error = message)
+                })
+        )
+    }
 }

+ 7 - 2
app/src/main/java/com/khmer9/cock8888main/screen/setting/UpdatePwdViewState.kt

@@ -1,10 +1,15 @@
 package com.khmer9.cock8888main.screen.setting
 
-
-
 data class UpdatePwdViewState(
     val initial: Boolean = false,
     val isProgress: Boolean = false,
     val isUpdateSuccess: Boolean = false,
     val error: String? = null
+)
+
+data class LogoutViewState(
+    val initial: Boolean = false,
+    val isProgress: Boolean = false,
+    val isSuccess: Boolean = false,
+    val error: String? = null
 )