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

View file

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