Changed habit type selection to dropdown

pull/498/head
Thomas Saedt 6 years ago
parent a3bfc05068
commit 29950ff28a

@ -33,15 +33,32 @@ class ListHabitsMenu @Inject constructor(
private val preferences: Preferences, private val preferences: Preferences,
private val themeSwitcher: ThemeSwitcher, private val themeSwitcher: ThemeSwitcher,
private val behavior: ListHabitsMenuBehavior private val behavior: ListHabitsMenuBehavior
) : BaseMenu(activity) { ) : BaseMenu(activity), Preferences.Listener {
private lateinit var topBarMenu: Menu
override fun onCreate(menu: Menu) { override fun onCreate(menu: Menu) {
val nightModeItem = menu.findItem(R.id.actionToggleNightMode) val nightModeItem = menu.findItem(R.id.actionToggleNightMode)
val hideArchivedItem = menu.findItem(R.id.actionHideArchived) val hideArchivedItem = menu.findItem(R.id.actionHideArchived)
val hideCompletedItem = menu.findItem(R.id.actionHideCompleted) val hideCompletedItem = menu.findItem(R.id.actionHideCompleted)
nightModeItem.isChecked = themeSwitcher.isNightMode nightModeItem.isChecked = themeSwitcher.isNightMode
hideArchivedItem.isChecked = !preferences.showArchived hideArchivedItem.isChecked = !preferences.showArchived
hideCompletedItem.isChecked = !preferences.showCompleted hideCompletedItem.isChecked = !preferences.showCompleted
topBarMenu = menu
//the habit creation menu should be disabled when numeric habits are also disabled
if (!preferences.isNumericalHabitsFeatureEnabled) {
setCreateHabitMenuEnabled(false, menu)
}
//let the class add itself as listener
preferences.addListener(this)
}
override fun onNumericalHabitsFeatureChanged() {
if(topBarMenu==null){return}
setCreateHabitMenuEnabled(preferences.isNumericalHabitsFeatureEnabled, topBarMenu)
} }
override fun onItemSelected(item: MenuItem): Boolean { override fun onItemSelected(item: MenuItem): Boolean {
@ -107,5 +124,18 @@ class ListHabitsMenu @Inject constructor(
} }
} }
/**
* @param enabled whether the create habit menu should be enabled or disabled
* @param menu a reference to the menu on which should be enabled or disabled
*/
fun setCreateHabitMenuEnabled(enabled: Boolean, menu: Menu) {
val habitCreationMenu = menu.findItem(R.id.actionAdd).subMenu
for (itemIndex: Int in 0 until habitCreationMenu.size()) {
val menuItem = habitCreationMenu.getItem(itemIndex)
menuItem.isEnabled = enabled
menuItem.isVisible = enabled
}
}
override fun getMenuResourceId() = R.menu.list_habits override fun getMenuResourceId() = R.menu.list_habits
} }

@ -89,6 +89,8 @@ class SharedPreferencesStorage
preferences.setNotificationsLed(getBoolean(key, false)) preferences.setNotificationsLed(getBoolean(key, false))
"pref_feature_sync" -> "pref_feature_sync" ->
preferences.isSyncEnabled = getBoolean(key, false) preferences.isSyncEnabled = getBoolean(key, false)
"pref_feature_numerical_habits" ->
preferences.isNumericalHabitsFeatureEnabled = getBoolean(key, false)
} }
sharedPreferences.registerOnSharedPreferenceChangeListener(this) sharedPreferences.registerOnSharedPreferenceChangeListener(this)
} }

@ -18,15 +18,28 @@
--> -->
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"> tools:context=".MainActivity">
<item <item
android:id="@+id/actionAdd" android:id="@+id/actionAdd"
android:icon="?iconAdd" android:icon="?iconAdd"
android:title="@string/add_habit" android:title="@string/add_habit"
app:showAsAction="always"/> app:showAsAction="always">
<menu android:id="@+id/createHabitMenu">
<item
android:id="@+id/actionCreateBooleanHabit"
android:enabled="true"
android:title="@string/yes_or_no" />
<item
android:id="@+id/actionCreateNumeralHabit"
android:enabled="true"
android:title="@string/number" />
</menu>
</item>
<item <item
android:id="@+id/action_filter" android:id="@+id/action_filter"
@ -38,31 +51,31 @@
android:id="@+id/actionHideArchived" android:id="@+id/actionHideArchived"
android:checkable="true" android:checkable="true"
android:enabled="true" android:enabled="true"
android:title="@string/hide_archived"/> android:title="@string/hide_archived" />
<item <item
android:id="@+id/actionHideCompleted" android:id="@+id/actionHideCompleted"
android:checkable="true" android:checkable="true"
android:enabled="true" android:enabled="true"
android:title="@string/hide_completed"/> android:title="@string/hide_completed" />
<item android:title="@string/sort"> <item android:title="@string/sort">
<menu> <menu>
<item <item
android:id="@+id/actionSortManual" android:id="@+id/actionSortManual"
android:title="@string/manually"/> android:title="@string/manually" />
<item <item
android:id="@+id/actionSortName" android:id="@+id/actionSortName"
android:title="@string/by_name"/> android:title="@string/by_name" />
<item <item
android:id="@+id/actionSortColor" android:id="@+id/actionSortColor"
android:title="@string/by_color"/> android:title="@string/by_color" />
<item <item
android:id="@+id/actionSortScore" android:id="@+id/actionSortScore"
android:title="@string/by_score"/> android:title="@string/by_score" />
</menu> </menu>
</item> </item>
</menu> </menu>
@ -74,23 +87,23 @@
android:enabled="true" android:enabled="true"
android:orderInCategory="50" android:orderInCategory="50"
android:title="@string/night_mode" android:title="@string/night_mode"
app:showAsAction="never"/> app:showAsAction="never" />
<item <item
android:id="@+id/actionSettings" android:id="@+id/actionSettings"
android:orderInCategory="100" android:orderInCategory="100"
android:title="@string/action_settings" android:title="@string/action_settings"
app:showAsAction="never"/> app:showAsAction="never" />
<item <item
android:id="@+id/actionFAQ" android:id="@+id/actionFAQ"
android:orderInCategory="100" android:orderInCategory="100"
android:title="@string/help" android:title="@string/help"
app:showAsAction="never"/> app:showAsAction="never" />
<item <item
android:id="@+id/actionAbout" android:id="@+id/actionAbout"
android:orderInCategory="100" android:orderInCategory="100"
android:title="@string/about" android:title="@string/about"
app:showAsAction="never"/> app:showAsAction="never" />
</menu> </menu>

@ -184,6 +184,10 @@
<string name="total">Total</string> <string name="total">Total</string>
<!-- Different types of habits -->
<string name="yes_or_no">Yes or No</string>
<string name="number">Number</string>
<!-- Middle part of the sentence '1 time in xx days' --> <!-- Middle part of the sentence '1 time in xx days' -->
<string name="time_every">time in</string> <string name="time_every">time in</string>

@ -232,6 +232,7 @@ public class Preferences
public void setNumericalHabitsFeatureEnabled(boolean enabled) public void setNumericalHabitsFeatureEnabled(boolean enabled)
{ {
storage.putBoolean("pref_feature_numerical_habits", enabled); storage.putBoolean("pref_feature_numerical_habits", enabled);
for (Listener l : listeners) l.onNumericalHabitsFeatureChanged();
} }
public boolean isPureBlackEnabled() public boolean isPureBlackEnabled()
@ -346,6 +347,10 @@ public class Preferences
default void onSyncFeatureChanged() default void onSyncFeatureChanged()
{ {
} }
default void onNumericalHabitsFeatureChanged()
{
}
} }
public interface Storage public interface Storage

@ -61,9 +61,17 @@ public class ListHabitsMenuBehavior
updateAdapterFilter(); updateAdapterFilter();
} }
/**
* This function is called when a new habit should be created. (A dialog will be created)
* When only a single type of habit type exists (e.g. the numeral habits feature is disabled) a menu is spawned directly.
* If this is not the case however, a dropdown menu is shown.
*/
public void onCreateHabit() public void onCreateHabit()
{ {
screen.showCreateHabitScreen(); if(!preferences.isNumericalHabitsFeatureEnabled())
{
screen.showCreateHabitScreen();
}
} }
public void onViewFAQ() public void onViewFAQ()

Loading…
Cancel
Save