-
Notifications
You must be signed in to change notification settings - Fork 138
/
MaterialAboutActivity.java
163 lines (133 loc) · 5.1 KB
/
MaterialAboutActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.danielstone.materialaboutlibrary;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.appbar.AppBarLayout;
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import androidx.appcompat.widget.Toolbar;
import android.util.TypedValue;
import android.view.MenuItem;
import com.danielstone.materialaboutlibrary.adapters.MaterialAboutListAdapter;
import com.danielstone.materialaboutlibrary.model.MaterialAboutList;
import com.danielstone.materialaboutlibrary.util.DefaultViewTypeManager;
import com.danielstone.materialaboutlibrary.util.ViewTypeManager;
import java.lang.ref.WeakReference;
public abstract class MaterialAboutActivity extends AppCompatActivity {
private MaterialAboutList list = new MaterialAboutList.Builder().build();
private RecyclerView recyclerView;
private MaterialAboutListAdapter adapter;
@NonNull
protected abstract MaterialAboutList getMaterialAboutList(@NonNull Context context);
@Nullable
protected abstract CharSequence getActivityTitle();
/**
* Get a reference to the recyclerview in case a snackbar needs to be displayed
* @return
*/
public RecyclerView getRecyclerView() {
return recyclerView;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mal_material_about_content);
CharSequence title = getActivityTitle();
if (title == null)
setTitle(R.string.mal_title_about);
else
setTitle(title);
assignViews();
initViews();
ListTask task = new ListTask(this);
task.execute();
}
private void assignViews() {
recyclerView = (RecyclerView) findViewById(R.id.mal_recyclerview);
recyclerView.setAlpha(0f);
recyclerView.setTranslationY(20);
}
private void initViews() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
adapter = new MaterialAboutListAdapter(getViewTypeManager());
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
RecyclerView.ItemAnimator animator = recyclerView.getItemAnimator();
if (animator instanceof SimpleItemAnimator) {
((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
}
}
@NonNull
protected ViewTypeManager getViewTypeManager() {
return new DefaultViewTypeManager();
}
@NonNull
protected MaterialAboutList getList() {
return list;
}
protected boolean shouldAnimate() {
return true;
}
protected void refreshMaterialAboutList() {
setMaterialAboutList(list);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
private void onTaskFinished(@Nullable MaterialAboutList materialAboutList) {
if (materialAboutList != null) {
list = materialAboutList;
adapter.setData(list.getCards());
if (shouldAnimate()) {
recyclerView.animate()
.alpha(1f)
.translationY(0f)
.setDuration(600)
.setInterpolator(new FastOutSlowInInterpolator()).start();
} else {
recyclerView.setAlpha(1f);
recyclerView.setTranslationY(0f);
}
} else {
finish();//?? why we remain here anyway?
}
}
protected void setMaterialAboutList(MaterialAboutList materialAboutList) {
list = materialAboutList;
adapter.setData(list.getCards());
}
private static class ListTask extends AsyncTask<String, String, MaterialAboutList> {
private WeakReference<MaterialAboutActivity> context;
ListTask(MaterialAboutActivity context) {
this.context = new WeakReference<>(context);
}
@Override
protected MaterialAboutList doInBackground(String... params) {
return isCancelled() || context.get() == null ? null : context.get().getMaterialAboutList(context.get());
}
@Override
protected void onPostExecute(MaterialAboutList materialAboutList) {
super.onPostExecute(materialAboutList);
if (context.get() != null) {
if (!context.get().isFinishing()) {
context.get().onTaskFinished(materialAboutList);
}
}
context = null;
}
}
}