Add the Remove Account functionality.
- Created the functionality to completely remove the current user account and its corresponding data from the database and the shared preferences, which will restart the whole application avoiding showing the License again.
This commit is contained in:
parent
85f20bf11d
commit
544b79321b
2 changed files with 35 additions and 2 deletions
|
@ -9,7 +9,6 @@ import android.util.Log
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.Toast
|
|
||||||
import androidx.appcompat.widget.Toolbar
|
import androidx.appcompat.widget.Toolbar
|
||||||
import androidx.collection.LongSparseArray
|
import androidx.collection.LongSparseArray
|
||||||
import androidx.lifecycle.ViewModelProviders
|
import androidx.lifecycle.ViewModelProviders
|
||||||
|
@ -508,12 +507,31 @@ class SettingsFragment : ConnectedFragment(), BaseSecurityLockDialog.OnPINPatter
|
||||||
message(R.string.msg__remove_account_confirmation)
|
message(R.string.msg__remove_account_confirmation)
|
||||||
negativeButton(android.R.string.cancel)
|
negativeButton(android.R.string.cancel)
|
||||||
positiveButton(android.R.string.ok) {
|
positiveButton(android.R.string.ok) {
|
||||||
Toast.makeText(it.context, "Removing Account", Toast.LENGTH_SHORT).show()
|
removeAccount(it.context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun removeAccount(context: Context) {
|
||||||
|
// Clears the database.
|
||||||
|
mViewModel.clearDatabase(context)
|
||||||
|
|
||||||
|
// Clears the shared preferences.
|
||||||
|
val pref = PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
|
pref.edit().clear().apply()
|
||||||
|
|
||||||
|
// Marks the license as agreed, so that it is not shown to the user again.
|
||||||
|
pref.edit().putInt(
|
||||||
|
Constants.KEY_LAST_AGREED_LICENSE_VERSION, Constants.CURRENT_LICENSE_VERSION).apply()
|
||||||
|
|
||||||
|
// Restarts the activity, which will restart the whole application since it uses a
|
||||||
|
// single activity architecture.
|
||||||
|
val intent = activity?.intent
|
||||||
|
activity?.finish()
|
||||||
|
activity?.startActivity(intent)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onServiceDisconnected(name: ComponentName?) {
|
override fun onServiceDisconnected(name: ComponentName?) {
|
||||||
super.onServiceDisconnected(name)
|
super.onServiceDisconnected(name)
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,17 @@
|
||||||
package cy.agorise.bitsybitshareswallet.viewmodels
|
package cy.agorise.bitsybitshareswallet.viewmodels
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
|
import android.content.Context
|
||||||
import androidx.lifecycle.AndroidViewModel
|
import androidx.lifecycle.AndroidViewModel
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import cy.agorise.bitsybitshareswallet.database.BitsyDatabase
|
||||||
import cy.agorise.bitsybitshareswallet.database.entities.UserAccount
|
import cy.agorise.bitsybitshareswallet.database.entities.UserAccount
|
||||||
import cy.agorise.bitsybitshareswallet.repositories.AuthorityRepository
|
import cy.agorise.bitsybitshareswallet.repositories.AuthorityRepository
|
||||||
import cy.agorise.bitsybitshareswallet.repositories.UserAccountRepository
|
import cy.agorise.bitsybitshareswallet.repositories.UserAccountRepository
|
||||||
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
class SettingsFragmentViewModel(application: Application) : AndroidViewModel(application) {
|
class SettingsFragmentViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
private var mUserAccountRepository = UserAccountRepository(application)
|
private var mUserAccountRepository = UserAccountRepository(application)
|
||||||
|
@ -18,4 +24,13 @@ class SettingsFragmentViewModel(application: Application) : AndroidViewModel(app
|
||||||
internal fun getWIF(userId: String, authorityType: Int): LiveData<String> {
|
internal fun getWIF(userId: String, authorityType: Int): LiveData<String> {
|
||||||
return mAuthorityRepository.getWIF(userId, authorityType)
|
return mAuthorityRepository.getWIF(userId, authorityType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal fun clearDatabase(context: Context) {
|
||||||
|
val db = BitsyDatabase.getDatabase(context)
|
||||||
|
viewModelScope.launch {
|
||||||
|
withContext(IO) {
|
||||||
|
db?.clearAllTables()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue