123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package com.vpn.fastestvpnservice.viewmodels
- import android.util.Log
- import android.widget.Toast
- import androidx.lifecycle.LiveData
- import androidx.lifecycle.MutableLiveData
- import androidx.lifecycle.ViewModel
- import com.google.gson.Gson
- import com.google.gson.reflect.TypeToken
- import com.vpn.fastestvpnservice.beans.DataResponse
- import com.vpn.fastestvpnservice.retrofit.RetrofitNetworkHandling
- import com.vpn.fastestvpnservice.retrofit.WebServiceFactory
- import de.blinkt.openvpn.core.App
- import retrofit2.Call
- import java.util.Objects
- class AccountViewModel: ViewModel() {
- var mutableLiveDataLogout = MutableLiveData<Boolean>()
- var liveDataLogout: LiveData<Boolean> = mutableLiveDataLogout
- var mutableLiveDataLogoutStatus = MutableLiveData<Boolean>(false)
- var liveDataLogoutStatus: LiveData<Boolean> = mutableLiveDataLogoutStatus
- var mutableLiveDataDelete = MutableLiveData<DataResponse<Objects>?>()
- var liveDataDelete: LiveData<DataResponse<Objects>?> = mutableLiveDataDelete
- var mutableLiveDataDeleteStatus = MutableLiveData<Boolean>(false)
- var liveDataDeleteStatus: LiveData<Boolean> = mutableLiveDataDeleteStatus
- var mutableLiveDataChangePassword = MutableLiveData<DataResponse<Objects>?>()
- var liveDataChangePassword: LiveData<DataResponse<Objects>?> = mutableLiveDataChangePassword
- fun logout(){
- WebServiceFactory.getInstance().logout().enqueue(
- RetrofitNetworkHandling<Any>(
- object : RetrofitNetworkHandling.ResponseCallback<Any> {
- override fun onSuccess(call: Call<Any>?, response: Any?) {
- try {
- Log.d("test_api_response","Logout onSuccess: ${response.toString()}")
- val gson = Gson()
- val jsonString = gson.toJson(response)
- val type = object : TypeToken<DataResponse<Object>>() {}.type
- val data = gson.fromJson<DataResponse<Object>>(jsonString, type)
- mutableLiveDataLogout.value = data.status
- Log.d("test_api_response","Logout After onSuccess: ${response.toString()}")
- } catch (ex: Exception) {
- }
- }
- override fun onFail(call: Call<Any>?, response: Any?) {
- Log.d("test_api_response","Logout onFail:")
- mutableLiveDataLogout.value = false
- }
- override fun onError(call: Call<Any>?, response: Any?) {
- Log.d("test_api_response","Logout onError:")
- mutableLiveDataLogout.value = false
- }
- })
- )
- }
- fun setLogoutStatus(status: Boolean) {
- mutableLiveDataLogoutStatus.value = status
- }
- fun deleteAccount() {
- WebServiceFactory.getInstance().deleteAccount().enqueue(
- RetrofitNetworkHandling<Any>(object :
- RetrofitNetworkHandling.ResponseCallback<Any> {
- override fun onSuccess(call: Call<Any>?, response: Any?) {
- try {
- val gson = Gson()
- val jsonString = gson.toJson(response)
- val type = object : TypeToken<DataResponse<Objects>>() {}.type
- val data = gson.fromJson<DataResponse<Objects>>(jsonString, type)
- Log.d("test_api_response","Delete try ${data.status}:")
- mutableLiveDataDelete.value = data
- } catch (ex: Exception) {
- }
- }
- override fun onFail(call: Call<Any>?, response: Any?) {
- Log.d("test_api_response","Delete onFail:")
- mutableLiveDataDelete.value = DataResponse(false)
- }
- override fun onError(call: Call<Any>?, response: Any?) {
- Log.d("test_api_response","Delete onError:")
- mutableLiveDataDelete.value = DataResponse(false)
- }
- })
- )
- }
- fun setDeleteStatus(status: Boolean) {
- mutableLiveDataDeleteStatus.value = status
- }
- fun changePassword(current_password: String, new_password: String, confirm_password: String) {
- Log.d("ChangePassword: ","email = $current_password $new_password $confirm_password")
- WebServiceFactory.getInstance()
- .changePassword(current_password, new_password, confirm_password).enqueue(
- RetrofitNetworkHandling<Any>(object :
- RetrofitNetworkHandling.ResponseCallback<Any> {
- override fun onSuccess(call: Call<Any>?, response: Any?) {
- try {
- val gson = Gson()
- val jsonString = gson.toJson(response)
- val type = object : TypeToken<DataResponse<Objects>>() {}.type
- val data = gson.fromJson<DataResponse<Objects>>(jsonString, type)
- mutableLiveDataChangePassword.value = data
- Log.d("ChangePassword: ","ChangePassword Response: ${data.status} ${data.message}")
- } catch (ex: Exception) {
- }
- }
- override fun onFail(call: Call<Any>?, response: Any?) {
- mutableLiveDataChangePassword.value = null
- }
- override fun onError(call: Call<Any>?, response: Any?) {
- mutableLiveDataChangePassword.value = null
- }
- })
- )
- }
- }
|