Skip to content

Action module extended #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public void onClick() {
});

SpinnerAction<String> spinnerAction = new SpinnerAction<>(
"Test spinner",
Arrays.asList("First", "Second", "Third"),
new SpinnerAction.OnItemSelectedListener<String>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.concurrent.TimeUnit;

import io.palaima.debugdrawer.DebugDrawer;
import io.palaima.debugdrawer.actions.ActionSetup;
import io.palaima.debugdrawer.actions.ActionsModule;
import io.palaima.debugdrawer.actions.ButtonAction;
import io.palaima.debugdrawer.actions.SpinnerAction;
Expand Down Expand Up @@ -54,6 +55,10 @@ protected void onCreate(Bundle savedInstanceState) {

setupToolBar();

if (!ActionSetup.getInstance().isInitialised()) {
ActionSetup.getInstance().initDefault(this);
}

SwitchAction switchAction = new SwitchAction("Test switch", new SwitchAction.Listener() {
@Override
public void onCheckedChanged(boolean value) {
Expand All @@ -69,14 +74,14 @@ public void onClick() {
});

SpinnerAction<String> spinnerAction = new SpinnerAction<>(
"Test spinner",
Arrays.asList("First", "Second", "Third"),
new SpinnerAction.OnItemSelectedListener<String>() {
@Override
public void onItemSelected(String value) {
Toast.makeText(MainActivity.this, "Spinner item selected - " + value, Toast.LENGTH_LONG).show();
}
},
1
}
);

new DebugDrawer.Builder(this).modules(
Expand Down
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

buildscript {
repositories {
maven {
url "https://maven.google.com"
}
jcenter()
google()
}
Expand All @@ -18,6 +21,9 @@ buildscript {
allprojects {

repositories {
maven {
url "https://maven.google.com"
}
jcenter()
google()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.palaima.debugdrawer.actions;

import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.palaima.debugdrawer.actions;

import android.content.Context;
import android.support.annotation.NonNull;

public class ActionSetup
{
private static ActionSetup instance = null;

public static ActionSetup getInstance() {
if(instance == null) {
instance = new ActionSetup();
}
return instance;
}

private DefaultValueHandler defaultValueHandler = null;
private SwitchAction.ValueHandler switchValueHandler = null;
private SpinnerAction.ValueHandler spinnerValueHandler = null;

private ActionSetup() {
}

public boolean isInitialised() {
return defaultValueHandler != null || (switchValueHandler != null && spinnerValueHandler != null);
}

public void initDefault(Context context) {
defaultValueHandler = new DefaultValueHandler(context);
}

public void initSwitchValueHandler(SwitchAction.ValueHandler switchValueHandler, SpinnerAction.ValueHandler spinnerValueHandler) {
this.switchValueHandler = switchValueHandler;
this.spinnerValueHandler = spinnerValueHandler;
}

public final SwitchAction.ValueHandler getSwitchValueHandler() {
if (switchValueHandler == null && defaultValueHandler == null) {
throw new RuntimeException("You must initialised the ActionSetup class before you can use the ActionModule!"); }
return switchValueHandler != null ? switchValueHandler : defaultValueHandler;
}

public final SpinnerAction.ValueHandler getSpinnerActionHandler() {
if (spinnerValueHandler == null && defaultValueHandler == null) {
throw new RuntimeException("You must initialised the ActionSetup class before you can use the ActionModule!"); }
return spinnerValueHandler != null ? spinnerValueHandler : defaultValueHandler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public ActionsModule(String title, Action... actions) {
if (actions != null) {
this.actions.addAll(Arrays.asList(actions));
}

}

public ActionsModule(Action... actions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.palaima.debugdrawer.actions;

import android.content.Context;
import android.content.SharedPreferences;

public class DefaultValueHandler implements SwitchAction.ValueHandler, SpinnerAction.ValueHandler
{
private final SharedPreferences sharedPreferences;

public DefaultValueHandler(Context context) {
sharedPreferences = context.getSharedPreferences("debugdrawer", Context.MODE_PRIVATE);
}

public DefaultValueHandler(Context context, String preferenceName) {
sharedPreferences = context.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
}

@Override
public boolean getSwitchValue(String preferenceName)
{
return sharedPreferences.getBoolean(preferenceName, false);
}

@Override
public void setSwitchValue(String preferenceName, boolean enabled)
{
sharedPreferences.edit().putBoolean(preferenceName, enabled).apply();
}

@Override
public int getSpinnerValue(String preferenceName)
{
return sharedPreferences.getInt(preferenceName, 0);
}

@Override
public void setSpinnerValue(String preferenceName, int selectedPosition)
{
sharedPreferences.edit().putInt(preferenceName, selectedPosition).apply();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -18,44 +19,48 @@ public interface OnItemSelectedListener<T> {
void onItemSelected(T value);
}

private final static int SPINNER_DEFAULT_POSITION = 0;

private final String name;
private final List<String> titles;
private final List<T> values;
private final OnItemSelectedListener<T> listener;

private int selectedPosition = SPINNER_DEFAULT_POSITION;

public SpinnerAction(List<T> values, OnItemSelectedListener<T> listener) {
this(getTitles(values), values, listener, SPINNER_DEFAULT_POSITION);
}
private int selectedPosition;

public SpinnerAction(List<T> values, OnItemSelectedListener<T> listener, int initialSelectedPosition) {
this(getTitles(values), values, listener, initialSelectedPosition);
public SpinnerAction(String name, List<T> values, OnItemSelectedListener<T> listener) {
this(name, getTitles(values), values, listener);
}

public SpinnerAction(List<String> titles, List<T> values, OnItemSelectedListener<T> listener, int initialSelectedPosition) {
public SpinnerAction(String name, List<String> titles, List<T> values, OnItemSelectedListener<T> listener) {
this.name = name;
this.values = values;
this.titles = titles;
this.listener = listener;
if (initialSelectedPosition >= 0 && initialSelectedPosition < values.size()) {
this.selectedPosition = initialSelectedPosition;
} else {
this.selectedPosition = ActionSetup.getInstance().getSpinnerActionHandler().getSpinnerValue(name);
if (selectedPosition < 0 || selectedPosition >= values.size()) {
throw new IllegalStateException("initial selected position is out of bounds");
}
}

@Override
public View getView(@NonNull final LayoutInflater inflater, @NonNull final LinearLayout parent) {
final Context context = parent.getContext();
final Spinner spinner = (Spinner) inflater.inflate(R.layout.dd_debug_drawer_module_actions_spinner, parent, false);
final View viewGroup = inflater.inflate(R.layout.dd_debug_drawer_module_actions_spinner, parent, false);

final TextView textView = viewGroup.findViewById(R.id.action_spinner_name);
final Spinner spinner = viewGroup.findViewById(R.id.action_spinner);

textView.setText(name);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (listener != null && position != selectedPosition) {
listener.onItemSelected(values.get(position));
}
selectedPosition = position;
if (position != selectedPosition) {
selectedPosition = position;
ActionSetup.getInstance().getSpinnerActionHandler().setSpinnerValue(name, selectedPosition);
}
}

@Override
Expand All @@ -72,7 +77,7 @@ public void onNothingSelected(AdapterView<?> parent) {
spinner.setSelection(selectedPosition);
}

return spinner;
return viewGroup;
}

@Override
Expand Down Expand Up @@ -112,4 +117,9 @@ private static <T> List<String> getTitles(List<T> values) {
}
return titles;
}

public interface ValueHandler {
int getSpinnerValue(String preferenceName );
void setSpinnerValue(String preferenceName, int selectedPosition);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.palaima.debugdrawer.actions;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
Expand All @@ -11,16 +9,12 @@
import android.widget.Switch;
import android.widget.TextView;

import java.lang.ref.WeakReference;

public class SwitchAction implements Action {

private final String name;
private final Listener listener;
private final boolean shouldEmitFirstValue;

private WeakReference<Context> contextRef;

private Switch switchButton;

public SwitchAction(String name, @Nullable Listener listener) {
Expand All @@ -37,17 +31,14 @@ public SwitchAction(String name, @Nullable Listener listener, boolean shouldEmit

@Override
public View getView(@NonNull final LayoutInflater inflater, @NonNull final LinearLayout parent) {
final Context context = parent.getContext();

if (contextRef == null) {
contextRef = new WeakReference<>(context);
}

View viewGroup = inflater.inflate(R.layout.dd_debug_drawer_module_actions_switch, parent, false);

final TextView textView = viewGroup.findViewById(R.id.action_switch_name);
switchButton = viewGroup.findViewById(R.id.action_switch_switch);

textView.setText(name);

switchButton.setChecked(ActionSetup.getInstance().getSwitchValueHandler().getSwitchValue(name));
switchButton = viewGroup.findViewById(R.id.action_switch_switch);
switchButton.setOnCheckedChangeListener(switchListener);

Expand Down Expand Up @@ -76,7 +67,7 @@ public void onPause() {

@Override
public void onStart() {
final boolean isChecked = readValue();
final boolean isChecked = ActionSetup.getInstance().getSwitchValueHandler().getSwitchValue(name);

switchButton.setOnCheckedChangeListener(null);
switchButton.setChecked(isChecked);
Expand All @@ -98,31 +89,17 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (listener != null) {
listener.onCheckedChanged(isChecked);
}
writeValue(isChecked);
ActionSetup.getInstance().getSwitchValueHandler().setSwitchValue(name, isChecked);
}
};

public boolean isChecked() {
return readValue();
}

public void setChecked(boolean checked) {
switchButton.setChecked(checked);
}

private boolean readValue() {
return getPreferences().getBoolean(name, false);
}

private void writeValue(boolean b) {
getPreferences().edit().putBoolean(name, b).apply();
}

private SharedPreferences getPreferences() {
return contextRef.get().getSharedPreferences(name, Context.MODE_PRIVATE);
}

public interface Listener {
void onCheckedChanged(boolean value);
}

public interface ValueHandler
{
boolean getSwitchValue(String preferenceName);
void setSwitchValue(String preferenceName, boolean enabled);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Spinner
<LinearLayout
style="@style/Widget.DebugDrawer.Base.RowWidget"
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Widget.DebugDrawer.Base.RowTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/action_spinner_name"
style="@style/Widget.DebugDrawer.Base.RowTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Spinner
android:id="@+id/action_spinner"
style="@style/Widget.DebugDrawer.Base.RowTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>