From 544b79321b589a556ad87e3f3eb69e94d7ac0070 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Mon, 2 Sep 2019 19:22:10 -0500 Subject: [PATCH] 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. --- .../fragments/SettingsFragment.kt | 22 +++++++++++++++++-- .../viewmodels/SettingsFragmentViewModel.kt | 15 +++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SettingsFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SettingsFragment.kt index 6122995..d4b4206 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SettingsFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SettingsFragment.kt @@ -9,7 +9,6 @@ import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.Toast import androidx.appcompat.widget.Toolbar import androidx.collection.LongSparseArray import androidx.lifecycle.ViewModelProviders @@ -508,12 +507,31 @@ class SettingsFragment : ConnectedFragment(), BaseSecurityLockDialog.OnPINPatter message(R.string.msg__remove_account_confirmation) negativeButton(android.R.string.cancel) 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?) { super.onServiceDisconnected(name) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/SettingsFragmentViewModel.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/SettingsFragmentViewModel.kt index 04944bc..7f7f547 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/SettingsFragmentViewModel.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/SettingsFragmentViewModel.kt @@ -1,11 +1,17 @@ package cy.agorise.bitsybitshareswallet.viewmodels import android.app.Application +import android.content.Context import androidx.lifecycle.AndroidViewModel 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.repositories.AuthorityRepository 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) { private var mUserAccountRepository = UserAccountRepository(application) @@ -18,4 +24,13 @@ class SettingsFragmentViewModel(application: Application) : AndroidViewModel(app internal fun getWIF(userId: String, authorityType: Int): LiveData { return mAuthorityRepository.getWIF(userId, authorityType) } + + internal fun clearDatabase(context: Context) { + val db = BitsyDatabase.getDatabase(context) + viewModelScope.launch { + withContext(IO) { + db?.clearAllTables() + } + } + } } \ No newline at end of file