Skip to content

Commit f876b5b

Browse files
author
yinjie
committed
support user customize background color and bug fix
1 parent b9fb6e9 commit f876b5b

31 files changed

+371
-30
lines changed

app/src/main/java/com/wingjay/jianshi/prefs/UserPrefs.java

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.Context;
44
import android.content.SharedPreferences;
55

6+
import com.wingjay.jianshi.R;
67
import com.wingjay.jianshi.util.UpgradeUtil;
78

89
/**
@@ -26,4 +27,14 @@ public boolean getVerticalWrite() {
2627
return getBoolean(KEY_VERTICAL_WRITE, false);
2728
}
2829

30+
private final static String KEY_GLOBAL_BACKGROUND_COLOR_RES = "global_background_color_res";
31+
32+
public void setBackgroundColor(int colorRes) {
33+
setInt(KEY_GLOBAL_BACKGROUND_COLOR_RES, colorRes);
34+
}
35+
36+
public int getBackgroundColor() {
37+
return getInt(KEY_GLOBAL_BACKGROUND_COLOR_RES, R.color.normal_bg);
38+
}
39+
2940
}

app/src/main/java/com/wingjay/jianshi/ui/DateChooseActivity.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.wingjay.jianshi.ui.widget.RedPointView;
1616
import com.wingjay.jianshi.ui.widget.ThreeLinePoemView;
1717
import com.wingjay.jianshi.ui.widget.VerticalTextView;
18+
import com.wingjay.jianshi.util.ConstantUtil;
1819
import com.wingjay.jianshi.util.DateUtil;
1920
import com.wingjay.jianshi.util.UpgradeUtil;
2021

@@ -104,7 +105,19 @@ public void onDayChoosed(DateTime chooseDate) {
104105

105106
@OnClick(R.id.setting)
106107
void toSettingsPage(View v) {
107-
startActivity(new Intent(DateChooseActivity.this, SettingActivity.class));
108+
Intent intent = new Intent(DateChooseActivity.this, SettingActivity.class);
109+
startActivityForResult(intent, ConstantUtil.REQUEST_CODE_BG_COLOR_CHANGE);
110+
}
111+
112+
@Override
113+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
114+
super.onActivityResult(requestCode, resultCode, data);
115+
if (requestCode == ConstantUtil.REQUEST_CODE_BG_COLOR_CHANGE) {
116+
if (resultCode == RESULT_OK) {
117+
setContainerBgColorFromPrefs();
118+
}
119+
}
120+
108121
}
109122

110123
@OnClick(R.id.day)

app/src/main/java/com/wingjay/jianshi/ui/SettingActivity.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import android.net.Uri;
55
import android.os.Bundle;
66
import android.support.v7.widget.SwitchCompat;
7-
import android.util.Log;
87
import android.view.View;
98
import android.widget.Toast;
109

1110
import com.wingjay.jianshi.R;
1211
import com.wingjay.jianshi.prefs.UserPrefs;
1312
import com.wingjay.jianshi.ui.base.BaseActivity;
13+
import com.wingjay.jianshi.ui.widget.BgColorPickDialogFragment;
1414

1515
import butterknife.InjectView;
1616
import butterknife.OnCheckedChanged;
@@ -24,6 +24,9 @@ public class SettingActivity extends BaseActivity {
2424
@InjectView(R.id.send_feedback)
2525
View sendFeedBack;
2626

27+
@InjectView(R.id.customize_bg_color)
28+
View customizeBgColor;
29+
2730
UserPrefs userPrefs;
2831

2932
@Override
@@ -59,4 +62,20 @@ void sendFeedback() {
5962
}
6063
}
6164

65+
@OnClick(R.id.customize_bg_color)
66+
void chooseBgColor() {
67+
BgColorPickDialogFragment bgColorPickDialogFragment = new BgColorPickDialogFragment();
68+
bgColorPickDialogFragment.setOnBackgroundColorChangedListener(
69+
new BgColorPickDialogFragment.OnBackgroundColorChangedListener() {
70+
@Override
71+
public void onBackgroundColorChanged(int newColorRes) {
72+
SettingActivity.this.setContainerBgColor(newColorRes);
73+
userPrefs.setBackgroundColor(newColorRes);
74+
setResult(RESULT_OK);
75+
}
76+
});
77+
bgColorPickDialogFragment.show(getSupportFragmentManager(), null);
78+
}
79+
80+
6281
}

app/src/main/java/com/wingjay/jianshi/ui/base/BaseActivity.java

+20
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
import android.support.annotation.LayoutRes;
55
import android.support.v7.app.AppCompatActivity;
66
import android.util.Log;
7+
import android.view.View;
8+
9+
10+
import com.wingjay.jianshi.R;
11+
import com.wingjay.jianshi.ui.theme.BackgroundColorHelper;
712

813
import butterknife.ButterKnife;
914

1015
public class BaseActivity extends AppCompatActivity {
1116

1217
protected boolean isVisible = false;
1318

19+
protected View containerView;
1420
protected String TAG = getClass().getSimpleName();
1521

1622
@Override
@@ -23,6 +29,20 @@ protected void onCreate(Bundle savedInstanceState) {
2329
public void setContentView(@LayoutRes int layoutResID) {
2430
super.setContentView(layoutResID);
2531
ButterKnife.inject(this);
32+
33+
containerView = findViewById(R.id.layout_container);
34+
setContainerBgColorFromPrefs();
35+
}
36+
37+
protected void setContainerBgColorFromPrefs() {
38+
if (containerView != null) {
39+
containerView.setBackgroundResource(BackgroundColorHelper.getBackgroundColorResFromPrefs(this));
40+
}
41+
}
42+
protected void setContainerBgColor(int colorRes) {
43+
if (containerView != null) {
44+
containerView.setBackgroundResource(colorRes);
45+
}
2646
}
2747

2848
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.wingjay.jianshi.ui.theme;
2+
3+
import android.content.Context;
4+
5+
import com.wingjay.jianshi.prefs.UserPrefs;
6+
7+
/**
8+
* Provide different color customized by user.
9+
*/
10+
public class BackgroundColorHelper {
11+
12+
public static int getBackgroundColorResFromPrefs(Context context) {
13+
UserPrefs userPrefs = new UserPrefs(context);
14+
return userPrefs.getBackgroundColor();
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.wingjay.jianshi.ui.theme;
2+
3+
import com.wingjay.jianshi.R;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
/**
9+
* Created by wingjay on 10/11/15.
10+
*/
11+
public enum TraditionalColorNamer {
12+
DEFAULT(R.color.normal_bg, "默认"),
13+
ZHU_QING(R.color.zhu_qing, "竹青"),
14+
CHI_JIN(R.color.chi_jin, "赤金"),
15+
QIAN_BAI(R.color.qian_bai, "铅白"),
16+
YING_BAI(R.color.ying_bai, "莹白"),
17+
SU(R.color.su, "素"),
18+
YUE_BAI(R.color.yue_bai, "月白"),
19+
SHUI_HONG(R.color.shui_hong, "水红"),
20+
CANG_HUANG(R.color.cang_huang, "苍黄"),
21+
DING_XIANG_SE(R.color.ding_xiang_se, "丁香色"),
22+
AI_LV(R.color.ai_lv, "艾绿"),
23+
YU_SE(R.color.yu_se, "玉色"),
24+
HUANG_LU(R.color.huang_lu, "黄栌"),
25+
JIANG_HUANG(R.color.jiang_huang, "姜黄");
26+
27+
private int colorRes;
28+
private String colorName;
29+
private static List<TraditionalColorNamer> colorNamerList;
30+
31+
TraditionalColorNamer(int res, String name) {
32+
this.colorRes = res;
33+
this.colorName = name;
34+
}
35+
36+
static {
37+
TraditionalColorNamer[] list = {
38+
DEFAULT, ZHU_QING, CHI_JIN, QIAN_BAI, YING_BAI, SU, YUE_BAI, SHUI_HONG,
39+
CANG_HUANG, DING_XIANG_SE, AI_LV, YU_SE, HUANG_LU, JIANG_HUANG
40+
};
41+
colorNamerList = Arrays.asList(list);
42+
}
43+
44+
public String getColorName() {
45+
return this.colorName;
46+
}
47+
48+
public int getColorRes() {
49+
return this.colorRes;
50+
}
51+
52+
public static List<TraditionalColorNamer> getAllColorNamer() {
53+
return colorNamerList;
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.wingjay.jianshi.ui.widget;
2+
3+
import android.content.Context;
4+
import android.os.Bundle;
5+
import android.support.annotation.Nullable;
6+
import android.support.v4.app.DialogFragment;
7+
import android.view.LayoutInflater;
8+
import android.view.View;
9+
import android.view.ViewGroup;
10+
import android.widget.BaseAdapter;
11+
import android.widget.ListView;
12+
import android.widget.TextView;
13+
14+
import com.wingjay.jianshi.R;
15+
import com.wingjay.jianshi.ui.theme.TraditionalColorNamer;
16+
17+
import java.util.List;
18+
19+
/**
20+
* Created by wingjay on 10/11/15.
21+
*/
22+
public class BgColorPickDialogFragment extends DialogFragment {
23+
24+
private ListView colorListView;
25+
26+
private OnBackgroundColorChangedListener onBackgroundColorChangedListener;
27+
28+
@Nullable
29+
@Override
30+
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
31+
View root = inflater.inflate(R.layout.fragment_bg_color_pick, container, false);
32+
getDialog().setTitle(getResources().getString(R.string.please_choose_bg_color));
33+
colorListView = (ListView) root.findViewById(R.id.bg_color_list_view);
34+
colorListView.setAdapter(new BgColorPickAdapter(getActivity()));
35+
36+
return root;
37+
}
38+
39+
private class BgColorPickAdapter extends BaseAdapter{
40+
41+
private class ViewHolder {
42+
RedPointView colorHint;
43+
TextView colorName;
44+
}
45+
46+
private List<TraditionalColorNamer> colorNamerList;
47+
private Context context;
48+
49+
public BgColorPickAdapter(Context context) {
50+
this.context = context;
51+
this.colorNamerList = TraditionalColorNamer.getAllColorNamer();
52+
}
53+
54+
@Override
55+
public int getCount() {
56+
return colorNamerList.size();
57+
}
58+
59+
@Override
60+
public Object getItem(int position) {
61+
return colorNamerList.get(position);
62+
}
63+
64+
@Override
65+
public long getItemId(int position) {
66+
return position;
67+
}
68+
69+
@Override
70+
public View getView(final int position, View convertView, ViewGroup parent) {
71+
ViewHolder viewHolder;
72+
if (convertView == null) {
73+
convertView = LayoutInflater.from(context)
74+
.inflate(R.layout.view_bg_color_pick, parent, false);
75+
viewHolder = new ViewHolder();
76+
viewHolder.colorHint = (RedPointView) convertView.findViewById(R.id.bg_color_hint);
77+
viewHolder.colorName = (TextView) convertView.findViewById(R.id.bg_color_name);
78+
convertView.setTag(viewHolder);
79+
} else {
80+
viewHolder = (ViewHolder) convertView.getTag();
81+
}
82+
83+
final TraditionalColorNamer colorNamer = colorNamerList.get(position);
84+
viewHolder.colorName.setText(colorNamer.getColorName());
85+
viewHolder.colorHint.setContainerBackgroundColor(colorNamer.getColorRes());
86+
convertView.setOnClickListener(new View.OnClickListener() {
87+
@Override
88+
public void onClick(View v) {
89+
dismiss();
90+
if (onBackgroundColorChangedListener != null) {
91+
onBackgroundColorChangedListener.onBackgroundColorChanged(
92+
colorNamer.getColorRes());
93+
}
94+
}
95+
});
96+
return convertView;
97+
}
98+
99+
}
100+
101+
public void setOnBackgroundColorChangedListener(
102+
OnBackgroundColorChangedListener onBackgroundColorChangedListener) {
103+
this.onBackgroundColorChangedListener = onBackgroundColorChangedListener;
104+
}
105+
106+
public interface OnBackgroundColorChangedListener {
107+
void onBackgroundColorChanged(int newColorRes);
108+
}
109+
110+
}

app/src/main/java/com/wingjay/jianshi/ui/widget/RedPointView.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import android.content.Context;
44
import android.content.res.TypedArray;
5+
import android.graphics.drawable.GradientDrawable;
56
import android.util.AttributeSet;
67
import android.view.LayoutInflater;
8+
import android.view.View;
79
import android.widget.FrameLayout;
810
import android.widget.TextView;
911

@@ -14,26 +16,37 @@
1416
*/
1517
public class RedPointView extends FrameLayout {
1618

19+
private View containerView;
1720
private TextView textView;
1821

1922
public RedPointView(Context context, AttributeSet attrs) {
2023
super(context, attrs);
2124
LayoutInflater.from(context).inflate(R.layout.view_red_point, this);
2225
textView = (TextView) findViewById(R.id.pointer);
26+
containerView = findViewById(R.id.red_point_container);
2327
TypedArray typedArray = context.getTheme()
2428
.obtainStyledAttributes(attrs, R.styleable.RedPointView, 0, 0);
2529
try {
2630
String text = typedArray.getString(R.styleable.RedPointView_text);
2731
setText(text);
32+
int colorRes = typedArray.getInt(R.styleable.RedPointView_redPointViewBgColor,
33+
R.color.bright_red);
34+
setContainerBackgroundColor(colorRes);
2835
} finally {
2936
typedArray.recycle();
3037
}
3138
}
3239

3340
public void setText(String text) {
34-
if (text.length() != 1) {
41+
if (text.length() > 1) {
3542
return;
3643
}
3744
textView.setText(text);
3845
}
46+
47+
public void setContainerBackgroundColor(int colorRes) {
48+
GradientDrawable drawable = (GradientDrawable) containerView.getBackground();
49+
drawable.setColor(getResources().getColor(colorRes));
50+
}
51+
3952
}

app/src/main/java/com/wingjay/jianshi/util/ConstantUtil.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
public class ConstantUtil {
77

88
public final static int REQUEST_CODE_VIEW_DIARY_FROM_LIST = 100;
9-
9+
public final static int REQUEST_CODE_BG_COLOR_CHANGE = 200;
1010

1111
}

app/src/main/res/layout/activity_base.xml

-16
This file was deleted.

0 commit comments

Comments
 (0)