AccountViewModel.kt 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package com.vpn.fastestvpnservice.viewmodels
  2. import android.util.Log
  3. import android.widget.Toast
  4. import androidx.lifecycle.LiveData
  5. import androidx.lifecycle.MutableLiveData
  6. import androidx.lifecycle.ViewModel
  7. import com.google.gson.Gson
  8. import com.google.gson.reflect.TypeToken
  9. import com.vpn.fastestvpnservice.beans.DataResponse
  10. import com.vpn.fastestvpnservice.retrofit.RetrofitNetworkHandling
  11. import com.vpn.fastestvpnservice.retrofit.WebServiceFactory
  12. import de.blinkt.openvpn.core.App
  13. import retrofit2.Call
  14. import java.util.Objects
  15. class AccountViewModel: ViewModel() {
  16. var mutableLiveDataLogout = MutableLiveData<Boolean>()
  17. var liveDataLogout: LiveData<Boolean> = mutableLiveDataLogout
  18. var mutableLiveDataLogoutStatus = MutableLiveData<Boolean>(false)
  19. var liveDataLogoutStatus: LiveData<Boolean> = mutableLiveDataLogoutStatus
  20. var mutableLiveDataDelete = MutableLiveData<DataResponse<Objects>?>()
  21. var liveDataDelete: LiveData<DataResponse<Objects>?> = mutableLiveDataDelete
  22. var mutableLiveDataDeleteStatus = MutableLiveData<Boolean>(false)
  23. var liveDataDeleteStatus: LiveData<Boolean> = mutableLiveDataDeleteStatus
  24. var mutableLiveDataChangePassword = MutableLiveData<DataResponse<Objects>?>()
  25. var liveDataChangePassword: LiveData<DataResponse<Objects>?> = mutableLiveDataChangePassword
  26. fun logout(){
  27. WebServiceFactory.getInstance().logout().enqueue(
  28. RetrofitNetworkHandling<Any>(
  29. object : RetrofitNetworkHandling.ResponseCallback<Any> {
  30. override fun onSuccess(call: Call<Any>?, response: Any?) {
  31. try {
  32. Log.d("test_api_response","Logout onSuccess: ${response.toString()}")
  33. val gson = Gson()
  34. val jsonString = gson.toJson(response)
  35. val type = object : TypeToken<DataResponse<Object>>() {}.type
  36. val data = gson.fromJson<DataResponse<Object>>(jsonString, type)
  37. mutableLiveDataLogout.value = data.status
  38. Log.d("test_api_response","Logout After onSuccess: ${response.toString()}")
  39. } catch (ex: Exception) {
  40. }
  41. }
  42. override fun onFail(call: Call<Any>?, response: Any?) {
  43. Log.d("test_api_response","Logout onFail:")
  44. mutableLiveDataLogout.value = false
  45. }
  46. override fun onError(call: Call<Any>?, response: Any?) {
  47. Log.d("test_api_response","Logout onError:")
  48. mutableLiveDataLogout.value = false
  49. }
  50. })
  51. )
  52. }
  53. fun setLogoutStatus(status: Boolean) {
  54. mutableLiveDataLogoutStatus.value = status
  55. }
  56. fun deleteAccount() {
  57. WebServiceFactory.getInstance().deleteAccount().enqueue(
  58. RetrofitNetworkHandling<Any>(object :
  59. RetrofitNetworkHandling.ResponseCallback<Any> {
  60. override fun onSuccess(call: Call<Any>?, response: Any?) {
  61. try {
  62. val gson = Gson()
  63. val jsonString = gson.toJson(response)
  64. val type = object : TypeToken<DataResponse<Objects>>() {}.type
  65. val data = gson.fromJson<DataResponse<Objects>>(jsonString, type)
  66. Log.d("test_api_response","Delete try ${data.status}:")
  67. mutableLiveDataDelete.value = data
  68. } catch (ex: Exception) {
  69. }
  70. }
  71. override fun onFail(call: Call<Any>?, response: Any?) {
  72. Log.d("test_api_response","Delete onFail:")
  73. mutableLiveDataDelete.value = DataResponse(false)
  74. }
  75. override fun onError(call: Call<Any>?, response: Any?) {
  76. Log.d("test_api_response","Delete onError:")
  77. mutableLiveDataDelete.value = DataResponse(false)
  78. }
  79. })
  80. )
  81. }
  82. fun setDeleteStatus(status: Boolean) {
  83. mutableLiveDataDeleteStatus.value = status
  84. }
  85. fun changePassword(current_password: String, new_password: String, confirm_password: String) {
  86. Log.d("ChangePassword: ","email = $current_password $new_password $confirm_password")
  87. WebServiceFactory.getInstance()
  88. .changePassword(current_password, new_password, confirm_password).enqueue(
  89. RetrofitNetworkHandling<Any>(object :
  90. RetrofitNetworkHandling.ResponseCallback<Any> {
  91. override fun onSuccess(call: Call<Any>?, response: Any?) {
  92. try {
  93. val gson = Gson()
  94. val jsonString = gson.toJson(response)
  95. val type = object : TypeToken<DataResponse<Objects>>() {}.type
  96. val data = gson.fromJson<DataResponse<Objects>>(jsonString, type)
  97. mutableLiveDataChangePassword.value = data
  98. Log.d("ChangePassword: ","ChangePassword Response: ${data.status} ${data.message}")
  99. } catch (ex: Exception) {
  100. }
  101. }
  102. override fun onFail(call: Call<Any>?, response: Any?) {
  103. mutableLiveDataChangePassword.value = null
  104. }
  105. override fun onError(call: Call<Any>?, response: Any?) {
  106. mutableLiveDataChangePassword.value = null
  107. }
  108. })
  109. )
  110. }
  111. }