From 71161b3c98241132068dd8d2bf01654198eaea71 Mon Sep 17 00:00:00 2001 From: Stefan Date: Sun, 21 Feb 2021 09:12:18 +0100 Subject: [PATCH] Search/Undo Bug Fixed --- app/build.gradle | 32 +++++++++---------- .../todoapp/data/viewmodel/ToDoViewModel.kt | 15 +++------ .../todoapp/fragments/list/ListFragment.kt | 11 ++++--- .../java/com/example/todoapp/utils/Utils.kt | 12 +++++++ build.gradle | 6 ++-- 5 files changed, 41 insertions(+), 35 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 7e508eb..0bd9d45 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/java/com/example/todoapp/data/viewmodel/ToDoViewModel.kt b/app/src/main/java/com/example/todoapp/data/viewmodel/ToDoViewModel.kt index d5edece..e461a24 100644 --- a/app/src/main/java/com/example/todoapp/data/viewmodel/ToDoViewModel.kt +++ b/app/src/main/java/com/example/todoapp/data/viewmodel/ToDoViewModel.kt @@ -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> - val sortByHighPriority: LiveData> - val sortByLowPriority: LiveData> - - init { - repository = ToDoRepository(toDoDao) - getAllData = repository.getAllData - sortByHighPriority = repository.sortByHighPriority - sortByLowPriority = repository.sortByLowPriority - } + val getAllData: LiveData> = repository.getAllData + val sortByHighPriority: LiveData> = repository.sortByHighPriority + val sortByLowPriority: LiveData> = repository.sortByLowPriority fun insertData(toDoData: ToDoData) { viewModelScope.launch(Dispatchers.IO) { diff --git a/app/src/main/java/com/example/todoapp/fragments/list/ListFragment.kt b/app/src/main/java/com/example/todoapp/fragments/list/ListFragment.kt index 7679896..f056e13 100644 --- a/app/src/main/java/com/example/todoapp/fragments/list/ListFragment.kt +++ b/app/src/main/java/com/example/todoapp/fragments/list/ListFragment.kt @@ -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 } - } \ No newline at end of file diff --git a/app/src/main/java/com/example/todoapp/utils/Utils.kt b/app/src/main/java/com/example/todoapp/utils/Utils.kt index ddf284f..9d368db 100644 --- a/app/src/main/java/com/example/todoapp/utils/Utils.kt +++ b/app/src/main/java/com/example/todoapp/utils/Utils.kt @@ -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 = @@ -14,4 +17,13 @@ fun hideKeyboard(activity: Activity) { currentFocusedView.windowToken, InputMethodManager.HIDE_NOT_ALWAYS ) } +} + +fun LiveData.observeOnce(lifecycleOwner: LifecycleOwner, observer: Observer){ + observe(lifecycleOwner, object : Observer{ + override fun onChanged(t: T) { + observer.onChanged(t) + removeObserver(this) + } + }) } \ No newline at end of file diff --git a/build.gradle b/build.gradle index f8ed55d..2c97792 100644 --- a/build.gradle +++ b/build.gradle @@ -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 }