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.
master
Severiano Jaramillo 2019-09-02 19:22:10 -05:00
parent 85f20bf11d
commit 544b79321b
2 changed files with 35 additions and 2 deletions

View File

@ -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)

View File

@ -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<String> {
return mAuthorityRepository.getWIF(userId, authorityType)
}
internal fun clearDatabase(context: Context) {
val db = BitsyDatabase.getDatabase(context)
viewModelScope.launch {
withContext(IO) {
db?.clearAllTables()
}
}
}
}