Skip to content

Fixing readme loading: Using webview #718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 26 additions & 81 deletions app/src/main/java/com/gh4a/fragment/RepositoryFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@
*/
package com.gh4a.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.support.v4.os.AsyncTaskCompat;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gh4a.R;
import com.gh4a.activities.CollaboratorListActivity;
import com.gh4a.activities.ContributorListActivity;
Expand All @@ -46,12 +42,10 @@
import com.gh4a.loader.PullRequestCountLoader;
import com.gh4a.loader.ReadmeLoader;
import com.gh4a.utils.ApiHelpers;
import com.gh4a.utils.HttpImageGetter;
import com.gh4a.utils.StringUtils;
import com.gh4a.utils.UiUtils;
import com.gh4a.widget.IntentSpan;
import com.vdurmont.emoji.EmojiParser;

import org.eclipse.egit.github.core.Permissions;
import org.eclipse.egit.github.core.Repository;

Expand All @@ -70,7 +64,6 @@ public static RepositoryFragment newInstance(Repository repository, String ref)
private Repository mRepository;
private View mContentView;
private String mRef;
private HttpImageGetter mImageGetter;

private final LoaderCallbacks<String> mReadmeCallback = new LoaderCallbacks<String>(this) {
@Override
Expand All @@ -80,13 +73,23 @@ protected Loader<LoaderResult<String>> onCreateLoader() {
}
@Override
protected void onResultReady(String result) {
TextView readmeView = (TextView) mContentView.findViewById(R.id.readme);
View progress = mContentView.findViewById(R.id.pb_readme);
AsyncTaskCompat.executeParallel(new FillReadmeTask(
mRepository.getId(), readmeView, progress, mImageGetter), result);
loadHtmlContent(result);
}
};

public void loadHtmlContent(String html) {
WebView webView = (WebView) mContentView.findViewById(R.id.webView);
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(true);

String mime = "text/html";
String encoding = "utf-8";

webView.getSettings().setJavaScriptEnabled(true);
webView.loadDataWithBaseURL(null, html, mime, encoding, null);
}

private final LoaderCallbacks<Integer> mPullRequestsCallback = new LoaderCallbacks<Integer>(this) {
@Override
protected Loader<LoaderResult<Integer>> onCreateLoader() {
Expand Down Expand Up @@ -120,57 +123,33 @@ protected View onCreateContentView(LayoutInflater inflater, ViewGroup parent) {
return mContentView;
}

@Override
public void onDestroyView() {
super.onDestroyView();
mImageGetter.destroy();
mImageGetter = null;
}

@Override
public void onRefresh() {
if (mContentView != null) {
mContentView.findViewById(R.id.readme).setVisibility(View.GONE);
mContentView.findViewById(R.id.pb_readme).setVisibility(View.VISIBLE);
mContentView.findViewById(R.id.pull_requests_progress).setVisibility(View.VISIBLE);
}
if (mImageGetter != null) {
mImageGetter.clearHtmlCache();
}

hideContentAndRestartLoaders(0, 1);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

mImageGetter = new HttpImageGetter(getActivity());
fillData();
setContentShown(true);

getLoaderManager().initLoader(0, null, mReadmeCallback);
getLoaderManager().initLoader(1, null, mPullRequestsCallback);
}

@Override
public void onResume() {
super.onResume();
mImageGetter.resume();
}

@Override
public void onPause() {
super.onPause();
mImageGetter.pause();
}

public void setRef(String ref) {
mRef = ref;
getArguments().putString("ref", ref);
// reload readme
getLoaderManager().restartLoader(0, null, mReadmeCallback);
if (mContentView != null) {
mContentView.findViewById(R.id.readme).setVisibility(View.GONE);
mContentView.findViewById(R.id.pb_readme).setVisibility(View.VISIBLE);
}
}
Expand Down Expand Up @@ -207,12 +186,17 @@ protected Intent getIntent() {
fillTextView(R.id.tv_url, 0, !StringUtils.isBlank(mRepository.getHomepage())
? mRepository.getHomepage() : mRepository.getHtmlUrl());

mContentView.findViewById(R.id.cell_stargazers).setOnClickListener(this);
mContentView.findViewById(R.id.cell_forks).setOnClickListener(this);
mContentView.findViewById(R.id.cell_pull_requests).setOnClickListener(this);
mContentView.findViewById(R.id.tv_contributors_label).setOnClickListener(this);
mContentView.findViewById(R.id.other_info).setOnClickListener(this);
mContentView.findViewById(R.id.tv_releases_label).setOnClickListener(this);
// Setting Click listeners
int[] viewIds = new int[]{
R.id.cell_stargazers,
R.id.cell_forks,
R.id.cell_pull_requests,
R.id.tv_contributors_label,
R.id.other_info,
R.id.tv_releases_label
};
for(int id : viewIds)
mContentView.findViewById(id).setOnClickListener(this);

Permissions permissions = mRepository.getPermissions();
updateClickableLabel(R.id.tv_collaborators_label,
Expand Down Expand Up @@ -310,43 +294,4 @@ public void onClick(View view) {
startActivity(intent);
}
}

private static class FillReadmeTask extends AsyncTask<String, Void, String> {
private final Long mId;
private final Context mContext;
private final TextView mReadmeView;
private final View mProgressView;
private final HttpImageGetter mImageGetter;

public FillReadmeTask(long id, TextView readmeView, View progressView,
HttpImageGetter imageGetter) {
mId = id;
mContext = readmeView.getContext();
mReadmeView = readmeView;
mProgressView = progressView;
mImageGetter = imageGetter;
}

@Override
protected String doInBackground(String... params) {
String readme = params[0];
if (readme != null) {
mImageGetter.encode(mContext, mId, readme);
}
return readme;
}

@Override
protected void onPostExecute(String result) {
if (result != null) {
mReadmeView.setMovementMethod(UiUtils.CHECKING_LINK_METHOD);
mImageGetter.bind(mReadmeView, result, mId);
} else {
mReadmeView.setText(R.string.repo_no_readme);
mReadmeView.setTypeface(Typeface.DEFAULT, Typeface.ITALIC);
}
mReadmeView.setVisibility(View.VISIBLE);
mProgressView.setVisibility(View.GONE);
}
}
}
24 changes: 5 additions & 19 deletions app/src/main/res/layout/repository.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,33 +218,19 @@

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/overview_header_spacing"
android:orientation="vertical">

<com.gh4a.widget.StyleableTextView
android:id="@+id/readme_title"
style="@style/HeaderLabel"
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/readme" />
android:layout_height="wrap_content">
</WebView>

<ProgressBar
android:id="@+id/pb_readme"
style="@style/LoadingProgress" />

<com.gh4a.widget.StyleableTextView
android:id="@+id/readme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/content_padding"
android:paddingLeft="@dimen/content_padding"
android:paddingRight="@dimen/content_padding"
android:textIsSelectable="true"
android:visibility="gone"
tools:text="Readme text"
tools:visibility="visible" />

</LinearLayout>
</android.support.v7.widget.CardView>

Expand Down