Search/Undo Bug Fixed

This commit is contained in:
Stefan 2021-02-21 09:12:18 +01:00
parent 80be91e696
commit 71161b3c98
5 changed files with 41 additions and 35 deletions

View File

@ -1,8 +1,8 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: "androidx.navigation.safeargs.kotlin"
apply plugin: "kotlin-parcelize"
apply plugin: 'androidx.navigation.safeargs.kotlin'
apply plugin: 'kotlin-parcelize'
android {
compileSdkVersion 30
@ -42,38 +42,38 @@ android {
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.21"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.30"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
// Navigation Component
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.2'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.3'
// Room components
implementation "androidx.room:room-runtime:2.2.5"
kapt "androidx.room:room-compiler:2.2.5"
implementation "androidx.room:room-ktx:2.2.5"
androidTestImplementation "androidx.room:room-testing:2.2.5"
implementation "androidx.room:room-runtime:2.2.6"
kapt "androidx.room:room-compiler:2.2.6"
implementation "androidx.room:room-ktx:2.2.6"
androidTestImplementation "androidx.room:room-testing:2.2.6"
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-common-java8:2.3.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0"
// Kotlin components
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.21"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2"
// DataBinding
kapt "com.android.databinding:compiler:3.2.0-alpha10"
kapt "androidx.databinding:databinding-common:4.1.1"
kapt "androidx.databinding:databinding-common:4.1.2"
// RecyclerView Animator
implementation 'jp.wasabeef:recyclerview-animators:3.0.0'

View File

@ -15,18 +15,11 @@ class ToDoViewModel(application: Application) : AndroidViewModel(application) {
private val toDoDao = ToDoDatabase.getDatabase(
application
).toDoDao()
private val repository: ToDoRepository
private val repository: ToDoRepository = ToDoRepository(toDoDao)
val getAllData: LiveData<List<ToDoData>>
val sortByHighPriority: LiveData<List<ToDoData>>
val sortByLowPriority: LiveData<List<ToDoData>>
init {
repository = ToDoRepository(toDoDao)
getAllData = repository.getAllData
sortByHighPriority = repository.sortByHighPriority
sortByLowPriority = repository.sortByLowPriority
}
val getAllData: LiveData<List<ToDoData>> = repository.getAllData
val sortByHighPriority: LiveData<List<ToDoData>> = repository.sortByHighPriority
val sortByLowPriority: LiveData<List<ToDoData>> = repository.sortByLowPriority
fun insertData(toDoData: ToDoData) {
viewModelScope.launch(Dispatchers.IO) {

View File

@ -2,12 +2,12 @@ package com.example.todoapp.fragments.list
import android.app.AlertDialog
import android.os.Bundle
import android.util.Log
import android.view.*
import androidx.appcompat.widget.SearchView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.*
import com.example.todoapp.R
import com.example.todoapp.data.models.ToDoData
@ -16,6 +16,7 @@ import com.example.todoapp.databinding.FragmentListBinding
import com.example.todoapp.fragments.SharedViewModel
import com.example.todoapp.fragments.list.adapter.ListAdapter
import com.example.todoapp.utils.hideKeyboard
import com.example.todoapp.utils.observeOnce
import com.google.android.material.snackbar.Snackbar
import jp.wasabeef.recyclerview.animators.SlideInUpAnimator
@ -106,8 +107,8 @@ class ListFragment : Fragment(), SearchView.OnQueryTextListener {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_delete_all -> confirmRemoval()
R.id.menu_priority_high -> mToDoViewModel.sortByHighPriority.observe(this, { adapter.setData(it) })
R.id.menu_priority_low -> mToDoViewModel.sortByLowPriority.observe(this, { adapter.setData(it) })
R.id.menu_priority_high -> mToDoViewModel.sortByHighPriority.observe(viewLifecycleOwner, { adapter.setData(it) })
R.id.menu_priority_low -> mToDoViewModel.sortByLowPriority.observe(viewLifecycleOwner, { adapter.setData(it) })
}
return super.onOptionsItemSelected(item)
}
@ -129,8 +130,9 @@ class ListFragment : Fragment(), SearchView.OnQueryTextListener {
private fun searchThroughDatabase(query: String) {
val searchQuery = "%$query%"
mToDoViewModel.searchDatabase(searchQuery).observe(this, { list ->
mToDoViewModel.searchDatabase(searchQuery).observeOnce(viewLifecycleOwner, { list ->
list?.let {
Log.d("ListFragment", "searchThroughDatabase")
adapter.setData(it)
}
})
@ -157,5 +159,4 @@ class ListFragment : Fragment(), SearchView.OnQueryTextListener {
super.onDestroyView()
_binding = null
}
}

View File

@ -3,6 +3,9 @@ package com.example.todoapp.utils
import android.app.Activity
import android.content.Context
import android.view.inputmethod.InputMethodManager
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
fun hideKeyboard(activity: Activity) {
val inputMethodManager =
@ -15,3 +18,12 @@ fun hideKeyboard(activity: Activity) {
)
}
}
fun <T> LiveData<T>.observeOnce(lifecycleOwner: LifecycleOwner, observer: Observer<T>){
observe(lifecycleOwner, object : Observer<T>{
override fun onChanged(t: T) {
observer.onChanged(t)
removeObserver(this)
}
})
}

View File

@ -6,10 +6,10 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.1'
classpath 'com.android.tools.build:gradle:4.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.21"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.3"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}