diff --git a/.idea/misc.xml b/.idea/misc.xml index 0ad17cbd..8978d23d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/app/build.gradle b/app/build.gradle index 300174c9..ae313899 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -78,4 +78,8 @@ dependencies { implementation project(':data') implementation project(':shared:ui') + + implementation project(':model') + + implementation project(':utils') } \ No newline at end of file diff --git a/app/src/main/java/one/nem/lacerta/MainActivity.java b/app/src/main/java/one/nem/lacerta/MainActivity.java index 6dec2e80..84d15b35 100644 --- a/app/src/main/java/one/nem/lacerta/MainActivity.java +++ b/app/src/main/java/one/nem/lacerta/MainActivity.java @@ -10,31 +10,45 @@ import androidx.navigation.ui.NavigationUI; import android.graphics.Color; import android.os.Bundle; import android.util.Log; +import android.view.Menu; +import android.view.MenuItem; 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 java.io.NotActiveException; import dagger.hilt.android.AndroidEntryPoint; +import one.nem.lacerta.utils.repository.SharedPrefUtils; + import javax.inject.Inject; @AndroidEntryPoint public class MainActivity extends AppCompatActivity { + @Inject + SharedPrefUtils sharedPrefUtils; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav); + + // Initialize app + if (sharedPrefUtils.getIsFirstLaunch()) initializeApp(); + // Init navigation try { FragmentManager supportFragmentManager = getSupportFragmentManager(); NavHostFragment navHostFragment = (NavHostFragment) supportFragmentManager.findFragmentById(R.id.nav_host_fragment); assert navHostFragment != null; NavController navController = navHostFragment.getNavController(); - BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav); NavigationUI.setupWithNavController(bottomNavigationView, navController); } catch (Exception e) { @@ -44,10 +58,30 @@ public class MainActivity extends AppCompatActivity { 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 getWindow().setNavigationBarColor(ContextCompat.getColor(this, one.nem.lacerta.shared.ui.R.color.colorSecondaryContainer)); // Set status bar color 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); + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 3305b5e2..cc3f088c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -18,7 +18,7 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:navGraph="@navigation/main_nav" - tools:layout="@layout/fragment_debug_menu_container" /> + tools:layout="@layout/fragment_home_top" /> + app:layout_constraintStart_toStartOf="parent" /> \ No newline at end of file diff --git a/app/src/main/res/menu/bottom_nav_menu.xml b/app/src/main/res/menu/bottom_nav_menu.xml index 87e98113..12a0311d 100644 --- a/app/src/main/res/menu/bottom_nav_menu.xml +++ b/app/src/main/res/menu/bottom_nav_menu.xml @@ -19,7 +19,7 @@ + android:title="@string/debug_menu_title" /> + app:startDestination="@id/feature_home_navigation"> diff --git a/model/src/main/java/one/nem/lacerta/model/pref/FeatureSwitchOverride.java b/model/src/main/java/one/nem/lacerta/model/pref/FeatureSwitchOverride.java new file mode 100644 index 00000000..9a9a2497 --- /dev/null +++ b/model/src/main/java/one/nem/lacerta/model/pref/FeatureSwitchOverride.java @@ -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; + } + +} diff --git a/utils/src/main/java/one/nem/lacerta/utils/FeatureSwitch.java b/utils/src/main/java/one/nem/lacerta/utils/FeatureSwitch.java new file mode 100644 index 00000000..1c198478 --- /dev/null +++ b/utils/src/main/java/one/nem/lacerta/utils/FeatureSwitch.java @@ -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; + } +} diff --git a/utils/src/main/java/one/nem/lacerta/utils/impl/SharedPrefUtilsImpl.java b/utils/src/main/java/one/nem/lacerta/utils/impl/SharedPrefUtilsImpl.java index f976b46b..07f3f012 100644 --- a/utils/src/main/java/one/nem/lacerta/utils/impl/SharedPrefUtilsImpl.java +++ b/utils/src/main/java/one/nem/lacerta/utils/impl/SharedPrefUtilsImpl.java @@ -4,13 +4,17 @@ package one.nem.lacerta.utils.impl; import android.content.Context; import android.content.SharedPreferences; +import javax.inject.Inject; + import dagger.hilt.android.qualifiers.ApplicationContext; +import one.nem.lacerta.model.pref.FeatureSwitchOverride; import one.nem.lacerta.utils.repository.SharedPrefUtils; public class SharedPrefUtilsImpl implements SharedPrefUtils{ private final Context applicationContext; + @Inject public SharedPrefUtilsImpl(@ApplicationContext Context applicationContext) { this.applicationContext = applicationContext; } @@ -26,4 +30,34 @@ public class SharedPrefUtilsImpl implements SharedPrefUtils{ // Editorの取得 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(); + } } diff --git a/utils/src/main/java/one/nem/lacerta/utils/repository/SharedPrefUtils.java b/utils/src/main/java/one/nem/lacerta/utils/repository/SharedPrefUtils.java index 68d21ae7..ac807157 100644 --- a/utils/src/main/java/one/nem/lacerta/utils/repository/SharedPrefUtils.java +++ b/utils/src/main/java/one/nem/lacerta/utils/repository/SharedPrefUtils.java @@ -2,6 +2,8 @@ package one.nem.lacerta.utils.repository; import android.content.SharedPreferences; +import one.nem.lacerta.model.pref.FeatureSwitchOverride; + public interface SharedPrefUtils { // Shared preferences editorの取得 @@ -10,4 +12,15 @@ public interface SharedPrefUtils { // TODO-rca: 名称をenumで管理する? 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); }