Merge pull request #89 from lacerta-doc/feature/switch

Feature/switch
This commit is contained in:
ろむねこ 2024-01-22 01:12:16 +09:00 committed by GitHub
commit 95d808dc75
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 140 additions and 8 deletions

View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">

View File

@ -78,4 +78,8 @@ dependencies {
implementation project(':data') implementation project(':data')
implementation project(':shared:ui') implementation project(':shared:ui')
implementation project(':model')
implementation project(':utils')
} }

View File

@ -10,31 +10,45 @@ import androidx.navigation.ui.NavigationUI;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast; import android.widget.Toast;
import one.nem.lacerta.model.pref.FeatureSwitchOverride;
import one.nem.lacerta.utils.FeatureSwitch;
import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.bottomnavigation.BottomNavigationView;
import java.io.NotActiveException;
import dagger.hilt.android.AndroidEntryPoint; import dagger.hilt.android.AndroidEntryPoint;
import one.nem.lacerta.utils.repository.SharedPrefUtils;
import javax.inject.Inject; import javax.inject.Inject;
@AndroidEntryPoint @AndroidEntryPoint
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
@Inject
SharedPrefUtils sharedPrefUtils;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav);
// Initialize app
if (sharedPrefUtils.getIsFirstLaunch()) initializeApp();
// Init navigation // Init navigation
try { try {
FragmentManager supportFragmentManager = getSupportFragmentManager(); FragmentManager supportFragmentManager = getSupportFragmentManager();
NavHostFragment navHostFragment = (NavHostFragment) supportFragmentManager.findFragmentById(R.id.nav_host_fragment); NavHostFragment navHostFragment = (NavHostFragment) supportFragmentManager.findFragmentById(R.id.nav_host_fragment);
assert navHostFragment != null; assert navHostFragment != null;
NavController navController = navHostFragment.getNavController(); NavController navController = navHostFragment.getNavController();
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav);
NavigationUI.setupWithNavController(bottomNavigationView, navController); NavigationUI.setupWithNavController(bottomNavigationView, navController);
} }
catch (Exception e) { catch (Exception e) {
@ -44,10 +58,30 @@ public class MainActivity extends AppCompatActivity {
finish(); // Exit app finish(); // Exit app
} }
// Apply feature switch
applyFeatureSwitch(bottomNavigationView, FeatureSwitchOverride.ENABLE_SEARCH, FeatureSwitch.FeatureMaster.enableSearch, one.nem.lacerta.feature.search.R.id.feature_search_navigation);
applyFeatureSwitch(bottomNavigationView, FeatureSwitchOverride.ENABLE_DEBUG_MENU, FeatureSwitch.FeatureMaster.enableDebugMenu, one.nem.lacerta.feature.debug.R.id.feature_debug_navigation);
// Set navigation bar color // Set navigation bar color
getWindow().setNavigationBarColor(ContextCompat.getColor(this, one.nem.lacerta.shared.ui.R.color.colorSecondaryContainer)); getWindow().setNavigationBarColor(ContextCompat.getColor(this, one.nem.lacerta.shared.ui.R.color.colorSecondaryContainer));
// Set status bar color // Set status bar color
getWindow().setStatusBarColor(ContextCompat.getColor(this, one.nem.lacerta.shared.ui.R.color.colorSurface)); getWindow().setStatusBarColor(ContextCompat.getColor(this, one.nem.lacerta.shared.ui.R.color.colorSurface));
} }
private void initializeApp() {
Log.d("Init", "Initializing app");
// Set feature switch override to default value
sharedPrefUtils.setFeatureSwitchOverride(FeatureSwitchOverride.ENABLE_SEARCH, FeatureSwitch.FeatureMaster.enableSearch);
sharedPrefUtils.setFeatureSwitchOverride(FeatureSwitchOverride.ENABLE_DEBUG_MENU, FeatureSwitch.FeatureMaster.enableDebugMenu);
// Set isFirstLaunch to false
sharedPrefUtils.setIsFirstLaunch(false);
}
private void applyFeatureSwitch(BottomNavigationView bottomNavigationView, FeatureSwitchOverride featureSwitchOverride, boolean defaultValue, int menuId) {
boolean isEnabled = FeatureSwitch.Meta.canOverrideSwitch ? sharedPrefUtils.getFeatureSwitchOverride(featureSwitchOverride) : defaultValue;
if (!isEnabled) bottomNavigationView.getMenu().removeItem(menuId);
}
} }

View File

@ -18,7 +18,7 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/main_nav" app:navGraph="@navigation/main_nav"
tools:layout="@layout/fragment_debug_menu_container" /> tools:layout="@layout/fragment_home_top" />
<com.google.android.material.bottomnavigation.BottomNavigationView <com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav" android:id="@+id/bottom_nav"
@ -31,7 +31,6 @@
app:labelVisibilityMode="selected" app:labelVisibilityMode="selected"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent" />
app:menu="@menu/bottom_nav_menu"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -19,7 +19,7 @@
<item <item
android:id="@id/feature_debug_navigation" android:id="@id/feature_debug_navigation"
android:icon="@drawable/developer_mode_24px" android:icon="@drawable/developer_mode_24px"
android:title="@string/debug_menu_title"/> android:title="@string/debug_menu_title" />
<item <item
android:id="@id/feature_setting_navigation" android:id="@id/feature_setting_navigation"

View File

@ -2,7 +2,7 @@
<navigation xmlns:android="http://schemas.android.com/apk/res/android" <navigation 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"
android:id="@+id/app_main_nav" android:id="@+id/app_main_nav"
app:startDestination="@id/feature_debug_navigation"> app:startDestination="@id/feature_home_navigation">
<include app:graph="@navigation/feature_debug_navigation" /> <include app:graph="@navigation/feature_debug_navigation" />

View File

@ -0,0 +1,30 @@
package one.nem.lacerta.model.pref;
public enum FeatureSwitchOverride {
ENABLE_DEBUG_MENU("enableDebugMenu"),
ENABLE_SEARCH("enableSearch"),
SHOW_DISPLAY_MENU("showDisplayMenu"),
SHOW_DATA_MENU("showDataMenu"),
SHOW_SCAN_MENU("showScanMenu");
private final String key;
FeatureSwitchOverride(String key) {
this.key = key;
}
public String getKey() {
return key;
}
public static FeatureSwitchOverride fromKey(String key) {
for (FeatureSwitchOverride value : values()) {
if (value.getKey().equals(key)) {
return value;
}
}
return null;
}
}

View File

@ -0,0 +1,19 @@
package one.nem.lacerta.utils;
public class FeatureSwitch {
public static class Meta {
public static boolean canOverrideSwitch = true;
}
public static class FeatureMaster {
public static boolean enableSearch = true;
public static boolean enableDebugMenu = false;
}
public static class Setting {
public static boolean showDisplayMenu = false;
public static boolean showDataMenu = false;
public static boolean showScanMenu = false;
}
}

View File

@ -4,13 +4,17 @@ package one.nem.lacerta.utils.impl;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import javax.inject.Inject;
import dagger.hilt.android.qualifiers.ApplicationContext; import dagger.hilt.android.qualifiers.ApplicationContext;
import one.nem.lacerta.model.pref.FeatureSwitchOverride;
import one.nem.lacerta.utils.repository.SharedPrefUtils; import one.nem.lacerta.utils.repository.SharedPrefUtils;
public class SharedPrefUtilsImpl implements SharedPrefUtils{ public class SharedPrefUtilsImpl implements SharedPrefUtils{
private final Context applicationContext; private final Context applicationContext;
@Inject
public SharedPrefUtilsImpl(@ApplicationContext Context applicationContext) { public SharedPrefUtilsImpl(@ApplicationContext Context applicationContext) {
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
} }
@ -26,4 +30,34 @@ public class SharedPrefUtilsImpl implements SharedPrefUtils{
// Editorの取得 // Editorの取得
return applicationContext.getSharedPreferences(name, Context.MODE_PRIVATE).edit(); return applicationContext.getSharedPreferences(name, Context.MODE_PRIVATE).edit();
} }
@Override
public SharedPreferences getSharedPreferences(String name) {
return applicationContext.getSharedPreferences(name, Context.MODE_PRIVATE);
}
@Override
public SharedPreferences getSharedPreferences() {
return applicationContext.getSharedPreferences("common", Context.MODE_PRIVATE); // TODO-rca: 決め打ちやめる?
}
@Override
public boolean getFeatureSwitchOverride(FeatureSwitchOverride featureSwitchOverride) {
return getSharedPreferences().getBoolean(featureSwitchOverride.getKey(), false);
}
@Override
public void setFeatureSwitchOverride(FeatureSwitchOverride featureSwitchOverride, boolean value) {
getEditor().putBoolean(featureSwitchOverride.getKey(), value).apply();
}
@Override
public boolean getIsFirstLaunch() {
return getSharedPreferences().getBoolean("isFirstLaunch", true);
}
@Override
public void setIsFirstLaunch(boolean value) {
getEditor().putBoolean("isFirstLaunch", value).apply();
}
} }

View File

@ -2,6 +2,8 @@ package one.nem.lacerta.utils.repository;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import one.nem.lacerta.model.pref.FeatureSwitchOverride;
public interface SharedPrefUtils { public interface SharedPrefUtils {
// Shared preferences editorの取得 // Shared preferences editorの取得
@ -10,4 +12,15 @@ public interface SharedPrefUtils {
// TODO-rca: 名称をenumで管理する // TODO-rca: 名称をenumで管理する
SharedPreferences.Editor getEditor(String name); SharedPreferences.Editor getEditor(String name);
SharedPreferences getSharedPreferences(String name);
SharedPreferences getSharedPreferences();
boolean getFeatureSwitchOverride(FeatureSwitchOverride featureSwitchOverride);
void setFeatureSwitchOverride(FeatureSwitchOverride featureSwitchOverride, boolean value);
boolean getIsFirstLaunch();
void setIsFirstLaunch(boolean value);
} }