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: 'com.android.application'
apply plugin: 'kotlin-android' apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-kapt'
apply plugin: "androidx.navigation.safeargs.kotlin" apply plugin: 'androidx.navigation.safeargs.kotlin'
apply plugin: "kotlin-parcelize" apply plugin: 'kotlin-parcelize'
android { android {
compileSdkVersion 30 compileSdkVersion 30
@ -42,38 +42,38 @@ android {
dependencies { dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"]) 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.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0' 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.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
// Navigation Component // Navigation Component
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.2' implementation 'androidx.navigation:navigation-fragment-ktx:2.3.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.2' implementation 'androidx.navigation:navigation-ui-ktx:2.3.3'
// Room components // Room components
implementation "androidx.room:room-runtime:2.2.5" implementation "androidx.room:room-runtime:2.2.6"
kapt "androidx.room:room-compiler:2.2.5" kapt "androidx.room:room-compiler:2.2.6"
implementation "androidx.room:room-ktx:2.2.5" implementation "androidx.room:room-ktx:2.2.6"
androidTestImplementation "androidx.room:room-testing:2.2.5" androidTestImplementation "androidx.room:room-testing:2.2.6"
// Lifecycle components // Lifecycle components
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0" implementation "androidx.lifecycle:lifecycle-common-java8:2.3.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0"
// Kotlin components // Kotlin components
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.21" api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7" api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9"
// DataBinding // DataBinding
kapt "com.android.databinding:compiler:3.2.0-alpha10" 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 // RecyclerView Animator
implementation 'jp.wasabeef:recyclerview-animators:3.0.0' 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( private val toDoDao = ToDoDatabase.getDatabase(
application application
).toDoDao() ).toDoDao()
private val repository: ToDoRepository private val repository: ToDoRepository = ToDoRepository(toDoDao)
val getAllData: LiveData<List<ToDoData>> val getAllData: LiveData<List<ToDoData>> = repository.getAllData
val sortByHighPriority: LiveData<List<ToDoData>> val sortByHighPriority: LiveData<List<ToDoData>> = repository.sortByHighPriority
val sortByLowPriority: LiveData<List<ToDoData>> val sortByLowPriority: LiveData<List<ToDoData>> = repository.sortByLowPriority
init {
repository = ToDoRepository(toDoDao)
getAllData = repository.getAllData
sortByHighPriority = repository.sortByHighPriority
sortByLowPriority = repository.sortByLowPriority
}
fun insertData(toDoData: ToDoData) { fun insertData(toDoData: ToDoData) {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {

View File

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

View File

@ -3,6 +3,9 @@ package com.example.todoapp.utils
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
fun hideKeyboard(activity: Activity) { fun hideKeyboard(activity: Activity) {
val inputMethodManager = val inputMethodManager =
@ -14,4 +17,13 @@ fun hideKeyboard(activity: Activity) {
currentFocusedView.windowToken, InputMethodManager.HIDE_NOT_ALWAYS currentFocusedView.windowToken, InputMethodManager.HIDE_NOT_ALWAYS
) )
} }
}
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() jcenter()
} }
dependencies { 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 "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.2" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.3"
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files
} }