From 6981b490bdea869923434dfd882381040f26e81d Mon Sep 17 00:00:00 2001 From: "Adam M. Szalkowski" Date: Sat, 13 Apr 2024 13:45:37 +0200 Subject: [PATCH] feat: add loading indicator fragment --- .../activitylauncher/MainActivity.kt | 7 ++-- .../activitylauncher/ui/LoadingFragment.kt | 41 +++++++++++++++++++ .../ui/PackageListFragment.kt | 18 ++++++++ app/src/main/res/layout/fragment_loading.xml | 29 +++++++++++++ app/src/main/res/navigation/nav_graph.xml | 24 ++++++++--- 5 files changed, 111 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/de/szalkowski/activitylauncher/ui/LoadingFragment.kt create mode 100644 app/src/main/res/layout/fragment_loading.xml diff --git a/app/src/main/java/de/szalkowski/activitylauncher/MainActivity.kt b/app/src/main/java/de/szalkowski/activitylauncher/MainActivity.kt index f771ff51..bf1d56aa 100644 --- a/app/src/main/java/de/szalkowski/activitylauncher/MainActivity.kt +++ b/app/src/main/java/de/szalkowski/activitylauncher/MainActivity.kt @@ -18,7 +18,7 @@ import de.szalkowski.activitylauncher.ui.DisclaimerDialogFragment import javax.inject.Inject @AndroidEntryPoint -class MainActivity() : AppCompatActivity(), ActionBarSearch { +class MainActivity : AppCompatActivity(), ActionBarSearch { private lateinit var appBarConfiguration: AppBarConfiguration private lateinit var binding: ActivityMainBinding @@ -43,12 +43,13 @@ class MainActivity() : AppCompatActivity(), ActionBarSearch { setupActionBarWithNavController(navController, appBarConfiguration) } - override var onActionBarSearchListener: ((String) -> Unit)? = null private var actionBarSearchView: SearchView? = null override var actionBarSearchText: String get() = actionBarSearchView?.query?.toString().orEmpty() - set(value) { actionBarSearchView?.setQuery(value, false) } + set(value) { + actionBarSearchView?.setQuery(value, false) + } override fun onCreateOptionsMenu(menu: Menu): Boolean { // Inflate the menu; this adds items to the action bar if it is present. diff --git a/app/src/main/java/de/szalkowski/activitylauncher/ui/LoadingFragment.kt b/app/src/main/java/de/szalkowski/activitylauncher/ui/LoadingFragment.kt new file mode 100644 index 00000000..ac3b4919 --- /dev/null +++ b/app/src/main/java/de/szalkowski/activitylauncher/ui/LoadingFragment.kt @@ -0,0 +1,41 @@ +package de.szalkowski.activitylauncher.ui + +import android.annotation.SuppressLint +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.navigation.fragment.findNavController +import dagger.hilt.android.AndroidEntryPoint +import de.szalkowski.activitylauncher.R +import de.szalkowski.activitylauncher.services.PackageListService +import javax.inject.Inject +import javax.inject.Provider +import kotlin.concurrent.thread + +@AndroidEntryPoint +class LoadingFragment : Fragment() { + @Inject + internal lateinit var packageListService: Provider + + @SuppressLint("RestrictedApi") + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? + ): View? { + val context = this.requireActivity() + + thread { + // preload package list + packageListService.get() + + context.runOnUiThread { + val action = LoadingFragmentDirections.actionLoadingFinished() + findNavController().navigate(action) + } + } + + // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_loading, container, false) + } +} \ No newline at end of file diff --git a/app/src/main/java/de/szalkowski/activitylauncher/ui/PackageListFragment.kt b/app/src/main/java/de/szalkowski/activitylauncher/ui/PackageListFragment.kt index 29104457..b67729a4 100644 --- a/app/src/main/java/de/szalkowski/activitylauncher/ui/PackageListFragment.kt +++ b/app/src/main/java/de/szalkowski/activitylauncher/ui/PackageListFragment.kt @@ -4,9 +4,13 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import androidx.navigation.fragment.findNavController +import androidx.navigation.ui.AppBarConfiguration +import androidx.navigation.ui.NavigationUI.setupActionBarWithNavController import dagger.hilt.android.AndroidEntryPoint +import de.szalkowski.activitylauncher.R import de.szalkowski.activitylauncher.databinding.FragmentPackageListBinding import javax.inject.Inject @@ -21,6 +25,20 @@ class PackageListFragment : Fragment() { // onDestroyView. private val binding get() = _binding!! + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + // disable back button + val appBarConfiguration = AppBarConfiguration.Builder( + R.id.PackageListFragment + ).build() + setupActionBarWithNavController( + this.requireActivity() as AppCompatActivity, + findNavController(), + appBarConfiguration + ) + } + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { diff --git a/app/src/main/res/layout/fragment_loading.xml b/app/src/main/res/layout/fragment_loading.xml new file mode 100644 index 00000000..3432cbf5 --- /dev/null +++ b/app/src/main/res/layout/fragment_loading.xml @@ -0,0 +1,29 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/navigation/nav_graph.xml b/app/src/main/res/navigation/nav_graph.xml index adfe0de6..298533e7 100644 --- a/app/src/main/res/navigation/nav_graph.xml +++ b/app/src/main/res/navigation/nav_graph.xml @@ -3,8 +3,24 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/nav_graph" - app:startDestination="@id/PackageListFragment"> + app:startDestination="@id/LoadingFragment"> + + + + + app:popExitAnim="@anim/slide_out_right" /> + app:popExitAnim="@anim/slide_out_right" />