Skip to content

Commit 3f5ff9f

Browse files
committed
Implement motion for presets view
1 parent dae0637 commit 3f5ff9f

File tree

1 file changed

+105
-48
lines changed

1 file changed

+105
-48
lines changed

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

Lines changed: 105 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2022 Pranav Pandey
2+
* Copyright 2018-2024 Pranav Pandey
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,11 +40,11 @@
4040
import com.pranavpandey.android.dynamic.support.Dynamic;
4141
import com.pranavpandey.android.dynamic.support.R;
4242
import com.pranavpandey.android.dynamic.support.model.DynamicAppTheme;
43+
import com.pranavpandey.android.dynamic.support.motion.DynamicMotion;
4344
import com.pranavpandey.android.dynamic.support.permission.DynamicPermissions;
4445
import com.pranavpandey.android.dynamic.support.recyclerview.DynamicRecyclerViewNested;
4546
import com.pranavpandey.android.dynamic.support.theme.adapter.DynamicPresetsAdapter;
4647
import com.pranavpandey.android.dynamic.support.util.DynamicLayoutUtils;
47-
import com.pranavpandey.android.dynamic.support.view.DynamicHeader;
4848
import com.pranavpandey.android.dynamic.support.view.base.DynamicItemView;
4949
import com.pranavpandey.android.dynamic.theme.Theme;
5050
import com.pranavpandey.android.dynamic.theme.ThemeContract;
@@ -157,6 +157,14 @@ protected void initialize() {
157157
mViewInfo = findViewById(R.id.ads_theme_presets_info_card);
158158
mInfo = findViewById(R.id.ads_theme_presets_info);
159159

160+
Dynamic.setOnClickListener(findViewById(R.id.ads_theme_presets_header),
161+
new OnClickListener() {
162+
@Override
163+
public void onClick(View v) {
164+
loadPresets();
165+
}
166+
});
167+
160168
Dynamic.setOnClickListener(findViewById(R.id.ads_theme_presets_info),
161169
new OnClickListener() {
162170
@Override
@@ -175,7 +183,7 @@ public void onClick(View v) {
175183
}
176184
});
177185

178-
Dynamic.setColorType(((DynamicHeader) findViewById(
186+
Dynamic.setColorType(((DynamicItemView) findViewById(
179187
R.id.ads_theme_presets_header)).getIconView(), Theme.ColorType.NONE);
180188
}
181189

@@ -206,6 +214,16 @@ protected void onDetachedFromWindow() {
206214
getContext(), LinearLayoutManager.HORIZONTAL);
207215
}
208216

217+
@Override
218+
public void showProgress(boolean animate) {
219+
super.showProgress(false);
220+
}
221+
222+
@Override
223+
public void hideProgress(boolean animate) {
224+
super.hideProgress(false);
225+
}
226+
209227
/**
210228
* Returns the type for the presets adapter.
211229
*
@@ -242,34 +260,55 @@ public void setPresetsAdapter(@Nullable Fragment owner, @LayoutRes int layout,
242260
loadPresets();
243261
}
244262

245-
public void loadPresets() {
246-
if (isPackageExists()) {
247-
if (isPermissionGranted()) {
248-
setPresetsVisible(true);
263+
/**
264+
* Runnable to load the theme presets.
265+
*/
266+
private final Runnable mLoadPresets = new Runnable() {
267+
@Override
268+
public void run() {
269+
if (isPackageExists()) {
270+
if (isPermissionGranted()) {
271+
setPresetsVisible(true);
272+
} else {
273+
mInfo.setSubtitle(getContext().getString(
274+
R.string.ads_permissions_subtitle_single));
275+
setPresetsVisible(false);
276+
}
249277
} else {
250-
mInfo.setSubtitle(getContext().getString(
251-
R.string.ads_permissions_subtitle_single));
278+
mInfo.setSubtitle(String.format(
279+
getContext().getString(R.string.ads_theme_presets_desc_app),
280+
getContext().getString(R.string.ads_theme_presets_app)));
252281
setPresetsVisible(false);
253282
}
254-
} else {
255-
mInfo.setSubtitle(getContext().getString(R.string.ads_theme_presets_desc));
256-
setPresetsVisible(false);
257-
}
258283

259-
if (mLifecycleOwner == null || !isPermissionGranted()) {
260-
return;
261-
}
284+
if (mLifecycleOwner == null || !isPermissionGranted()) {
285+
return;
286+
}
287+
288+
if (mLoader == null) {
289+
mLoader = LoaderManager.getInstance(mLifecycleOwner).initLoader(
290+
ADS_LOADER_PRESETS, null, mLoaderCallbacks);
291+
}
262292

263-
if (mLoader == null) {
264-
mLoader = LoaderManager.getInstance(mLifecycleOwner).initLoader(
265-
ADS_LOADER_PRESETS, null, mLoaderCallbacks);
293+
if (mLoader.isStarted()) {
294+
mLoader.stopLoading();
295+
mLoader.forceLoad();
296+
} else {
297+
mLoader.startLoading();
298+
}
266299
}
300+
};
267301

268-
if (mLoader.isStarted()) {
269-
mLoader.forceLoad();
270-
} else {
271-
mLoader.startLoading();
302+
/**
303+
* Try to load the theme presets.
304+
*/
305+
public void loadPresets() {
306+
if (DynamicMotion.getInstance().isMotion()) {
307+
DynamicMotion.getInstance().beginDelayedTransition((ViewGroup) getParent());
272308
}
309+
310+
post(mLoadPresets);
311+
postDelayed(mLoadPresets, DynamicMotion.Duration.SHORT);
273312
}
274313

275314
/**
@@ -344,8 +383,14 @@ public boolean isPermissionGranted() {
344383
* Sets the visibility of the presets view.
345384
*
346385
* @param visible {@code true} to make the header and recycler view visible.
386+
* @param data The presets data to be set.
347387
*/
348-
private void setPresetsVisible(boolean visible) {
388+
private void setPresetsVisible(final boolean visible, @Nullable Cursor data) {
389+
if (DynamicMotion.getInstance().isMotion()) {
390+
DynamicMotion.getInstance().beginDelayedTransition(
391+
(ViewGroup) DynamicPresetsView.this.getParent());
392+
}
393+
349394
if (visible) {
350395
Dynamic.setVisibility(mHeader, VISIBLE);
351396
Dynamic.setVisibility(mViewInfo, GONE);
@@ -354,6 +399,19 @@ private void setPresetsVisible(boolean visible) {
354399
Dynamic.setVisibility(mHeader, GONE);
355400
Dynamic.setVisibility(getRecyclerView(), GONE);
356401
}
402+
403+
mPresetsAdapter.setPresets(data);
404+
}
405+
406+
/**
407+
* Sets the visibility of the presets view.
408+
*
409+
* @param visible {@code true} to make the header and recycler view visible.
410+
*
411+
* @see #setPresetsVisible(boolean, Cursor)
412+
*/
413+
private void setPresetsVisible(boolean visible) {
414+
setPresetsVisible(visible, null);
357415
}
358416

359417
/**
@@ -363,43 +421,42 @@ private void setPresetsVisible(boolean visible) {
363421
new LoaderManager.LoaderCallbacks<Cursor>() {
364422
@Override
365423
public @NonNull Loader<Cursor> onCreateLoader(int id, @Nullable Bundle args) {
366-
if (id == ADS_LOADER_PRESETS) {
367-
if (isPermissionGranted()) {
368-
try {
369-
showProgress();
370-
return new CursorLoader(getContext().getApplicationContext(),
371-
ThemeContract.Preset.CONTENT_URI,
372-
new String[] { ThemeContract.Preset.Column.THEME },
373-
null, null, null);
374-
} catch (Exception e) {
375-
e.printStackTrace();
376-
}
377-
}
424+
if (id != ADS_LOADER_PRESETS) {
425+
throw new IllegalArgumentException();
426+
}
378427

379-
return new Loader<>(getContext().getApplicationContext());
428+
if (isPermissionGranted()) {
429+
try {
430+
setPresetsVisible(false);
431+
432+
return new CursorLoader(getContext().getApplicationContext(),
433+
ThemeContract.Preset.CONTENT_URI,
434+
new String[] { ThemeContract.Preset.Column.THEME },
435+
null, null, null);
436+
} catch (Exception e) {
437+
e.printStackTrace();
438+
}
380439
}
381440

382-
throw new IllegalArgumentException();
441+
return new Loader<>(getContext().getApplicationContext());
383442
}
384443

385444
@Override
386445
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor data) {
387-
if (loader.getId() == ADS_LOADER_PRESETS) {
388-
if (data != null) {
389-
hideProgress();
390-
mPresetsAdapter.setPresets(data);
391-
}
392-
393-
setPresetsVisible(data != null && data.getCount() > 0);
446+
if (loader.getId() != ADS_LOADER_PRESETS) {
447+
return;
394448
}
449+
450+
setPresetsVisible(data != null && data.getCount() > 0, data);
395451
}
396452

397453
@Override
398454
public void onLoaderReset(@NonNull Loader<Cursor> loader) {
399-
if (loader.getId() == ADS_LOADER_PRESETS) {
400-
mPresetsAdapter.setPresets(null);
401-
setPresetsVisible(false);
455+
if (loader.getId() != ADS_LOADER_PRESETS) {
456+
return;
402457
}
458+
459+
setPresetsVisible(false, null);
403460
}
404461
};
405462
}

0 commit comments

Comments
 (0)