Skip to content

Commit ac066fe

Browse files
author
Mattia PRIMAVERA
committed
Loading Readme file: replacing TextView with WebView
1 parent ec0352b commit ac066fe

File tree

2 files changed

+31
-100
lines changed

2 files changed

+31
-100
lines changed

app/src/main/java/com/gh4a/fragment/RepositoryFragment.java

+26-81
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,17 @@
1515
*/
1616
package com.gh4a.fragment;
1717

18-
import android.content.Context;
1918
import android.content.Intent;
20-
import android.graphics.Typeface;
21-
import android.os.AsyncTask;
2219
import android.os.Bundle;
2320
import android.support.v4.content.Loader;
24-
import android.support.v4.os.AsyncTaskCompat;
2521
import android.text.SpannableStringBuilder;
2622
import android.view.LayoutInflater;
2723
import android.view.View;
2824
import android.view.View.OnClickListener;
2925
import android.view.ViewGroup;
26+
import android.webkit.WebView;
3027
import android.widget.LinearLayout;
3128
import android.widget.TextView;
32-
3329
import com.gh4a.R;
3430
import com.gh4a.activities.CollaboratorListActivity;
3531
import com.gh4a.activities.ContributorListActivity;
@@ -46,12 +42,10 @@
4642
import com.gh4a.loader.PullRequestCountLoader;
4743
import com.gh4a.loader.ReadmeLoader;
4844
import com.gh4a.utils.ApiHelpers;
49-
import com.gh4a.utils.HttpImageGetter;
5045
import com.gh4a.utils.StringUtils;
5146
import com.gh4a.utils.UiUtils;
5247
import com.gh4a.widget.IntentSpan;
5348
import com.vdurmont.emoji.EmojiParser;
54-
5549
import org.eclipse.egit.github.core.Permissions;
5650
import org.eclipse.egit.github.core.Repository;
5751

@@ -70,7 +64,6 @@ public static RepositoryFragment newInstance(Repository repository, String ref)
7064
private Repository mRepository;
7165
private View mContentView;
7266
private String mRef;
73-
private HttpImageGetter mImageGetter;
7467

7568
private final LoaderCallbacks<String> mReadmeCallback = new LoaderCallbacks<String>(this) {
7669
@Override
@@ -80,13 +73,23 @@ protected Loader<LoaderResult<String>> onCreateLoader() {
8073
}
8174
@Override
8275
protected void onResultReady(String result) {
83-
TextView readmeView = (TextView) mContentView.findViewById(R.id.readme);
8476
View progress = mContentView.findViewById(R.id.pb_readme);
85-
AsyncTaskCompat.executeParallel(new FillReadmeTask(
86-
mRepository.getId(), readmeView, progress, mImageGetter), result);
77+
loadHtmlContent(result);
8778
}
8879
};
8980

81+
public void loadHtmlContent(String html) {
82+
WebView webView = (WebView) mContentView.findViewById(R.id.webView);
83+
webView.setVerticalScrollBarEnabled(false);
84+
webView.setHorizontalScrollBarEnabled(true);
85+
86+
String mime = "text/html";
87+
String encoding = "utf-8";
88+
89+
webView.getSettings().setJavaScriptEnabled(true);
90+
webView.loadDataWithBaseURL(null, html, mime, encoding, null);
91+
}
92+
9093
private final LoaderCallbacks<Integer> mPullRequestsCallback = new LoaderCallbacks<Integer>(this) {
9194
@Override
9295
protected Loader<LoaderResult<Integer>> onCreateLoader() {
@@ -120,57 +123,33 @@ protected View onCreateContentView(LayoutInflater inflater, ViewGroup parent) {
120123
return mContentView;
121124
}
122125

123-
@Override
124-
public void onDestroyView() {
125-
super.onDestroyView();
126-
mImageGetter.destroy();
127-
mImageGetter = null;
128-
}
129-
130126
@Override
131127
public void onRefresh() {
132128
if (mContentView != null) {
133-
mContentView.findViewById(R.id.readme).setVisibility(View.GONE);
134129
mContentView.findViewById(R.id.pb_readme).setVisibility(View.VISIBLE);
135130
mContentView.findViewById(R.id.pull_requests_progress).setVisibility(View.VISIBLE);
136131
}
137-
if (mImageGetter != null) {
138-
mImageGetter.clearHtmlCache();
139-
}
132+
140133
hideContentAndRestartLoaders(0, 1);
141134
}
142135

143136
@Override
144137
public void onActivityCreated(Bundle savedInstanceState) {
145138
super.onActivityCreated(savedInstanceState);
146139

147-
mImageGetter = new HttpImageGetter(getActivity());
148140
fillData();
149141
setContentShown(true);
150142

151143
getLoaderManager().initLoader(0, null, mReadmeCallback);
152144
getLoaderManager().initLoader(1, null, mPullRequestsCallback);
153145
}
154146

155-
@Override
156-
public void onResume() {
157-
super.onResume();
158-
mImageGetter.resume();
159-
}
160-
161-
@Override
162-
public void onPause() {
163-
super.onPause();
164-
mImageGetter.pause();
165-
}
166-
167147
public void setRef(String ref) {
168148
mRef = ref;
169149
getArguments().putString("ref", ref);
170150
// reload readme
171151
getLoaderManager().restartLoader(0, null, mReadmeCallback);
172152
if (mContentView != null) {
173-
mContentView.findViewById(R.id.readme).setVisibility(View.GONE);
174153
mContentView.findViewById(R.id.pb_readme).setVisibility(View.VISIBLE);
175154
}
176155
}
@@ -207,12 +186,17 @@ protected Intent getIntent() {
207186
fillTextView(R.id.tv_url, 0, !StringUtils.isBlank(mRepository.getHomepage())
208187
? mRepository.getHomepage() : mRepository.getHtmlUrl());
209188

210-
mContentView.findViewById(R.id.cell_stargazers).setOnClickListener(this);
211-
mContentView.findViewById(R.id.cell_forks).setOnClickListener(this);
212-
mContentView.findViewById(R.id.cell_pull_requests).setOnClickListener(this);
213-
mContentView.findViewById(R.id.tv_contributors_label).setOnClickListener(this);
214-
mContentView.findViewById(R.id.other_info).setOnClickListener(this);
215-
mContentView.findViewById(R.id.tv_releases_label).setOnClickListener(this);
189+
// Setting Click listeners
190+
int[] viewIds = new int[]{
191+
R.id.cell_stargazers,
192+
R.id.cell_forks,
193+
R.id.cell_pull_requests,
194+
R.id.tv_contributors_label,
195+
R.id.other_info,
196+
R.id.tv_releases_label
197+
};
198+
for(int id : viewIds)
199+
mContentView.findViewById(id).setOnClickListener(this);
216200

217201
Permissions permissions = mRepository.getPermissions();
218202
updateClickableLabel(R.id.tv_collaborators_label,
@@ -310,43 +294,4 @@ public void onClick(View view) {
310294
startActivity(intent);
311295
}
312296
}
313-
314-
private static class FillReadmeTask extends AsyncTask<String, Void, String> {
315-
private final Long mId;
316-
private final Context mContext;
317-
private final TextView mReadmeView;
318-
private final View mProgressView;
319-
private final HttpImageGetter mImageGetter;
320-
321-
public FillReadmeTask(long id, TextView readmeView, View progressView,
322-
HttpImageGetter imageGetter) {
323-
mId = id;
324-
mContext = readmeView.getContext();
325-
mReadmeView = readmeView;
326-
mProgressView = progressView;
327-
mImageGetter = imageGetter;
328-
}
329-
330-
@Override
331-
protected String doInBackground(String... params) {
332-
String readme = params[0];
333-
if (readme != null) {
334-
mImageGetter.encode(mContext, mId, readme);
335-
}
336-
return readme;
337-
}
338-
339-
@Override
340-
protected void onPostExecute(String result) {
341-
if (result != null) {
342-
mReadmeView.setMovementMethod(UiUtils.CHECKING_LINK_METHOD);
343-
mImageGetter.bind(mReadmeView, result, mId);
344-
} else {
345-
mReadmeView.setText(R.string.repo_no_readme);
346-
mReadmeView.setTypeface(Typeface.DEFAULT, Typeface.ITALIC);
347-
}
348-
mReadmeView.setVisibility(View.VISIBLE);
349-
mProgressView.setVisibility(View.GONE);
350-
}
351-
}
352297
}

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

+5-19
Original file line numberDiff line numberDiff line change
@@ -218,33 +218,19 @@
218218

219219
<LinearLayout
220220
android:layout_width="match_parent"
221-
android:layout_height="wrap_content"
221+
android:layout_height="match_parent"
222222
android:layout_marginBottom="@dimen/overview_header_spacing"
223223
android:orientation="vertical">
224224

225-
<com.gh4a.widget.StyleableTextView
226-
android:id="@+id/readme_title"
227-
style="@style/HeaderLabel"
225+
<WebView
226+
android:id="@+id/webView"
228227
android:layout_width="match_parent"
229-
android:layout_height="wrap_content"
230-
android:text="@string/readme" />
228+
android:layout_height="wrap_content">
229+
</WebView>
231230

232231
<ProgressBar
233232
android:id="@+id/pb_readme"
234233
style="@style/LoadingProgress" />
235-
236-
<com.gh4a.widget.StyleableTextView
237-
android:id="@+id/readme"
238-
android:layout_width="match_parent"
239-
android:layout_height="wrap_content"
240-
android:paddingBottom="@dimen/content_padding"
241-
android:paddingLeft="@dimen/content_padding"
242-
android:paddingRight="@dimen/content_padding"
243-
android:textIsSelectable="true"
244-
android:visibility="gone"
245-
tools:text="Readme text"
246-
tools:visibility="visible" />
247-
248234
</LinearLayout>
249235
</android.support.v7.widget.CardView>
250236

0 commit comments

Comments
 (0)