Fix blacklist dialog bug

Fix a problem where the blacklist dialog wouldn't properly detect when there were no changes made to the list of paths.
This commit is contained in:
OxygenCobalt 2021-03-20 16:12:06 -06:00
parent 68887ffb64
commit a55b42a3da
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
2 changed files with 21 additions and 25 deletions

View file

@ -63,7 +63,7 @@ class BlacklistDialog : BottomSheetDialogFragment() {
} }
binding.blacklistConfirm.setOnClickListener { binding.blacklistConfirm.setOnClickListener {
if (blacklistModel.modified) { if (blacklistModel.isModified()) {
saveAndRestart() saveAndRestart()
} else { } else {
dismiss() dismiss()
@ -99,7 +99,6 @@ class BlacklistDialog : BottomSheetDialogFragment() {
folderChooser( folderChooser(
requireContext(), requireContext(),
initialDirectory = File(Environment.getExternalStorageDirectory().absolutePath), initialDirectory = File(Environment.getExternalStorageDirectory().absolutePath),
waitForPositiveButton = false,
emptyTextRes = R.string.error_no_dirs emptyTextRes = R.string.error_no_dirs
) )
@ -135,19 +134,18 @@ class BlacklistDialog : BottomSheetDialogFragment() {
private fun saveAndRestart() { private fun saveAndRestart() {
blacklistModel.save { blacklistModel.save {
playbackModel.savePlaybackState(requireContext(), ::reincarnate) playbackModel.savePlaybackState(requireContext(), ::hardRestart)
} }
} }
private fun reincarnate() { private fun hardRestart() {
// Instead of having to do a ton of cleanup and make a ton of horrible code changes // Instead of having to do a ton of cleanup and horrible code changes
// to restart this application non-destructively, I just restart the UI task [There is only // to restart this application non-destructively, I just restart the UI task [There is only
// one, after all] and then kill the application using exitProcess. Works well enough. // one, after all] and then kill the application using exitProcess. Works well enough.
val intent = Intent(requireContext().applicationContext, MainActivity::class.java).apply { val intent = Intent(requireContext().applicationContext, MainActivity::class.java)
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
}
requireContext().startActivity(intent) startActivity(intent)
exitProcess(0) exitProcess(0)
} }

View file

@ -21,8 +21,7 @@ class BlacklistViewModel(context: Context) : ViewModel() {
val paths: LiveData<MutableList<String>> get() = mPaths val paths: LiveData<MutableList<String>> get() = mPaths
private val blacklistDatabase = BlacklistDatabase.getInstance(context) private val blacklistDatabase = BlacklistDatabase.getInstance(context)
var modified = false private var dbPaths = listOf<String>()
private set
init { init {
loadDatabasePaths() loadDatabasePaths()
@ -33,12 +32,10 @@ class BlacklistViewModel(context: Context) : ViewModel() {
* [save] is called. * [save] is called.
*/ */
fun addPath(path: String) { fun addPath(path: String) {
if (mPaths.value!!.contains(path)) return if (!mPaths.value!!.contains(path)) {
mPaths.value!!.add(path) mPaths.value!!.add(path)
mPaths.value = mPaths.value mPaths.value = mPaths.value
}
modified = true
} }
/** /**
@ -48,8 +45,6 @@ class BlacklistViewModel(context: Context) : ViewModel() {
fun removePath(path: String) { fun removePath(path: String) {
mPaths.value!!.remove(path) mPaths.value!!.remove(path)
mPaths.value = mPaths.value mPaths.value = mPaths.value
modified = true
} }
/** /**
@ -58,7 +53,8 @@ class BlacklistViewModel(context: Context) : ViewModel() {
fun save(onDone: () -> Unit) { fun save(onDone: () -> Unit) {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
blacklistDatabase.writePaths(mPaths.value!!) blacklistDatabase.writePaths(mPaths.value!!)
modified = false dbPaths = mPaths.value!!
onDone() onDone()
} }
} }
@ -68,16 +64,18 @@ class BlacklistViewModel(context: Context) : ViewModel() {
*/ */
fun loadDatabasePaths() { fun loadDatabasePaths() {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
val paths = blacklistDatabase.readPaths().toMutableList() dbPaths = blacklistDatabase.readPaths()
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
// Can only change LiveData on the main thread. mPaths.value = dbPaths.toMutableList()
mPaths.value = paths }
}
} }
modified = false /**
} * Check if changes have been made to the viewmodel's paths.
} */
fun isModified() = dbPaths != paths.value
class Factory(private val context: Context) : ViewModelProvider.Factory { class Factory(private val context: Context) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T { override fun <T : ViewModel?> create(modelClass: Class<T>): T {