Skip to content

Commit

Permalink
Add new demo examples. Extra frames to add own content i preferences …
Browse files Browse the repository at this point in the history
…activity.
  • Loading branch information
michal_szwarc committed Mar 24, 2015
1 parent ba76dd8 commit da1f1aa
Show file tree
Hide file tree
Showing 25 changed files with 376 additions and 80 deletions.
4 changes: 2 additions & 2 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kenumir.materialsettings">
<manifest
package="com.kenumir.materialsettings">

<application />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,130 @@
package com.kenumir.materialsettings;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.kenumir.materialsettings.storage.SimpleStorageInterface;
import com.kenumir.materialsettings.storage.StorageInterface;

import java.util.HashMap;
import java.util.Map;

/**
* Created by Kenumir on 2015-03-15.
*/
public abstract class MaterialSettings extends ActionBarActivity {

public static enum ContentFrames {
FRAME_TOP(0),
FRAME_TOP_INSIDE(1),
FRAME_BOTTOM(2),
FRAME_BOTTOM_INSIDE(3);

private int id;

ContentFrames(int idx) {
id = idx;
}
public int getValue() {
return this.id;
}
}

private static String SAVE_PREFIX = "SSI_";

private LinearLayout material_settings_content;
private Toolbar toolbar;
private StorageInterface mStorageInterface;
private HashMap<String, MaterialSettingsItem> items;
private FrameLayout[] frames;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);

items = new HashMap<String, MaterialSettingsItem>();
items = new HashMap<>();

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

material_settings_content = (LinearLayout) findViewById(R.id.material_settings_content);
frames = new FrameLayout[4];
frames[0] = (FrameLayout) findViewById(R.id.material_settings_top_frame);
frames[1] = (FrameLayout) findViewById(R.id.material_settings_top_frame_inside);
frames[2] = (FrameLayout) findViewById(R.id.material_settings_bottom_frame_inside);
frames[3] = (FrameLayout) findViewById(R.id.material_settings_bottom_frame);

mStorageInterface = initStorageInterface();

if (savedInstanceState != null) {
for(String key : savedInstanceState.keySet()) {
if (key.startsWith(SAVE_PREFIX)) {
String keyName = key.substring(SAVE_PREFIX.length());
Object value = savedInstanceState.get(key);
//Log.i("tests", "k: " + key + " k2: " + keyName +", t=" + value.getClass().getName() + ", v=" + value);
if (value instanceof String) {
mStorageInterface.save(keyName, (String) value);
} else if (value instanceof Integer) {
mStorageInterface.save(keyName, (Integer) value);
} else if (value instanceof Float) {
mStorageInterface.save(keyName, (Float) value);
} else if (value instanceof Long) {
mStorageInterface.save(keyName, (Long) value);
} else if (value instanceof Boolean) {
mStorageInterface.save(keyName, (Boolean) value);
} else {
mStorageInterface.save(keyName, value.toString());
}
}
}
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
StorageInterface si = getStorageInterface();
if (si instanceof SimpleStorageInterface) {
saveAll();
Map<String, ?> all = ((SimpleStorageInterface) si).getAll();
if (all.size() > 0) {
// save to bundle
for(String key : all.keySet()) {
Object value = all.get(key);
if (value instanceof String) {
outState.putString(SAVE_PREFIX + key, (String) value);
} else if (value instanceof Integer) {
outState.putInt(SAVE_PREFIX + key, (Integer) value);
} else if (value instanceof Float) {
outState.putFloat(SAVE_PREFIX + key, (Float) value);
} else if (value instanceof Long) {
outState.putString(SAVE_PREFIX + key, (String) value);
} else if (value instanceof Boolean) {
outState.putBoolean(SAVE_PREFIX + key, (Boolean) value);
} else {
outState.putString(SAVE_PREFIX + key, value.toString());
}
}
}
}
super.onSaveInstanceState(outState);
}

public FrameLayout getContentFrame(ContentFrames frame) {
return frames[frame.getValue()];
}

/**
* save all settings values at StorageInterface
*/
public void saveAll() {
for(String key : items.keySet()) {
items.get(key).save();
}
}

public void addItem(MaterialSettingsItem item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
*/
public abstract class MaterialSettingsItem {

protected MaterialSettings mContext;
protected Context mContext;
protected MaterialSettings mMaterialSettings;
protected String name;

public MaterialSettingsItem(MaterialSettings ctx, String name) {
public MaterialSettingsItem(Context ctx, String name) {
this.mContext = ctx;
if (ctx instanceof MaterialSettings)
this.mMaterialSettings = (MaterialSettings) ctx;
this.name = name;
}

Expand All @@ -37,11 +40,19 @@ public View getView(ViewGroup parent) {
return null;
}

public void setMaterialSettings(MaterialSettings m) {
mMaterialSettings = m;
}

public StorageInterface getStorageInterface() {
return mContext.getStorageInterface();
if (mMaterialSettings != null)
return mMaterialSettings.getStorageInterface();
else
return null;
}

public abstract int getViewResource();
public abstract void setupView(View v);
public abstract void save();

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.widget.CompoundButton;
import android.widget.TextView;

import com.kenumir.materialsettings.MaterialSettings;
import com.kenumir.materialsettings.MaterialSettingsItem;
import com.kenumir.materialsettings.R;
import com.kenumir.materialsettings.views.CheckableLinearLayout;
Expand All @@ -25,7 +24,7 @@ public static interface OnCheckedChangeListener {
private CheckableLinearLayout mCheckableLinearLayout;
private OnCheckedChangeListener mOnCheckedChangeListener;

public CheckboxItem(MaterialSettings ctx, String name) {
public CheckboxItem(Context ctx, String name) {
super(ctx, name);
}

Expand All @@ -36,7 +35,8 @@ public int getViewResource() {

@Override
public void setupView(View v) {
checked = getStorageInterface().load(name, isDefaultValue());

checked = getStorageInterface() != null ? getStorageInterface().load(name, isDefaultValue()) : isDefaultValue();
mCheckableLinearLayout = (CheckableLinearLayout) v;
titleView = (TextView) v.findViewById(R.id.material_dialog_item_title);
subtitleView = (TextView) v.findViewById(R.id.material_dialog_item_subtitle);
Expand All @@ -49,13 +49,19 @@ public void setupView(View v) {
mCheckableLinearLayout.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mContext.getStorageInterface().save(name, isChecked);
save();
if (getOnCheckedChangeListener() != null)
getOnCheckedChangeListener().onCheckedChange(CheckboxItem.this, isChecked);
}
});
}

@Override
public void save() {
if (getStorageInterface() != null)
getStorageInterface().save(name, isChecked());
}

public CheckboxItem updateTitle(String newTitle) {
if (titleView != null)
titleView.setText(newTitle);
Expand Down Expand Up @@ -89,6 +95,7 @@ public CheckboxItem setSubtitle(String subtitle) {
}

public boolean isChecked() {
//Log.d("tests", "C: " + name + "=" + mCheckableLinearLayout.isChecked());
return mCheckableLinearLayout.isChecked();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.Context;
import android.view.View;

import com.kenumir.materialsettings.MaterialSettings;
import com.kenumir.materialsettings.MaterialSettingsItem;
import com.kenumir.materialsettings.R;

Expand All @@ -12,7 +11,7 @@
*/
public class DividerItem extends MaterialSettingsItem {

public DividerItem(MaterialSettings ctx) {
public DividerItem(Context ctx) {
super(ctx, null);
}

Expand All @@ -25,4 +24,9 @@ public int getViewResource() {
public void setupView(View v) {

}

@Override
public void save() {
// NOP
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.view.View;
import android.widget.TextView;

import com.kenumir.materialsettings.MaterialSettings;
import com.kenumir.materialsettings.MaterialSettingsItem;
import com.kenumir.materialsettings.R;

Expand All @@ -15,7 +14,7 @@ public class HeaderItem extends MaterialSettingsItem {

private String title;

public HeaderItem(MaterialSettings ctx) {
public HeaderItem(Context ctx) {
super(ctx, null);
}

Expand All @@ -29,6 +28,11 @@ public void setupView(View v) {
((TextView) v).setText(title);
}

@Override
public void save() {
// NOP
}

public String getTitle() {
return title;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.kenumir.materialsettings.items;

import com.kenumir.materialsettings.MaterialSettings;
import android.content.Context;

import com.kenumir.materialsettings.R;

/**
* Created by Kenumir on 2015-03-20.
*/
public class SwitcherItem extends CheckboxItem {

public SwitcherItem(MaterialSettings ctx, String name) {
public SwitcherItem(Context ctx, String name) {
super(ctx, name);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.kenumir.materialsettings.items;

import android.content.Context;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kenumir.materialsettings.MaterialSettings;
import com.kenumir.materialsettings.MaterialSettingsItem;
import com.kenumir.materialsettings.R;
import com.kenumir.materialsettings.views.CheckableLinearLayout;

/**
* Created by Kenumir on 2015-03-16.
Expand All @@ -24,7 +21,7 @@ public static interface OnClickListener {
private OnClickListener onclick;
private TextView titleView, subtitleView;

public TextItem(MaterialSettings ctx, String name) {
public TextItem(Context ctx, String name) {
super(ctx, name);
}

Expand All @@ -50,6 +47,11 @@ public void onClick(View v) {
});
}

@Override
public void save() {
// NOP
}

public TextItem updateTitle(String newTitle) {
if (titleView != null)
titleView.setText(newTitle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.content.Context;
import android.content.SharedPreferences;

import java.util.Map;

/**
* Created by Kenumir on 2015-03-18.
*/
Expand All @@ -15,12 +17,12 @@ public PreferencesStorageInterface(Context ctx) {
}

@Override
public void save(String key, boolean value) {
public void save(String key, Boolean value) {
prefs.edit().putBoolean(key, value).apply();
}

@Override
public boolean load(String key, boolean defaultValue) {
public boolean load(String key, Boolean defaultValue) {
return prefs.getBoolean(key, defaultValue);
}

Expand Down Expand Up @@ -63,4 +65,10 @@ public void save(String key, Long value) {
public Long load(String key, Long defaultValue) {
return prefs.getLong(key, defaultValue);
}

@Override
public Map<String, ?> getAll() {

return prefs.getAll();
}
}
Loading

0 comments on commit da1f1aa

Please sign in to comment.