diff --git a/app/src/main/java/io/timelimit/android/ui/fragment/SingleFragmentWrapper.kt b/app/src/main/java/io/timelimit/android/ui/fragment/SingleFragmentWrapper.kt new file mode 100644 index 0000000..f3e4652 --- /dev/null +++ b/app/src/main/java/io/timelimit/android/ui/fragment/SingleFragmentWrapper.kt @@ -0,0 +1,65 @@ +/* + * TimeLimit Copyright 2019 - 2020 Jonas Lochmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.timelimit.android.ui.fragment + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.navigation.NavController +import androidx.navigation.Navigation +import io.timelimit.android.R +import io.timelimit.android.livedata.liveDataFromValue +import io.timelimit.android.ui.main.ActivityViewModelHolder +import io.timelimit.android.ui.main.AuthenticationFab +import kotlinx.android.synthetic.main.fragment_main.* + +abstract class SingleFragmentWrapper: Fragment() { + private val activity: ActivityViewModelHolder by lazy { getActivity() as ActivityViewModelHolder } + private lateinit var navController: NavController + + protected val navigation get() = navController + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + navController = Navigation.findNavController(container!!) + + return inflater.inflate(R.layout.single_fragment_wrapper, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + AuthenticationFab.manageAuthenticationFab( + fab = fab, + fragment = this, + shouldHighlight = activity.getActivityViewModel().shouldHighlightAuthenticationButton, + authenticatedUser = activity.getActivityViewModel().authenticatedUser, + doesSupportAuth = liveDataFromValue(showAuthButton) + ) + + fab.setOnClickListener { activity.showAuthenticationScreen() } + + if (savedInstanceState == null) { + childFragmentManager.beginTransaction() + .replace(R.id.container, createChildFragment()) + .commit() + } + } + + abstract fun createChildFragment(): Fragment + abstract val showAuthButton: Boolean +} \ No newline at end of file diff --git a/app/src/main/java/io/timelimit/android/ui/fragment/SingleFragmentWrappers.kt b/app/src/main/java/io/timelimit/android/ui/fragment/SingleFragmentWrappers.kt new file mode 100644 index 0000000..5f0b743 --- /dev/null +++ b/app/src/main/java/io/timelimit/android/ui/fragment/SingleFragmentWrappers.kt @@ -0,0 +1,49 @@ +/* + * TimeLimit Copyright 2019 - 2020 Jonas Lochmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package io.timelimit.android.ui.fragment + +import androidx.fragment.app.Fragment +import io.timelimit.android.R +import io.timelimit.android.extensions.safeNavigate +import io.timelimit.android.ui.overview.about.AboutFragment +import io.timelimit.android.ui.overview.about.AboutFragmentParentHandlers + +class AboutFragmentWrapped: SingleFragmentWrapper(), AboutFragmentParentHandlers { + override val showAuthButton: Boolean = false + override fun createChildFragment(): Fragment = AboutFragment() + + override fun onShowDiagnoseScreen() { + navigation.safeNavigate( + AboutFragmentWrappedDirections.actionAboutFragmentWrappedToDiagnoseMainFragment(), + R.id.aboutFragmentWrapped + ) + } + + override fun onShowPurchaseScreen() { + navigation.safeNavigate( + AboutFragmentWrappedDirections.actionAboutFragmentWrappedToPurchaseFragment(), + R.id.aboutFragmentWrapped + ) + } + + override fun onShowStayAwesomeScreen() { + navigation.safeNavigate( + AboutFragmentWrappedDirections.actionAboutFragmentWrappedToStayAwesomeFragment(), + R.id.aboutFragmentWrapped + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/io/timelimit/android/ui/overview/main/MainFragment.kt b/app/src/main/java/io/timelimit/android/ui/overview/main/MainFragment.kt index c0a9334..134b40b 100644 --- a/app/src/main/java/io/timelimit/android/ui/overview/main/MainFragment.kt +++ b/app/src/main/java/io/timelimit/android/ui/overview/main/MainFragment.kt @@ -16,9 +16,7 @@ package io.timelimit.android.ui.overview.main import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup +import android.view.* import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentTransaction import androidx.lifecycle.LiveData @@ -60,6 +58,12 @@ class MainFragment : Fragment(), OverviewFragmentParentHandlers, AboutFragmentPa private var wereViewsCreated = false private var didRedirectToUserScreen = false + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + setHasOptionsMenu(true) + } + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { navigation = Navigation.findNavController(container!!) @@ -231,4 +235,22 @@ class MainFragment : Fragment(), OverviewFragmentParentHandlers, AboutFragmentPa } override fun getCustomTitle(): LiveData = liveDataFromValue("${getString(R.string.main_tab_overview)} (${getString(R.string.app_name)})") + + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + super.onCreateOptionsMenu(menu, inflater) + + inflater.inflate(R.menu.fragment_main_menu, menu) + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) { + R.id.menu_main_about -> { + navigation.safeNavigate( + MainFragmentDirections.actionOverviewFragmentToAboutFragmentWrapped(), + R.id.overviewFragment + ) + + true + } + else -> super.onOptionsItemSelected(item) + } } diff --git a/app/src/main/res/layout/single_fragment_wrapper.xml b/app/src/main/res/layout/single_fragment_wrapper.xml new file mode 100644 index 0000000..1160dab --- /dev/null +++ b/app/src/main/res/layout/single_fragment_wrapper.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + diff --git a/app/src/main/res/menu/fragment_main_menu.xml b/app/src/main/res/menu/fragment_main_menu.xml new file mode 100644 index 0000000..0f6bacd --- /dev/null +++ b/app/src/main/res/menu/fragment_main_menu.xml @@ -0,0 +1,21 @@ + + + + + \ 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 9a7ac8e..0a99494 100644 --- a/app/src/main/res/navigation/nav_graph.xml +++ b/app/src/main/res/navigation/nav_graph.xml @@ -90,6 +90,13 @@ + + + + + +