Skip to content

Commit 7111da0

Browse files
committed
Add support for theme presets
Dynamic theme 1.1.0. Update other dependencies. Improve layout and styles.
1 parent 1b8e6e3 commit 7111da0

File tree

18 files changed

+809
-82
lines changed

18 files changed

+809
-82
lines changed

build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ buildscript {
2121
'targetSdk' : 29,
2222
'buildTools' : '29.0.3',
2323
'drawerlayout': '1.1.0-alpha03',
24-
'dynamic' : '3.0.1',
24+
'dynamic' : '3.1.0',
2525
'fragment' : '1.2.0',
2626
'kotlin' : '1.3.61',
27+
'locale' : '1.0.1',
2728
'material' : '1.2.0-alpha04',
28-
'preference' : '1.1.0',
29-
'support' : '1.0.0',
29+
'preferences' : '1.1.0',
3030
'swiperefresh': '1.1.0-alpha03',
31-
'theme' : '1.0.2',
31+
'theme' : '1.1.0',
3232
'work' : '2.3.0'
3333
]
3434

dynamic-support/build.gradle

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ android {
4242

4343
dependencies {
4444
api "com.pranavpandey.android:dynamic-theme:${versions.theme}"
45-
api "com.pranavpandey.android:dynamic-locale:${versions.support}"
46-
api "com.pranavpandey.android:dynamic-preferences:${versions.support}"
45+
api "com.pranavpandey.android:dynamic-locale:${versions.locale}"
46+
api "com.pranavpandey.android:dynamic-preferences:${versions.preferences}"
4747
api "com.pranavpandey.android:dynamic-toasts:${versions.dynamic}"
4848
api "androidx.drawerlayout:drawerlayout:${versions.drawerlayout}"
4949
api "androidx.fragment:fragment:${versions.fragment}"
50-
api "androidx.preference:preference:${versions.preference}"
5150
api "androidx.swiperefreshlayout:swiperefreshlayout:${versions.swiperefresh}"
5251
api "androidx.work:work-runtime:${versions.work}"
5352
api "com.google.android.material:material:${versions.material}"

dynamic-support/src/main/java/com/pranavpandey/android/dynamic/support/activity/DynamicActivity.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -637,18 +637,6 @@ public void setAppBarShadowVisible(boolean visible) {
637637
*/
638638
public void addView(@Nullable ViewGroup viewGroup,
639639
@Nullable View view, boolean removePrevious) {
640-
if (viewGroup == null) {
641-
return;
642-
}
643-
644-
if (view == null) {
645-
if (removePrevious) {
646-
viewGroup.removeAllViews();
647-
}
648-
649-
return;
650-
}
651-
652640
DynamicViewUtils.addView(viewGroup, view, removePrevious);
653641
setFrameVisibility(viewGroup);
654642
}

dynamic-support/src/main/java/com/pranavpandey/android/dynamic/support/fragment/DynamicViewPager2Fragment.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ public abstract class DynamicViewPager2Fragment extends DynamicFragment
5757
*/
5858
private TabLayout mTabLayout;
5959

60-
@Override
61-
public void onCreate(@Nullable Bundle savedInstanceState) {
62-
super.onCreate(savedInstanceState);
63-
64-
setRetainInstance(false);
65-
}
66-
6760
@Override
6861
public View onCreateView(@NonNull LayoutInflater inflater,
6962
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

dynamic-support/src/main/java/com/pranavpandey/android/dynamic/support/model/DynamicAppTheme.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,10 +871,19 @@ public int getCornerRadius() {
871871
}
872872

873873
@Override
874-
public int getCornerSizeDp() {
874+
public int getCornerSizeDp(boolean resolve) {
875+
if (!resolve && cornerRadius == AUTO) {
876+
return AUTO;
877+
}
878+
875879
return DynamicUnitUtils.convertPixelsToDp(getCornerRadius());
876880
}
877881

882+
@Override
883+
public int getCornerSizeDp() {
884+
return getCornerSizeDp(true);
885+
}
886+
878887
@Override
879888
public @NonNull DynamicAppTheme setCornerRadius(int cornerRadius) {
880889
this.cornerRadius = cornerRadius;

dynamic-support/src/main/java/com/pranavpandey/android/dynamic/support/permission/fragment/DynamicPermissionsFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ public void onPermissionSelected(@NonNull View view, int position,
309309
* Show hint if permission request is cancelled by the user.
310310
*/
311311
private void showHint() {
312-
getDynamicActivity().getSnackBar(
313-
R.string.ads_perm_info_grant_all, Snackbar.LENGTH_LONG).show();
312+
getDynamicActivity().getSnackBar(R.string.ads_perm_info_grant_all,
313+
Snackbar.LENGTH_LONG).show();
314314
}
315315

316316
/**

dynamic-support/src/main/java/com/pranavpandey/android/dynamic/support/theme/DynamicLayoutInflater.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public void run() {
137137

138138
final Drawable icon = ((ListMenuItemView) menuItemView)
139139
.getItemData().getIcon();
140+
140141
if (icon != null) {
141142
((ListMenuItemView) menuItemView).setIcon(
142143
DynamicDrawableUtils.colorizeDrawable(icon, tintColor));

dynamic-support/src/main/java/com/pranavpandey/android/dynamic/support/theme/DynamicTheme.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -945,11 +945,11 @@ public void deleteLocalTheme(@NonNull Context context) {
945945
}
946946

947947
/**
948-
* Returns the dynamic app theme from the Json string.
948+
* Returns the dynamic app theme from the JSON string.
949949
*
950-
* @param theme The dynamic app theme Json string to be converted.
950+
* @param theme The dynamic app theme JSON string to be converted.
951951
*
952-
* @return The dynamic app theme from the Json string.
952+
* @return The dynamic app theme from the JSON string.
953953
*/
954954
public @Nullable DynamicAppTheme getTheme(@Nullable String theme) {
955955
return new Gson().fromJson(theme, DynamicAppTheme.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
/*
2+
* Copyright 2019 Pranav Pandey
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.pranavpandey.android.dynamic.support.theme.adapter;
18+
19+
import android.content.Context;
20+
import android.database.Cursor;
21+
import android.view.LayoutInflater;
22+
import android.view.View;
23+
import android.view.ViewGroup;
24+
25+
import androidx.annotation.IntDef;
26+
import androidx.annotation.LayoutRes;
27+
import androidx.annotation.NonNull;
28+
import androidx.annotation.Nullable;
29+
import androidx.recyclerview.widget.RecyclerView;
30+
31+
import com.pranavpandey.android.dynamic.support.R;
32+
import com.pranavpandey.android.dynamic.support.model.DynamicAppTheme;
33+
import com.pranavpandey.android.dynamic.support.theme.view.DynamicPresetsView;
34+
import com.pranavpandey.android.dynamic.support.theme.view.ThemePreview;
35+
import com.pranavpandey.android.dynamic.support.widget.DynamicCardView;
36+
import com.pranavpandey.android.dynamic.theme.ThemeContract;
37+
import com.pranavpandey.android.dynamic.theme.utils.DynamicThemeUtils;
38+
39+
import static com.pranavpandey.android.dynamic.support.theme.adapter.DynamicPresetsAdapter.Type.HORIZONTAL;
40+
import static com.pranavpandey.android.dynamic.support.theme.adapter.DynamicPresetsAdapter.Type.VERTICAL;
41+
42+
/**
43+
* A recycler view adapter to show the theme presets.
44+
*/
45+
public class DynamicPresetsAdapter<T extends DynamicAppTheme>
46+
extends RecyclerView.Adapter<DynamicPresetsAdapter.ViewHolder<T>> {
47+
48+
/**
49+
* Interface to hold the presets layout according to the recycler view orientation.
50+
*/
51+
@IntDef(value = { VERTICAL, HORIZONTAL })
52+
public @interface Type {
53+
54+
/**
55+
* Constant for the vertical orientation.
56+
*/
57+
int VERTICAL = 1;
58+
59+
/**
60+
* Constant for the horizontal orientation.
61+
*/
62+
int HORIZONTAL = 2;
63+
}
64+
65+
/**
66+
* Layout inflater used by this adapter.
67+
*/
68+
private final LayoutInflater mInflater;
69+
70+
/**
71+
* Cursor for presets handled by this adapter.
72+
*/
73+
private Cursor mPresets;
74+
75+
/**
76+
* Type of the preset layout used by this adapter.
77+
*/
78+
private @Type int mType;
79+
80+
/**
81+
* Layout resource containing the theme preview.
82+
*
83+
* @see ThemePreview
84+
*/
85+
private @LayoutRes int mLayoutRes;
86+
87+
/**
88+
* Listener to handle the preset events.
89+
*/
90+
private DynamicPresetsView.DynamicPresetsListener<T> mDynamicPresetsListener;
91+
92+
/**
93+
* Constructor to initialize an object of this class.
94+
*
95+
* @param context The context to get the layout inflater.
96+
* @param type The type of the preset layout.
97+
*/
98+
public DynamicPresetsAdapter(@NonNull Context context, @Type int type) {
99+
this(context, type, type == VERTICAL ? R.layout.ads_layout_item_preset
100+
: R.layout.ads_layout_item_preset_horizontal);
101+
}
102+
103+
/**
104+
* Constructor to initialize an object of this class.
105+
*
106+
* @param context The context to get the layout inflater.
107+
* @param type The type of the preset layout.
108+
* @param layoutRes The layout resource containing the theme preview.
109+
*
110+
* @see ThemePreview
111+
*/
112+
public DynamicPresetsAdapter(@NonNull Context context,
113+
@Type int type, @LayoutRes int layoutRes) {
114+
this.mInflater = LayoutInflater.from(context);
115+
this.mType = type;
116+
this.mLayoutRes = layoutRes;
117+
}
118+
119+
@Override
120+
public @NonNull ViewHolder<T> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
121+
return new ViewHolder<>(mInflater.inflate(mLayoutRes, parent, false));
122+
}
123+
124+
@Override
125+
public void onBindViewHolder(@NonNull ViewHolder<T> holder, int position) {
126+
if (mPresets != null) {
127+
holder.getRoot().setVisibility(View.VISIBLE);
128+
try {
129+
if (mDynamicPresetsListener != null) {
130+
T theme = null;
131+
if (mPresets.moveToPosition(position)) {
132+
String decodeTheme = DynamicThemeUtils.decodeTheme(
133+
mPresets.getString(mPresets.getColumnIndexOrThrow(
134+
ThemeContract.Preset.Column.THEME)));
135+
136+
if (decodeTheme != null) {
137+
theme = mDynamicPresetsListener.getDynamicTheme(decodeTheme);
138+
}
139+
}
140+
141+
if (theme == null) {
142+
return;
143+
}
144+
145+
holder.getThemePreview().getActionView()
146+
.setImageResource(R.drawable.ads_ic_palette);
147+
holder.getThemePreview().setDynamicTheme(theme);
148+
149+
if (holder.getRoot() instanceof DynamicCardView) {
150+
((DynamicCardView) holder.getRoot()).setCorner(
151+
(float) theme.getCornerRadius());
152+
}
153+
154+
holder.getRoot().setOnClickListener(new View.OnClickListener() {
155+
@Override
156+
public void onClick(View v) {
157+
mDynamicPresetsListener.onPresetClick(v,
158+
holder.getThemePreview().getDynamicTheme().toDynamicString(),
159+
holder.getThemePreview());
160+
}
161+
});
162+
holder.getThemePreview().getActionView().setOnClickListener(
163+
new View.OnClickListener() {
164+
@Override
165+
public void onClick(View v) {
166+
mDynamicPresetsListener.onPresetClick(v,
167+
holder.getThemePreview().getDynamicTheme().toDynamicString(),
168+
holder.getThemePreview());
169+
}
170+
});
171+
} else {
172+
holder.getRoot().setClickable(false);
173+
holder.getThemePreview().getActionView().setClickable(false);
174+
}
175+
} catch (Exception ignored) {
176+
}
177+
} else {
178+
holder.getRoot().setVisibility(View.GONE);
179+
}
180+
}
181+
182+
@Override
183+
public int getItemCount() {
184+
return mPresets == null ? 0 : mPresets.getCount();
185+
}
186+
187+
/**
188+
* Sets the theme presets for this adapter.
189+
*
190+
* @param presets The theme presets to be set.
191+
*/
192+
public void setPresets(@Nullable Cursor presets) {
193+
mPresets = presets;
194+
195+
notifyDataSetChanged();
196+
}
197+
198+
/**
199+
* Returns the layout resource containing the theme preview.
200+
*
201+
* @return The layout resource containing the theme preview.
202+
*/
203+
public int getLayoutRes() {
204+
return mLayoutRes;
205+
}
206+
207+
/**
208+
* Get the dynamic preset listener used by this adapter.
209+
*
210+
* @return The dynamic preset listener used by this adapter.
211+
*/
212+
public @Nullable DynamicPresetsView.DynamicPresetsListener<T> getDynamicPresetsListener() {
213+
return mDynamicPresetsListener;
214+
}
215+
216+
/**
217+
* Sets the dynamic preset listener for this adapter.
218+
*/
219+
public void setDynamicPresetsListener(
220+
@Nullable DynamicPresetsView.DynamicPresetsListener<T> dynamicPresetsListener) {
221+
this.mDynamicPresetsListener = dynamicPresetsListener;
222+
223+
notifyDataSetChanged();
224+
}
225+
226+
/**
227+
* View holder class to hold the preset view.
228+
*/
229+
static class ViewHolder<T extends DynamicAppTheme> extends RecyclerView.ViewHolder {
230+
231+
/**
232+
* Item view root.
233+
*/
234+
private final ViewGroup root;
235+
236+
/**
237+
* Theme preview to display the preset.
238+
*/
239+
private final ThemePreview<T> themePreview;
240+
241+
/**
242+
* Constructor to initialize views from the supplied layout.
243+
*
244+
* @param view The view for this view holder.
245+
*/
246+
ViewHolder(View view) {
247+
super(view);
248+
249+
root = view.findViewById(R.id.ads_preset_root);
250+
themePreview = view.findViewById(R.id.ads_preset_theme_preview);
251+
}
252+
253+
/**
254+
* Get the item view root.
255+
*
256+
* @return The item view root.
257+
*/
258+
ViewGroup getRoot() {
259+
return root;
260+
}
261+
262+
/**
263+
* Get the theme preview to display the preset.
264+
*
265+
* @return The theme preview to display the preset.
266+
*/
267+
ThemePreview<T> getThemePreview() {
268+
return themePreview;
269+
}
270+
}
271+
}

0 commit comments

Comments
 (0)