mirror of https://github.com/kurisufriend/Clover
parent
28c81e6662
commit
c8c89345e9
@ -0,0 +1,19 @@ |
||||
package org.floens.chan.core.preferences; |
||||
|
||||
import android.content.SharedPreferences; |
||||
|
||||
public abstract class Preference<T> { |
||||
protected final SharedPreferences sharedPreferences; |
||||
protected final String key; |
||||
protected final T def; |
||||
|
||||
public Preference(SharedPreferences sharedPreferences, String key, T def) { |
||||
this.sharedPreferences = sharedPreferences; |
||||
this.key = key; |
||||
this.def = def; |
||||
} |
||||
|
||||
public abstract T get(); |
||||
|
||||
public abstract void set(T value); |
||||
} |
@ -0,0 +1,19 @@ |
||||
package org.floens.chan.core.preferences; |
||||
|
||||
import android.content.SharedPreferences; |
||||
|
||||
public class StringPreference extends Preference<String> { |
||||
public StringPreference(SharedPreferences sharedPreferences, String key, String def) { |
||||
super(sharedPreferences, key, def); |
||||
} |
||||
|
||||
@Override |
||||
public String get() { |
||||
return sharedPreferences.getString(key, def); |
||||
} |
||||
|
||||
@Override |
||||
public void set(String value) { |
||||
sharedPreferences.edit().putString(key, value).apply(); |
||||
} |
||||
} |
@ -0,0 +1,106 @@ |
||||
/* |
||||
* Clover - 4chan browser https://github.com/Floens/Clover/
|
||||
* Copyright (C) 2014 Floens |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, either version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package org.floens.chan.ui.controller; |
||||
|
||||
import android.app.AlertDialog; |
||||
import android.content.Context; |
||||
import android.view.View; |
||||
import android.widget.LinearLayout; |
||||
|
||||
import org.floens.chan.R; |
||||
import org.floens.chan.core.preferences.ChanPreferences; |
||||
import org.floens.chan.ui.preferences.LinkPreference; |
||||
import org.floens.chan.ui.preferences.ListPreference; |
||||
import org.floens.chan.ui.preferences.PreferenceGroup; |
||||
import org.floens.chan.ui.preferences.PreferenceItem; |
||||
import org.floens.chan.ui.preferences.PreferencesController; |
||||
|
||||
public class MainSettingsController extends PreferencesController { |
||||
private ListPreference theme; |
||||
private LinkPreference link; |
||||
|
||||
public MainSettingsController(Context context) { |
||||
super(context); |
||||
} |
||||
|
||||
@Override |
||||
public void onCreate() { |
||||
super.onCreate(); |
||||
|
||||
//TODO correct header colors, background, themeing
|
||||
|
||||
navigationItem.title = context.getString(R.string.action_settings); |
||||
|
||||
view = inflateRes(R.layout.settings_layout); |
||||
content = (LinearLayout) view.findViewById(R.id.scrollview_content); |
||||
|
||||
populatePreferences(); |
||||
|
||||
buildPreferences(); |
||||
} |
||||
|
||||
@Override |
||||
public void onPreferenceChange(PreferenceItem item) { |
||||
super.onPreferenceChange(item); |
||||
|
||||
if (item == theme) { |
||||
link.setEnabled(((ListPreference)item).getPreference().get().equals("dark")); |
||||
} |
||||
} |
||||
|
||||
private void populatePreferences() { |
||||
PreferenceGroup settings = new PreferenceGroup("Settings"); |
||||
|
||||
ListPreference.Item[] themeItems = new ListPreference.Item[2]; |
||||
themeItems[0] = new ListPreference.Item<>("Light", "light"); |
||||
themeItems[1] = new ListPreference.Item<>("Dark", "dark"); |
||||
theme = new ListPreference(this, ChanPreferences.testTheme, "Theme", themeItems); |
||||
settings.preferenceItems.add(theme); |
||||
|
||||
// BooleanPreference bool = new BooleanPreference(p, "A name", "akey", false);
|
||||
// settings.preferenceItems.add(bool);
|
||||
|
||||
link = new LinkPreference(this, "A link", new View.OnClickListener() { |
||||
public void onClick(View v) { |
||||
new AlertDialog.Builder(context).setMessage("click").setPositiveButton(R.string.ok, null).show(); |
||||
} |
||||
}); |
||||
settings.preferenceItems.add(link); |
||||
|
||||
groups.add(settings); |
||||
|
||||
/*PreferenceGroup posting = new PreferenceGroup("Posting"); |
||||
|
||||
ListPreference.Item[] postItems = new ListPreference.Item[4]; |
||||
postItems[0] = new ListPreference.Item<>("Top", "one"); |
||||
postItems[1] = new ListPreference.Item<>("Top", "two"); |
||||
postItems[2] = new ListPreference.Item<>("Top", "three"); |
||||
postItems[3] = new ListPreference.Item<>("Top", "four"); |
||||
posting.preferenceItems.add(new ListPreference(p, "Something", "something", postItems)); |
||||
posting.preferenceItems.add(new ListPreference(p, "Something", "something", postItems)); |
||||
posting.preferenceItems.add(new ListPreference(p, "Something", "something", postItems)); |
||||
posting.preferenceItems.add(new ListPreference(p, "Something", "something", postItems)); |
||||
posting.preferenceItems.add(new ListPreference(p, "Something", "something", postItems)); |
||||
posting.preferenceItems.add(new ListPreference(p, "Something", "something", postItems)); |
||||
posting.preferenceItems.add(new ListPreference(p, "Something", "something", postItems)); |
||||
posting.preferenceItems.add(new ListPreference(p, "Something", "something", postItems)); |
||||
posting.preferenceItems.add(new ListPreference(p, "Something", "something", postItems)); |
||||
|
||||
groups.add(posting);*/ |
||||
} |
||||
} |
@ -1,38 +0,0 @@ |
||||
/* |
||||
* Clover - 4chan browser https://github.com/Floens/Clover/
|
||||
* Copyright (C) 2014 Floens |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, either version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package org.floens.chan.ui.controller; |
||||
|
||||
import android.content.Context; |
||||
|
||||
import org.floens.chan.R; |
||||
import org.floens.chan.controller.Controller; |
||||
|
||||
public class SettingsController extends Controller { |
||||
public SettingsController(Context context) { |
||||
super(context); |
||||
} |
||||
|
||||
@Override |
||||
public void onCreate() { |
||||
super.onCreate(); |
||||
|
||||
navigationItem.title = context.getString(R.string.action_settings); |
||||
|
||||
view = inflateRes(R.layout.settings_layout); |
||||
} |
||||
} |
@ -0,0 +1,40 @@ |
||||
package org.floens.chan.ui.preferences; |
||||
|
||||
import android.support.v7.widget.SwitchCompat; |
||||
import android.view.View; |
||||
import android.widget.CompoundButton; |
||||
|
||||
import org.floens.chan.R; |
||||
import org.floens.chan.core.preferences.Preference; |
||||
|
||||
public class BooleanPreference extends PreferenceItem implements View.OnClickListener, CompoundButton.OnCheckedChangeListener { |
||||
private SwitchCompat switcher; |
||||
private Preference<Boolean> preference; |
||||
|
||||
public BooleanPreference(PreferencesController preferencesController, Preference<Boolean> preference, String name) { |
||||
super(preferencesController, name); |
||||
this.preference = preference; |
||||
} |
||||
|
||||
@Override |
||||
public void setView(View view) { |
||||
super.setView(view); |
||||
|
||||
view.setOnClickListener(this); |
||||
|
||||
switcher = (SwitchCompat) view.findViewById(R.id.switcher); |
||||
switcher.setOnCheckedChangeListener(this); |
||||
|
||||
switcher.setChecked(preference.get()); |
||||
} |
||||
|
||||
@Override |
||||
public void onClick(View v) { |
||||
switcher.toggle(); |
||||
} |
||||
|
||||
@Override |
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
||||
preference.set(isChecked); |
||||
} |
||||
} |
@ -0,0 +1,23 @@ |
||||
package org.floens.chan.ui.preferences; |
||||
|
||||
import android.view.View; |
||||
|
||||
public class LinkPreference extends PreferenceItem { |
||||
private final View.OnClickListener clickListener; |
||||
|
||||
public LinkPreference(PreferencesController preferencesController, String name, View.OnClickListener clickListener) { |
||||
super(preferencesController, name); |
||||
this.clickListener = clickListener; |
||||
} |
||||
|
||||
@Override |
||||
public void setView(View view) { |
||||
super.setView(view); |
||||
view.setOnClickListener(clickListener); |
||||
} |
||||
|
||||
@Override |
||||
public void setEnabled(boolean enabled) { |
||||
|
||||
} |
||||
} |
@ -0,0 +1,87 @@ |
||||
package org.floens.chan.ui.preferences; |
||||
|
||||
import android.view.Gravity; |
||||
import android.view.View; |
||||
|
||||
import org.floens.chan.core.preferences.Preference; |
||||
import org.floens.chan.ui.view.FloatingMenu; |
||||
import org.floens.chan.ui.view.FloatingMenuItem; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import static org.floens.chan.utils.AndroidUtils.dp; |
||||
|
||||
public class ListPreference extends PreferenceItem implements FloatingMenu.FloatingMenuCallback, View.OnClickListener { |
||||
public final Item[] items; |
||||
|
||||
private Preference<String> preference; |
||||
|
||||
private int selected; |
||||
|
||||
public ListPreference(PreferencesController preferencesController, Preference<String> preference, String name, Item[] items) { |
||||
super(preferencesController, name); |
||||
this.preference = preference; |
||||
this.items = items; |
||||
|
||||
selectItem(); |
||||
} |
||||
|
||||
public String getBottomDescription() { |
||||
return items[selected].name; |
||||
} |
||||
|
||||
public Preference<String> getPreference() { |
||||
return preference; |
||||
} |
||||
|
||||
@Override |
||||
public void setView(View view) { |
||||
super.setView(view); |
||||
view.setOnClickListener(this); |
||||
} |
||||
|
||||
@Override |
||||
public void onClick(View v) { |
||||
List<FloatingMenuItem> menuItems = new ArrayList<>(2); |
||||
|
||||
for (Item item : items) { |
||||
menuItems.add(new FloatingMenuItem(item.key, item.name)); |
||||
} |
||||
|
||||
FloatingMenu menu = new FloatingMenu(v.getContext()); |
||||
menu.setAnchor(v, Gravity.LEFT, dp(5), dp(5)); |
||||
menu.setPopupWidth(FloatingMenu.POPUP_WIDTH_ANCHOR); |
||||
menu.setCallback(this); |
||||
menu.setItems(menuItems); |
||||
menu.show(); |
||||
} |
||||
|
||||
@Override |
||||
public void onFloatingMenuItemClicked(FloatingMenu menu, FloatingMenuItem item) { |
||||
String selectedKey = (String) item.getId(); |
||||
preference.set(selectedKey); |
||||
selectItem(); |
||||
preferencesController.onPreferenceChange(this); |
||||
} |
||||
|
||||
private void selectItem() { |
||||
String selectedKey = preference.get(); |
||||
for (int i = 0; i < items.length; i++) { |
||||
if (items[i].key.equals(selectedKey)) { |
||||
selected = i; |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
|
||||
public static class Item<T> { |
||||
public final String name; |
||||
public final T key; |
||||
|
||||
public Item(String name, T key) { |
||||
this.name = name; |
||||
this.key = key; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,14 @@ |
||||
package org.floens.chan.ui.preferences; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class PreferenceGroup { |
||||
public final String name; |
||||
|
||||
public PreferenceGroup(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public List<PreferenceItem> preferenceItems = new ArrayList<>(); |
||||
} |
@ -0,0 +1,29 @@ |
||||
package org.floens.chan.ui.preferences; |
||||
|
||||
import android.view.View; |
||||
|
||||
public abstract class PreferenceItem { |
||||
public PreferencesController preferencesController; |
||||
public final String name; |
||||
public View view; |
||||
|
||||
public PreferenceItem(PreferencesController preferencesController, String name) { |
||||
this.preferencesController = preferencesController; |
||||
this.name = name; |
||||
} |
||||
|
||||
public void setView(View view) { |
||||
this.view = view; |
||||
} |
||||
|
||||
public void setEnabled(boolean enabled) { |
||||
} |
||||
|
||||
public String getTopDescription() { |
||||
return name; |
||||
} |
||||
|
||||
public String getBottomDescription() { |
||||
return null; |
||||
} |
||||
} |
@ -0,0 +1,137 @@ |
||||
package org.floens.chan.ui.preferences; |
||||
|
||||
import android.content.Context; |
||||
import android.content.res.Configuration; |
||||
import android.view.LayoutInflater; |
||||
import android.view.View; |
||||
import android.view.ViewGroup; |
||||
import android.widget.LinearLayout; |
||||
import android.widget.TextView; |
||||
|
||||
import org.floens.chan.R; |
||||
import org.floens.chan.controller.Controller; |
||||
import org.floens.chan.utils.AndroidUtils; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import static org.floens.chan.utils.AndroidUtils.dp; |
||||
|
||||
public class PreferencesController extends Controller implements AndroidUtils.OnMeasuredCallback { |
||||
protected LinearLayout content; |
||||
protected List<PreferenceGroup> groups = new ArrayList<>(); |
||||
|
||||
public PreferencesController(Context context) { |
||||
super(context); |
||||
} |
||||
|
||||
@Override |
||||
public void onCreate() { |
||||
super.onCreate(); |
||||
} |
||||
|
||||
@Override |
||||
public void onShow() { |
||||
super.onShow(); |
||||
|
||||
AndroidUtils.waitForLayout(view, this); |
||||
} |
||||
|
||||
@Override |
||||
public void onConfigurationChanged(Configuration newConfig) { |
||||
super.onConfigurationChanged(newConfig); |
||||
AndroidUtils.waitForLayout(view, this); |
||||
} |
||||
|
||||
@Override |
||||
public void onMeasured(View view, int width, int height) { |
||||
setMargins(); |
||||
} |
||||
|
||||
public void onPreferenceChange(PreferenceItem item) { |
||||
if (item instanceof ListPreference) { |
||||
setDescriptionText(item.view, item.getTopDescription(), item.getBottomDescription()); |
||||
} |
||||
} |
||||
|
||||
private void setMargins() { |
||||
boolean tablet = view.getWidth() > dp(500); // TODO is tablet
|
||||
|
||||
int margin = 0; |
||||
if (tablet) { |
||||
margin = (int) (view.getWidth() * 0.1); |
||||
} |
||||
|
||||
int itemMargin = 0; |
||||
if (tablet) { |
||||
itemMargin = dp(16); |
||||
} |
||||
|
||||
List<View> groups = AndroidUtils.findViewsById(content, R.id.group); |
||||
for (View group : groups) { |
||||
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) group.getLayoutParams(); |
||||
params.leftMargin = margin; |
||||
params.rightMargin = margin; |
||||
group.setLayoutParams(params); |
||||
} |
||||
|
||||
List<View> items = AndroidUtils.findViewsById(content, R.id.preference_item); |
||||
for (View item : items) { |
||||
item.setPadding(itemMargin, item.getPaddingTop(), itemMargin, item.getPaddingBottom()); |
||||
} |
||||
} |
||||
|
||||
protected void buildPreferences() { |
||||
LayoutInflater inf = LayoutInflater.from(context); |
||||
boolean firstGroup = true; |
||||
for (PreferenceGroup group : groups) { |
||||
LinearLayout groupLayout = (LinearLayout) inf.inflate(R.layout.preference_group, content, false); |
||||
((TextView) groupLayout.findViewById(R.id.header)).setText(group.name); |
||||
|
||||
if (firstGroup) { |
||||
firstGroup = false; |
||||
((LinearLayout.LayoutParams) groupLayout.getLayoutParams()).topMargin = 0; |
||||
} |
||||
|
||||
content.addView(groupLayout); |
||||
|
||||
for (int i = 0; i < group.preferenceItems.size(); i++) { |
||||
PreferenceItem preferenceItem = group.preferenceItems.get(i); |
||||
|
||||
ViewGroup preferenceView = null; |
||||
String topValue = preferenceItem.getTopDescription(); |
||||
String bottomValue = preferenceItem.getBottomDescription(); |
||||
|
||||
if ((preferenceItem instanceof ListPreference) || (preferenceItem instanceof LinkPreference)) { |
||||
preferenceView = (ViewGroup) inf.inflate(R.layout.preference_link, groupLayout, false); |
||||
} else if (preferenceItem instanceof BooleanPreference) { |
||||
preferenceView = (ViewGroup) inf.inflate(R.layout.preference_boolean, groupLayout, false); |
||||
} |
||||
|
||||
setDescriptionText(preferenceView, topValue, bottomValue); |
||||
|
||||
groupLayout.addView(preferenceView); |
||||
|
||||
preferenceItem.setView(preferenceView); |
||||
|
||||
if (i < group.preferenceItems.size() - 1) { |
||||
inf.inflate(R.layout.preference_divider, groupLayout, true); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
private void setDescriptionText(View view, String topText, String bottomText) { |
||||
((TextView) view.findViewById(R.id.top)).setText(topText); |
||||
|
||||
TextView bottom = ((TextView) view.findViewById(R.id.bottom)); |
||||
if (bottom != null) { |
||||
if (bottomText == null) { |
||||
ViewGroup parent = (ViewGroup) bottom.getParent(); |
||||
parent.removeView(bottom); |
||||
} else { |
||||
bottom.setText(bottomText); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,28 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:id="@+id/preference_item" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:background="?android:selectableItemBackground" |
||||
android:orientation="horizontal" |
||||
android:paddingTop="16dp" |
||||
android:paddingBottom="16dp" |
||||
android:gravity="center_vertical"> |
||||
|
||||
<LinearLayout |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:orientation="vertical"> |
||||
|
||||
<include layout="@layout/preference_description" /> |
||||
|
||||
</LinearLayout> |
||||
|
||||
<android.support.v7.widget.SwitchCompat |
||||
android:id="@+id/switcher" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:paddingRight="16dp" /> |
||||
|
||||
</LinearLayout> |
@ -0,0 +1,25 @@ |
||||
<merge xmlns:android="http://schemas.android.com/apk/res/android"> |
||||
<TextView |
||||
android:id="@+id/top" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:ellipsize="end" |
||||
android:maxLines="1" |
||||
android:paddingLeft="16dp" |
||||
android:paddingRight="16dp" |
||||
android:singleLine="true" |
||||
android:textColor="#ff000000" |
||||
android:textSize="16sp" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/bottom" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:ellipsize="end" |
||||
android:maxLines="1" |
||||
android:paddingLeft="16dp" |
||||
android:paddingRight="16dp" |
||||
android:singleLine="true" |
||||
android:textColor="#89000000" |
||||
android:textSize="14sp" /> |
||||
</merge> |
@ -0,0 +1,6 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<View xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:id="@+id/divider" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="1px" |
||||
android:background="#22000000" /> |
@ -0,0 +1,25 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:id="@+id/group" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="vertical" |
||||
android:elevation="4dp" |
||||
android:background="#ffffffff" |
||||
android:layout_marginTop="6dp" |
||||
android:layout_marginBottom="6dp"> |
||||
|
||||
<TextView |
||||
android:id="@+id/header" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:paddingLeft="16dp" |
||||
android:paddingTop="16dp" |
||||
android:paddingRight="16dp" |
||||
android:singleLine="true" |
||||
android:maxLines="1" |
||||
android:ellipsize="end" |
||||
android:textSize="16sp" |
||||
android:textColor="#89000000" /> |
||||
|
||||
</LinearLayout> |
@ -0,0 +1,13 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:id="@+id/preference_item" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:background="?android:selectableItemBackground" |
||||
android:orientation="vertical" |
||||
android:paddingTop="16dp" |
||||
android:paddingBottom="16dp"> |
||||
|
||||
<include layout="@layout/preference_description" /> |
||||
|
||||
</LinearLayout> |
Loading…
Reference in new issue