This repository was archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathArticleDetailFragment.java
More file actions
executable file
·304 lines (261 loc) · 11.3 KB
/
ArticleDetailFragment.java
File metadata and controls
executable file
·304 lines (261 loc) · 11.3 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package com.example.xyzreader.ui;
import android.app.Fragment;
import android.app.LoaderManager;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import android.os.Bundle;
import androidx.core.app.ShareCompat;
import androidx.palette.graphics.Palette;
import android.text.Html;
import android.text.format.DateUtils;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.xyzreader.R;
import com.example.xyzreader.data.ArticleLoader;
/**
* A fragment representing a single Article detail screen. This fragment is
* either contained in a {@link ArticleListActivity} in two-pane mode (on
* tablets) or a {@link ArticleDetailActivity} on handsets.
*/
public class ArticleDetailFragment extends Fragment implements
LoaderManager.LoaderCallbacks<Cursor> {
private static final String TAG = "ArticleDetailFragment";
public static final String ARG_ITEM_ID = "item_id";
private static final float PARALLAX_FACTOR = 1.25f;
private Cursor mCursor;
private long mItemId;
private View mRootView;
private int mMutedColor = 0xFF333333;
private ObservableScrollView mScrollView;
private DrawInsetsFrameLayout mDrawInsetsFrameLayout;
private ColorDrawable mStatusBarColorDrawable;
private int mTopInset;
private View mPhotoContainerView;
private ImageView mPhotoView;
private int mScrollY;
private boolean mIsCard = false;
private int mStatusBarFullOpacityBottom;
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss");
// Use default locale format
private SimpleDateFormat outputFormat = new SimpleDateFormat();
// Most time functions can only handle 1902 - 2037
private GregorianCalendar START_OF_EPOCH = new GregorianCalendar(2,1,1);
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ArticleDetailFragment() {
}
public static ArticleDetailFragment newInstance(long itemId) {
Bundle arguments = new Bundle();
arguments.putLong(ARG_ITEM_ID, itemId);
ArticleDetailFragment fragment = new ArticleDetailFragment();
fragment.setArguments(arguments);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
mItemId = getArguments().getLong(ARG_ITEM_ID);
}
mIsCard = getResources().getBoolean(R.bool.detail_is_card);
mStatusBarFullOpacityBottom = getResources().getDimensionPixelSize(
R.dimen.detail_card_top_margin);
setHasOptionsMenu(true);
}
public ArticleDetailActivity getActivityCast() {
return (ArticleDetailActivity) getActivity();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// In support library r8, calling initLoader for a fragment in a FragmentPagerAdapter in
// the fragment's onCreate may cause the same LoaderManager to be dealt to multiple
// fragments because their mIndex is -1 (haven't been added to the activity yet). Thus,
// we do this in onActivityCreated.
getLoaderManager().initLoader(0, null, this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_article_detail, container, false);
mDrawInsetsFrameLayout = (DrawInsetsFrameLayout)
mRootView.findViewById(R.id.draw_insets_frame_layout);
mDrawInsetsFrameLayout.setOnInsetsCallback(new DrawInsetsFrameLayout.OnInsetsCallback() {
@Override
public void onInsetsChanged(Rect insets) {
mTopInset = insets.top;
}
});
mScrollView = (ObservableScrollView) mRootView.findViewById(R.id.scrollview);
mScrollView.setCallbacks(new ObservableScrollView.Callbacks() {
@Override
public void onScrollChanged() {
mScrollY = mScrollView.getScrollY();
getActivityCast().onUpButtonFloorChanged(mItemId, ArticleDetailFragment.this);
mPhotoContainerView.setTranslationY((int) (mScrollY - mScrollY / PARALLAX_FACTOR));
updateStatusBar();
}
});
mPhotoView = (ImageView) mRootView.findViewById(R.id.photo);
mPhotoContainerView = mRootView.findViewById(R.id.photo_container);
mStatusBarColorDrawable = new ColorDrawable(0);
mRootView.findViewById(R.id.share_fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(Intent.createChooser(ShareCompat.IntentBuilder.from(getActivity())
.setType("text/plain")
.setText("Some sample text")
.getIntent(), getString(R.string.action_share)));
}
});
bindViews();
updateStatusBar();
return mRootView;
}
private void updateStatusBar() {
int color = 0;
if (mPhotoView != null && mTopInset != 0 && mScrollY > 0) {
float f = progress(mScrollY,
mStatusBarFullOpacityBottom - mTopInset * 3,
mStatusBarFullOpacityBottom - mTopInset);
color = Color.argb((int) (255 * f),
(int) (Color.red(mMutedColor) * 0.9),
(int) (Color.green(mMutedColor) * 0.9),
(int) (Color.blue(mMutedColor) * 0.9));
}
mStatusBarColorDrawable.setColor(color);
mDrawInsetsFrameLayout.setInsetBackground(mStatusBarColorDrawable);
}
static float progress(float v, float min, float max) {
return constrain((v - min) / (max - min), 0, 1);
}
static float constrain(float val, float min, float max) {
if (val < min) {
return min;
} else if (val > max) {
return max;
} else {
return val;
}
}
private Date parsePublishedDate() {
try {
String date = mCursor.getString(ArticleLoader.Query.PUBLISHED_DATE);
return dateFormat.parse(date);
} catch (ParseException ex) {
Log.e(TAG, ex.getMessage());
Log.i(TAG, "passing today's date");
return new Date();
}
}
private void bindViews() {
if (mRootView == null) {
return;
}
TextView titleView = (TextView) mRootView.findViewById(R.id.article_title);
TextView bylineView = (TextView) mRootView.findViewById(R.id.article_byline);
bylineView.setMovementMethod(new LinkMovementMethod());
TextView bodyView = (TextView) mRootView.findViewById(R.id.article_body);
bodyView.setTypeface(Typeface.createFromAsset(getResources().getAssets(), "Rosario-Regular.ttf"));
if (mCursor != null) {
mRootView.setAlpha(0);
mRootView.setVisibility(View.VISIBLE);
mRootView.animate().alpha(1);
titleView.setText(mCursor.getString(ArticleLoader.Query.TITLE));
Date publishedDate = parsePublishedDate();
if (!publishedDate.before(START_OF_EPOCH.getTime())) {
bylineView.setText(Html.fromHtml(
DateUtils.getRelativeTimeSpanString(
publishedDate.getTime(),
System.currentTimeMillis(), DateUtils.HOUR_IN_MILLIS,
DateUtils.FORMAT_ABBREV_ALL).toString()
+ " by <font color='#ffffff'>"
+ mCursor.getString(ArticleLoader.Query.AUTHOR)
+ "</font>"));
} else {
// If date is before 1902, just show the string
bylineView.setText(Html.fromHtml(
outputFormat.format(publishedDate) + " by <font color='#ffffff'>"
+ mCursor.getString(ArticleLoader.Query.AUTHOR)
+ "</font>"));
}
bodyView.setText(Html.fromHtml(mCursor.getString(ArticleLoader.Query.BODY).replaceAll("(\r\n|\n)", "<br />")));
ImageLoaderHelper.getInstance(getActivity()).getImageLoader()
.get(mCursor.getString(ArticleLoader.Query.PHOTO_URL), new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
Bitmap bitmap = imageContainer.getBitmap();
if (bitmap != null) {
Palette p = Palette.generate(bitmap, 12);
mMutedColor = p.getDarkMutedColor(0xFF333333);
mPhotoView.setImageBitmap(imageContainer.getBitmap());
mRootView.findViewById(R.id.meta_bar)
.setBackgroundColor(mMutedColor);
updateStatusBar();
}
}
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
} else {
mRootView.setVisibility(View.GONE);
titleView.setText("N/A");
bylineView.setText("N/A" );
bodyView.setText("N/A");
}
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return ArticleLoader.newInstanceForItemId(getActivity(), mItemId);
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
if (!isAdded()) {
if (cursor != null) {
cursor.close();
}
return;
}
mCursor = cursor;
if (mCursor != null && !mCursor.moveToFirst()) {
Log.e(TAG, "Error reading item detail cursor");
mCursor.close();
mCursor = null;
}
bindViews();
}
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
mCursor = null;
bindViews();
}
public int getUpButtonFloor() {
if (mPhotoContainerView == null || mPhotoView.getHeight() == 0) {
return Integer.MAX_VALUE;
}
// account for parallax
return mIsCard
? (int) mPhotoContainerView.getTranslationY() + mPhotoView.getHeight() - mScrollY
: mPhotoView.getHeight() - mScrollY;
}
}