diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 2c7e034..3a6d748 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -14,7 +14,6 @@ - diff --git a/app/build.gradle b/app/build.gradle index 0a7b11c..dc6b058 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,13 +12,13 @@ plugins { } android { - compileSdkVersion 31 + compileSdkVersion 32 buildToolsVersion "30.0.3" defaultConfig { applicationId "com.example.todoapp" minSdkVersion 26 - targetSdkVersion 31 + targetSdkVersion 32 versionCode 1 versionName "1.0" @@ -57,17 +57,17 @@ android { dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) - implementation 'androidx.core:core-ktx:1.7.0' - implementation 'androidx.appcompat:appcompat:1.4.1' - implementation 'androidx.constraintlayout:constraintlayout:2.1.3' + implementation 'androidx.core:core-ktx:1.8.0' + implementation 'androidx.appcompat:appcompat:1.4.2' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.legacy:legacy-support-v4:1.0.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' // Navigation Component - implementation 'androidx.navigation:navigation-fragment-ktx:2.4.1' - implementation 'androidx.navigation:navigation-ui-ktx:2.4.1' + implementation 'androidx.navigation:navigation-fragment-ktx:2.5.0' + implementation 'androidx.navigation:navigation-ui-ktx:2.5.0' // Room components implementation "androidx.room:room-runtime:2.4.2" @@ -77,11 +77,11 @@ dependencies { // Lifecycle components implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" - implementation "androidx.lifecycle:lifecycle-common-java8:2.4.1" - implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1" + implementation "androidx.lifecycle:lifecycle-common-java8:2.5.0" + implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.0" // Kotlin components api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0" - api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0" + api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1" } \ No newline at end of file diff --git a/app/src/main/java/com/example/todoapp/fragments/add/AddFragment.kt b/app/src/main/java/com/example/todoapp/fragments/add/AddFragment.kt index ebc1811..647797c 100644 --- a/app/src/main/java/com/example/todoapp/fragments/add/AddFragment.kt +++ b/app/src/main/java/com/example/todoapp/fragments/add/AddFragment.kt @@ -3,6 +3,8 @@ package com.example.todoapp.fragments.add import android.os.Bundle import android.view.* import android.widget.Toast +import androidx.core.view.MenuHost +import androidx.core.view.MenuProvider import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.navigation.fragment.findNavController @@ -27,24 +29,27 @@ class AddFragment : Fragment() { // Inflate the layout for this fragment _binding = FragmentAddBinding.inflate(layoutInflater, container, false) - // Set Menu - setHasOptionsMenu(true) - // Spinner Item Selected Listener binding.prioritiesSpinner.onItemSelectedListener = mSharedViewModel.listener return binding.root } - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - inflater.inflate(R.menu.add_fragment_menu, menu) - } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + val menuHost: MenuHost = requireActivity() + menuHost.addMenuProvider(object : MenuProvider { + override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { + menuInflater.inflate(R.menu.add_fragment_menu, menu) + } - override fun onOptionsItemSelected(item: MenuItem): Boolean { - if(item.itemId == R.id.menu_add){ - insertDataToDb() - } - return super.onOptionsItemSelected(item) + override fun onMenuItemSelected(menuItem: MenuItem): Boolean { + if (menuItem.itemId == R.id.menu_add) { + insertDataToDb() + } + return true + } + }) } private fun insertDataToDb() { @@ -53,7 +58,7 @@ class AddFragment : Fragment() { val mDescription = binding.descriptionEt.text.toString() val validation = mSharedViewModel.verifyDataFromUser(mTitle, mDescription) - if(validation){ + if (validation) { // Insert Data to Database val newData = ToDoData( 0, @@ -65,8 +70,9 @@ class AddFragment : Fragment() { Toast.makeText(requireContext(), "Successfully added!", Toast.LENGTH_SHORT).show() // Navigate Back findNavController().navigate(R.id.action_addFragment_to_listFragment) - }else{ - Toast.makeText(requireContext(), "Please fill out all fields.", Toast.LENGTH_SHORT).show() + } else { + Toast.makeText(requireContext(), "Please fill out all fields.", Toast.LENGTH_SHORT) + .show() } } 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 52c1650..9bf3d58 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 @@ -6,10 +6,11 @@ import android.util.Log import android.view.* import android.widget.Toast import androidx.appcompat.widget.SearchView +import androidx.core.view.MenuHost +import androidx.core.view.MenuProvider import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.recyclerview.widget.ItemTouchHelper -import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.StaggeredGridLayoutManager import com.example.todoapp.R @@ -45,14 +46,11 @@ class ListFragment : Fragment(), SearchView.OnQueryTextListener { setupRecyclerview() // Observe LiveData - mToDoViewModel.getAllData.observe(viewLifecycleOwner, { data -> + mToDoViewModel.getAllData.observe(viewLifecycleOwner) { data -> mSharedViewModel.checkIfDatabaseEmpty(data) adapter.setData(data) binding.recyclerView.scheduleLayoutAnimation() - }) - - // Set Menu - setHasOptionsMenu(true) + } // Hide soft keyboard hideKeyboard(requireActivity()) @@ -60,10 +58,42 @@ class ListFragment : Fragment(), SearchView.OnQueryTextListener { return binding.root } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + val menuHost: MenuHost = requireActivity() + menuHost.addMenuProvider(object : MenuProvider { + override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { + menuInflater.inflate(R.menu.list_fragment_menu, menu) + + val search = menu.findItem(R.id.menu_search) + val searchView = search.actionView as? SearchView + searchView?.isSubmitButtonEnabled = true + searchView?.setOnQueryTextListener(this@ListFragment) + } + + override fun onMenuItemSelected(menuItem: MenuItem): Boolean { + when (menuItem.itemId) { + R.id.menu_delete_all -> confirmRemoval() + 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 true + } + }) + + } + private fun setupRecyclerview() { val recyclerView = binding.recyclerView recyclerView.adapter = adapter - recyclerView.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL) + recyclerView.layoutManager = + StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL) // Swipe to Delete swipeToDelete(recyclerView) @@ -95,24 +125,6 @@ class ListFragment : Fragment(), SearchView.OnQueryTextListener { snackBar.show() } - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - inflater.inflate(R.menu.list_fragment_menu, menu) - - val search = menu.findItem(R.id.menu_search) - val searchView = search.actionView as? SearchView - searchView?.isSubmitButtonEnabled = true - searchView?.setOnQueryTextListener(this) - } - - override fun onOptionsItemSelected(item: MenuItem): Boolean { - when (item.itemId) { - R.id.menu_delete_all -> confirmRemoval() - 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) - } - override fun onQueryTextSubmit(query: String?): Boolean { if (query != null) { searchThroughDatabase(query) @@ -130,12 +142,12 @@ class ListFragment : Fragment(), SearchView.OnQueryTextListener { private fun searchThroughDatabase(query: String) { val searchQuery = "%$query%" - mToDoViewModel.searchDatabase(searchQuery).observeOnce(viewLifecycleOwner, { list -> + mToDoViewModel.searchDatabase(searchQuery).observeOnce(viewLifecycleOwner) { list -> list?.let { Log.d("ListFragment", "searchThroughDatabase") adapter.setData(it) } - }) + } } // Show AlertDialog to Confirm Removal of All Items from Database Table diff --git a/app/src/main/java/com/example/todoapp/fragments/update/UpdateFragment.kt b/app/src/main/java/com/example/todoapp/fragments/update/UpdateFragment.kt index 44fec0f..05c5525 100644 --- a/app/src/main/java/com/example/todoapp/fragments/update/UpdateFragment.kt +++ b/app/src/main/java/com/example/todoapp/fragments/update/UpdateFragment.kt @@ -4,6 +4,8 @@ import android.app.AlertDialog import android.os.Bundle import android.view.* import android.widget.Toast +import androidx.core.view.MenuHost +import androidx.core.view.MenuProvider import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.navigation.fragment.findNavController @@ -32,25 +34,28 @@ class UpdateFragment : Fragment() { _binding = FragmentUpdateBinding.inflate(inflater, container, false) binding.args = args - // Set Menu - setHasOptionsMenu(true) - // Spinner Item Selected Listener binding.currentPrioritiesSpinner.onItemSelectedListener = mSharedViewModel.listener return binding.root } - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - inflater.inflate(R.menu.update_fragment_menu, menu) - } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + val menuHost: MenuHost = requireActivity() + menuHost.addMenuProvider(object: MenuProvider { + override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { + menuInflater.inflate(R.menu.update_fragment_menu, menu) + } - override fun onOptionsItemSelected(item: MenuItem): Boolean { - when (item.itemId) { - R.id.menu_save -> updateItem() - R.id.menu_delete -> confirmItemRemoval() - } - return super.onOptionsItemSelected(item) + override fun onMenuItemSelected(menuItem: MenuItem): Boolean { + when (menuItem.itemId) { + R.id.menu_save -> updateItem() + R.id.menu_delete -> confirmItemRemoval() + } + return true + } + }) } private fun updateItem() { diff --git a/build.gradle b/build.gradle index 064fbcc..6e1be2a 100644 --- a/build.gradle +++ b/build.gradle @@ -6,10 +6,10 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:7.1.2' + classpath 'com.android.tools.build:gradle:7.2.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10" - classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.4.1" + classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.5.0" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 827a7f2..52bac51 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip