Remove BaseMenu

pull/699/head
Alinson S. Xavier 5 years ago
parent c15c14ffcd
commit 3523b2c1a3

@ -39,19 +39,8 @@ import org.isoron.androidbase.*
* getExceptionHandler.
*/
abstract class BaseActivity : AppCompatActivity() {
private var baseMenu: BaseMenu? = null
private var screen: BaseScreen? = null
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
if (menu != null) baseMenu?.onCreate(menuInflater, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if (item == null) return false
return baseMenu?.onItemSelected(item) ?: false
}
fun restartWithFade(cls: Class<*>?) {
Handler().postDelayed({
finish()
@ -60,10 +49,6 @@ abstract class BaseActivity : AppCompatActivity() {
}, 500) // HACK: Let the menu disappear first
}
fun setBaseMenu(baseMenu: BaseMenu?) {
this.baseMenu = baseMenu
}
fun setScreen(screen: BaseScreen?) {
this.screen = screen
}

@ -1,81 +0,0 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker 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, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker 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 <http://www.gnu.org/licenses/>.
*/
package org.isoron.androidbase.activities
import android.view.*
import androidx.annotation.*
/**
* Base class for all the menus in the application.
*
* This class receives from BaseActivity all callbacks related to menus, such as
* menu creation and click events. It also handles some implementation details
* of creating menus in Android, such as inflating the resources.
*/
abstract class BaseMenu(private val activity: BaseActivity) {
/**
* Declare that the menu has changed, and should be recreated.
*/
fun invalidate() {
activity.invalidateOptionsMenu()
}
/**
* Called when the menu is first displayed.
*
* The given menu is already inflated and ready to receive items. The
* application should override this method and add items to the menu here.
*
* @param menu the menu that is being created.
*/
open fun onCreate(menu: Menu) {}
/**
* Called when the menu is first displayed.
*
* This method should not be overridden. The application should override
* the methods onCreate(Menu) and getMenuResourceId instead.
*
* @param inflater a menu inflater, for creating the menu
* @param menu the menu that is being created.
*/
fun onCreate(inflater: MenuInflater, menu: Menu) {
menu.clear()
inflater.inflate(getMenuResourceId(), menu)
onCreate(menu)
}
/**
* Called whenever an item on the menu is selected.
*
* @param item the item that was selected.
* @return true if the event was consumed, or false otherwise
*/
open fun onItemSelected(item: MenuItem): Boolean = false
/**
* Returns the id of the resource that should be used to inflate this menu.
*
* @return id of the menu resource.
*/
@MenuRes
protected abstract fun getMenuResourceId(): Int
}

@ -86,19 +86,6 @@ open class BaseScreen(@JvmField protected var activity: BaseActivity) {
*/
open fun reattachDialogs() {}
/**
* Sets the menu to be shown by this screen.
*
*
* This menu will be visible if when there is no active selection operation.
* If the provided menu is null, then no menu will be shown.
*
* @param menu the menu to be shown.
*/
fun setMenu(menu: BaseMenu?) {
activity.setBaseMenu(menu)
}
/**
* Sets the root view for this screen.
*

@ -20,6 +20,7 @@
package org.isoron.uhabits.activities.habits.list
import android.os.*
import android.view.*
import kotlinx.coroutines.*
import org.isoron.uhabits.*
import org.isoron.uhabits.activities.*
@ -43,6 +44,8 @@ class ListHabitsActivity : HabitsActivity() {
lateinit var syncManager: SyncManager
private val scope = CoroutineScope(Dispatchers.Main)
private lateinit var menu: ListHabitsMenu
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
prefs = appComponent.preferences
@ -53,6 +56,7 @@ class ListHabitsActivity : HabitsActivity() {
screen = component.listHabitsScreen
adapter = component.habitCardListAdapter
taskRunner = appComponent.taskRunner
menu = component.listHabitsMenu
setScreen(screen)
component.listHabitsBehavior.onStartup()
@ -85,4 +89,13 @@ class ListHabitsActivity : HabitsActivity() {
}
super.onResume()
}
override fun onCreateOptionsMenu(m: Menu): Boolean {
menu.onCreate(menuInflater, m)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return menu.onItemSelected(item)
}
}

@ -29,23 +29,24 @@ import javax.inject.*
@ActivityScope
class ListHabitsMenu @Inject constructor(
activity: BaseActivity,
private val activity: BaseActivity,
private val preferences: Preferences,
private val themeSwitcher: ThemeSwitcher,
private val behavior: ListHabitsMenuBehavior
) : BaseMenu(activity){
) {
override fun onCreate(menu: Menu) {
fun onCreate(inflater: MenuInflater, menu: Menu) {
menu.clear()
inflater.inflate(R.menu.list_habits, menu)
val nightModeItem = menu.findItem(R.id.actionToggleNightMode)
val hideArchivedItem = menu.findItem(R.id.actionHideArchived)
val hideCompletedItem = menu.findItem(R.id.actionHideCompleted)
nightModeItem.isChecked = themeSwitcher.isNightMode
hideArchivedItem.isChecked = !preferences.showArchived
hideCompletedItem.isChecked = !preferences.showCompleted
}
override fun onItemSelected(item: MenuItem): Boolean {
fun onItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.actionToggleNightMode -> {
behavior.onToggleNightMode()
@ -74,13 +75,13 @@ class ListHabitsMenu @Inject constructor(
R.id.actionHideArchived -> {
behavior.onToggleShowArchived()
invalidate()
activity.invalidateOptionsMenu()
return true
}
R.id.actionHideCompleted -> {
behavior.onToggleShowCompleted()
invalidate()
activity.invalidateOptionsMenu()
return true
}
@ -112,6 +113,4 @@ class ListHabitsMenu @Inject constructor(
else -> return false
}
}
override fun getMenuResourceId() = R.menu.list_habits
}

@ -67,7 +67,6 @@ class ListHabitsScreen
private val colorPickerFactory: ColorPickerDialogFactory,
private val numberPickerFactory: NumberPickerFactory,
private val behavior: Lazy<ListHabitsBehavior>,
private val menu: Lazy<ListHabitsMenu>,
private val selectionMenu: Lazy<ListHabitsSelectionMenu>
) : BaseScreen(activity),
CommandRunner.Listener,
@ -80,7 +79,6 @@ class ListHabitsScreen
}
fun onAttached() {
setMenu(menu.get())
setSelectionMenu(selectionMenu.get())
commandRunner.addListener(this)
if(activity.intent.action == "android.intent.action.VIEW") {

Loading…
Cancel
Save