use of com.ferg.awfulapp.thread.AwfulURL in project Awful.apk by Awful.
the class ThreadDisplayFragment method startPostRedirect.
/**
* Reload with a new URL
* @param postUrl The URL of the post we should land on
*/
private void startPostRedirect(final String postUrl) {
final AwfulActivity activity = getAwfulActivity();
if (activity == null) {
return;
}
if (redirect != null) {
redirect.cancel(false);
}
setProgress(50);
redirect = new RedirectTask(postUrl) {
@Override
protected void onPostExecute(String url) {
if (isCancelled()) {
return;
} else if (url == null) {
getAlertView().show(new AwfulError());
return;
}
AwfulURL result = AwfulURL.parse(url);
if (postUrl.contains(Constants.VALUE_LASTPOST)) {
// This is a workaround for how the forums handle the perPage value with goto=lastpost.
// The redirected url is lacking the perpage=XX value.
// We just override the assumed (40) with the number we requested when starting the redirect.
// I gotta ask chooch to fix this at some point.
result.setPerPage(getPrefs().postPerPage);
}
if (result.getType() == TYPE.THREAD) {
int threadId = (int) result.getId();
int threadPage = (int) result.getPage(getPrefs().postPerPage);
String postJump = result.getFragment().replaceAll("\\D", "");
if (bypassBackStack) {
openThread(threadId, threadPage, postJump, true);
} else {
pushThread(threadId, threadPage, postJump);
}
} else if (result.getType() == TYPE.INDEX) {
activity.showForumIndex();
}
redirect = null;
bypassBackStack = false;
setProgress(100);
}
}.execute();
}
use of com.ferg.awfulapp.thread.AwfulURL in project Awful.apk by Awful.
the class SearchFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater aInflater, ViewGroup aContainer, Bundle aSavedState) {
super.onCreateView(aInflater, aContainer, aSavedState);
Timber.v("onCreateView");
View result = inflateView(R.layout.search, aContainer, aInflater);
mSearchQuery = result.findViewById(R.id.search_query);
mSRL = result.findViewById(R.id.search_srl);
mSRL.setOnRefreshListener(this);
mSRL.setColorSchemeResources(ColorProvider.getSRLProgressColors(null));
mSRL.setProgressBackgroundColor(ColorProvider.getSRLBackgroundColor(null));
mSRL.setEnabled(false);
mSearchResultList = result.findViewById(R.id.search_results);
mSearchResultList.setAdapter(new RecyclerView.Adapter<SearchResultHolder>() {
@Override
public SearchResultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_result_item, parent, false);
return new SearchResultHolder(view);
}
@Override
public void onBindViewHolder(SearchResultHolder holder, final int position) {
AwfulSearch search = mSearchResults.get(position);
holder.threadName.setText(search.getThreadTitle());
holder.hitInfo.setText(Html.fromHtml("<b>" + search.getUsername() + "</b> in <b>" + search.getForumTitle() + "</b>"));
holder.blurb.setText(Html.fromHtml(search.getBlurb()));
holder.threadName.setText(search.getThreadTitle());
holder.timestamp.setText(search.getPostDate());
final String threadlink = search.getThreadLink();
final int forumId = search.getForumId();
final ProgressDialog redirectDialog = new ProgressDialog(getContext());
final RedirectTask redirect = new RedirectTask(Constants.BASE_URL + threadlink) {
@Override
protected void onPostExecute(String url) {
if (!isCancelled()) {
if (url != null) {
AwfulURL result = AwfulURL.parse(url);
Activity activity = getActivity();
Intent openThread = new Intent().setClass(activity, ForumsIndexActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra(Constants.THREAD_ID, (int) result.getId()).putExtra(Constants.THREAD_PAGE, (int) result.getPage()).putExtra(Constants.FORUM_ID, forumId).putExtra(Constants.FORUM_PAGE, 1).putExtra(Constants.THREAD_FRAGMENT, result.getFragment().substring(4));
redirectDialog.dismiss();
activity.finish();
startActivity(openThread);
} else {
getAlertView().show(new AwfulError());
}
}
}
};
holder.self.setOnClickListener(v -> {
if (getActivity() != null) {
if (redirect.getStatus() == AsyncTask.Status.PENDING) {
redirect.execute();
redirectDialog.setMessage("Just a second");
redirectDialog.setTitle("Loading");
redirectDialog.setIndeterminate(true);
redirectDialog.setCancelable(false);
redirectDialog.show();
}
}
});
}
@Override
public int getItemCount() {
if (mSearchResults != null) {
return mSearchResults.size();
}
return 0;
}
});
mSearchResultList.setLayoutManager(new LinearLayoutManager(getContext()));
return result;
}
use of com.ferg.awfulapp.thread.AwfulURL in project Awful.apk by Awful.
the class ThreadDisplayFragment method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mPostLoaderCallback = new PostLoaderManager();
mThreadLoaderCallback = new ThreadDataCallback();
boolean loadFromCache = false;
if (savedInstanceState != null) {
// restoring old state - we have a thread ID and page
// TODO: 04/05/2017 post filtering state isn't restored properly - need to do filtering AND maintain filtered page/position AND recreate the backstack/'go back' UI
Timber.i("Restoring fragment - loading cached posts from database");
setThreadId(savedInstanceState.getInt(THREAD_ID_KEY, NULL_THREAD_ID));
setPageNumber(savedInstanceState.getInt(THREAD_PAGE_KEY, FIRST_PAGE));
// TODO: 04/05/2017 saved scroll position doesn't seem to actually get used to set the position?
savedScrollPosition = savedInstanceState.getInt(SCROLL_POSITION_KEY, 0);
loadFromCache = true;
} else {
Timber.i("No saved fragment state - initialising by parsing Intent");
Intent data = getActivity().getIntent();
if (data.getData() != null && data.getScheme().equals("http")) {
AwfulURL url = AwfulURL.parse(data.getDataString());
setPostJump(url.getFragment().replaceAll("\\D", ""));
switch(url.getType()) {
case THREAD:
if (url.isRedirect()) {
startPostRedirect(url.getURL(getPrefs().postPerPage));
} else {
setThreadId((int) url.getId());
setPageNumber((int) url.getPage(getPrefs().postPerPage));
}
break;
case POST:
startPostRedirect(url.getURL(getPrefs().postPerPage));
break;
case INDEX:
displayForumIndex();
break;
}
}
}
// no valid thread ID means do nothing I guess? If the intent that created the activity+fragments didn't request a thread
if (getThreadId() <= 0) {
return;
}
// so we get the same state as before (we don't want to reload the page and e.g. have all the posts marked as seen)
if (loadFromCache) {
refreshPosts();
refreshInfo();
} else {
syncThread();
}
}
Aggregations