-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #321 from novoda/BOT-320/feature-flags
BOT-320/Feature menu
- Loading branch information
Showing
12 changed files
with
245 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...t/mobile/src/main/java/com/novoda/tpbot/feature_selection/FeatureSelectionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.novoda.tpbot.feature_selection; | ||
|
||
public interface FeatureSelectionController<LIST, FEATURE> { | ||
|
||
void attachFeatureSelectionTo(LIST toAttachTo); | ||
|
||
void handleFeatureToggle(FEATURE featureRepresentation); | ||
|
||
boolean contains(FEATURE featureRepresentation); | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
.../mobile/src/main/java/com/novoda/tpbot/feature_selection/FeatureSelectionPersistence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.novoda.tpbot.feature_selection; | ||
|
||
public interface FeatureSelectionPersistence { | ||
|
||
boolean isFeatureEnabled(); | ||
|
||
void setFeatureEnabled(); | ||
|
||
void setFeatureDisabled(); | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
...bile/src/main/java/com/novoda/tpbot/feature_selection/MenuFeatureSelectionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.novoda.tpbot.feature_selection; | ||
|
||
import android.content.Context; | ||
import android.support.annotation.MenuRes; | ||
import android.util.SparseArray; | ||
import android.view.Menu; | ||
import android.view.MenuInflater; | ||
import android.view.MenuItem; | ||
|
||
import com.novoda.tpbot.R; | ||
|
||
public final class MenuFeatureSelectionController implements FeatureSelectionController<Menu, MenuItem> { | ||
|
||
@MenuRes | ||
private static final int FEATURE_MENU_RESOURCE = R.menu.feature_menu; | ||
|
||
private final MenuInflater menuInflater; | ||
private final SparseArray<FeatureSelectionPersistence> features; | ||
|
||
public static FeatureSelectionController<Menu, MenuItem> createFrom(Context context) { | ||
MenuInflater menuInflater = new MenuInflater(context); | ||
|
||
SparseArray<FeatureSelectionPersistence> features = new SparseArray<>(); | ||
features.put(R.id.video_call_menu_item, VideoCallSharedPreferencesPersistence.newInstance(context)); | ||
features.put(R.id.server_connection_menu_item, ServerConnectionSharedPreferencesPersistence.newInstance(context)); | ||
|
||
return new MenuFeatureSelectionController(menuInflater, features); | ||
} | ||
|
||
private MenuFeatureSelectionController(MenuInflater menuInflater, SparseArray<FeatureSelectionPersistence> features) { | ||
this.menuInflater = menuInflater; | ||
this.features = features; | ||
} | ||
|
||
@Override | ||
public void attachFeatureSelectionTo(Menu toAttachTo) { | ||
menuInflater.inflate(FEATURE_MENU_RESOURCE, toAttachTo); | ||
|
||
for (int index = 0; index < features.size(); index++) { | ||
int key = features.keyAt(index); | ||
|
||
MenuItem menuItem = toAttachTo.findItem(key); | ||
FeatureSelectionPersistence featureSelectionPersistence = features.get(key); | ||
menuItem.setChecked(featureSelectionPersistence.isFeatureEnabled()); | ||
} | ||
} | ||
|
||
@Override | ||
public void handleFeatureToggle(MenuItem featureRepresentation) { | ||
FeatureSelectionPersistence featureSelectionPersistence = features.get(featureRepresentation.getItemId()); | ||
|
||
if (featureSelectionPersistence == null) { | ||
throw new IllegalStateException("You must check if data is present before using handleFeatureToggle()."); | ||
} | ||
|
||
if (featureSelectionPersistence.isFeatureEnabled()) { | ||
featureRepresentation.setChecked(false); | ||
featureSelectionPersistence.setFeatureDisabled(); | ||
} else { | ||
featureRepresentation.setChecked(true); | ||
featureSelectionPersistence.setFeatureEnabled(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean contains(MenuItem featureRepresentation) { | ||
return features.get(featureRepresentation.getItemId()) != null; | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...java/com/novoda/tpbot/feature_selection/ServerConnectionSharedPreferencesPersistence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.novoda.tpbot.feature_selection; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
|
||
public final class ServerConnectionSharedPreferencesPersistence implements FeatureSelectionPersistence { | ||
|
||
private static final String SERVER_CONNECTION_PREF_NAME = "server_connection"; | ||
private static final String SERVER_CONNECTION_PREFERENCES_ON_OFF_KEY = "server_connection_preferences_on_off"; | ||
private static final boolean ON = true; | ||
private static final boolean OFF = false; | ||
|
||
private final SharedPreferences sharedPreferences; | ||
|
||
public static ServerConnectionSharedPreferencesPersistence newInstance(Context context) { | ||
SharedPreferences sharedPreferences = context.getSharedPreferences(SERVER_CONNECTION_PREF_NAME, Context.MODE_PRIVATE); | ||
return new ServerConnectionSharedPreferencesPersistence(sharedPreferences); | ||
} | ||
|
||
private ServerConnectionSharedPreferencesPersistence(SharedPreferences sharedPreferences) { | ||
this.sharedPreferences = sharedPreferences; | ||
} | ||
|
||
@Override | ||
public boolean isFeatureEnabled() { | ||
return sharedPreferences.getBoolean(SERVER_CONNECTION_PREFERENCES_ON_OFF_KEY, OFF); | ||
} | ||
|
||
@Override | ||
public void setFeatureEnabled() { | ||
sharedPreferences.edit() | ||
.putBoolean(SERVER_CONNECTION_PREFERENCES_ON_OFF_KEY, ON) | ||
.apply(); | ||
} | ||
|
||
@Override | ||
public void setFeatureDisabled() { | ||
sharedPreferences.edit() | ||
.putBoolean(SERVER_CONNECTION_PREFERENCES_ON_OFF_KEY, OFF) | ||
.apply(); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...c/main/java/com/novoda/tpbot/feature_selection/VideoCallSharedPreferencesPersistence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.novoda.tpbot.feature_selection; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
|
||
public final class VideoCallSharedPreferencesPersistence implements FeatureSelectionPersistence { | ||
|
||
private static final String VIDEO_CALL_PREF_NAME = "video_call"; | ||
private static final String VIDEO_CALL_PREFERENCES_ON_OFF_KEY = "video_call_preferences_on_off"; | ||
private static final boolean ON = true; | ||
private static final boolean OFF = false; | ||
|
||
private final SharedPreferences sharedPreferences; | ||
|
||
public static VideoCallSharedPreferencesPersistence newInstance(Context context) { | ||
SharedPreferences sharedPreferences = context.getSharedPreferences(VIDEO_CALL_PREF_NAME, Context.MODE_PRIVATE); | ||
return new VideoCallSharedPreferencesPersistence(sharedPreferences); | ||
} | ||
|
||
private VideoCallSharedPreferencesPersistence(SharedPreferences sharedPreferences) { | ||
this.sharedPreferences = sharedPreferences; | ||
} | ||
|
||
@Override | ||
public boolean isFeatureEnabled() { | ||
return sharedPreferences.getBoolean(VIDEO_CALL_PREFERENCES_ON_OFF_KEY, OFF); | ||
} | ||
|
||
@Override | ||
public void setFeatureEnabled() { | ||
sharedPreferences.edit() | ||
.putBoolean(VIDEO_CALL_PREFERENCES_ON_OFF_KEY, ON) | ||
.apply(); | ||
} | ||
|
||
@Override | ||
public void setFeatureDisabled() { | ||
sharedPreferences.edit() | ||
.putBoolean(VIDEO_CALL_PREFERENCES_ON_OFF_KEY, OFF) | ||
.apply(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<menu xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto"> | ||
|
||
<item | ||
android:id="@+id/video_call_menu_item" | ||
android:icon="@android:drawable/checkbox_on_background" | ||
android:title="@string/automatically_join_video_call" | ||
android:checkable="true" | ||
app:showAsAction="never" /> | ||
|
||
<item | ||
android:id="@+id/server_connection_menu_item" | ||
android:icon="@android:drawable/checkbox_on_background" | ||
android:title="@string/connect_to_server" | ||
android:checkable="true" | ||
app:showAsAction="never" /> | ||
|
||
</menu> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters