Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Asynctask issue #3051 solved #3058

Open
wants to merge 4 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.ViewGroup;
Expand All @@ -31,6 +32,7 @@
import me.ccrama.redditslide.Notifications.NotificationJobScheduler;
import me.ccrama.redditslide.R;
import me.ccrama.redditslide.Reddit;
import me.ccrama.redditslide.Services.CommentScreenTask;
import me.ccrama.redditslide.SettingValues;
import me.ccrama.redditslide.UserSubscriptions;
import me.ccrama.redditslide.util.LogUtil;
Expand All @@ -49,6 +51,7 @@ public class CommentsScreenSingle extends BaseActivityAnim {
private String name;
private String context;
private int contextNumber;
private CommentScreenTask.AsyncGetSubredditName asyncGetSubredditName;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Expand Down Expand Up @@ -92,15 +95,14 @@ public void onCreate(Bundle savedInstance) {
applyColorTheme();
setContentView(R.layout.activity_slide);
name = getIntent().getExtras().getString(EXTRA_SUBMISSION, "");

subreddit = getIntent().getExtras().getString(EXTRA_SUBREDDIT, "");
np = getIntent().getExtras().getBoolean(EXTRA_NP, false);
context = getIntent().getExtras().getString(EXTRA_CONTEXT, "");

contextNumber = getIntent().getExtras().getInt(EXTRA_CONTEXT_NUMBER, 5);

if (subreddit.equals(Reddit.EMPTY_STRING)) {
new AsyncGetSubredditName().execute(name);
asyncGetSubredditName = new CommentScreenTask.AsyncGetSubredditName(this);
asyncGetSubredditName.execute(name);
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.activity_background, typedValue, true);
int color = typedValue.data;
Expand Down Expand Up @@ -209,69 +211,20 @@ public void onPageScrollStateChanged(int state) {
boolean archived;
boolean contest;

private class AsyncGetSubredditName extends AsyncTask<String, Void, String> {

@Override
protected void onPostExecute(String s) {
subreddit = s;
setupAdapter();
}

@Override
protected String doInBackground(String... params) {
try {
final Submission s = Authentication.reddit.getSubmission(params[0]);
if (SettingValues.storeHistory) {
if (SettingValues.storeNSFWHistory && s.isNsfw() || !s.isNsfw()) {
HasSeen.addSeen(s.getFullName());
}
LastComments.setComments(s);
}
HasSeen.setHasSeenSubmission(new ArrayList<Submission>() {{
this.add(s);
}});
locked = s.isLocked();
archived = s.isArchived();
contest = s.getDataNode().get("contest_mode").asBoolean();
if(s.getSubredditName() == null){
subreddit = "Promoted";
} else {
subreddit = s.getSubredditName();
}
return subreddit;

} catch (Exception e) {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
new AlertDialogWrapper.Builder(CommentsScreenSingle.this).setTitle(
R.string.submission_not_found)
.setMessage(R.string.submission_not_found_msg)
.setPositiveButton(R.string.btn_ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
finish();
}
})
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
finish();
}
})
.show();
}
});
} catch (Exception ignored) {

}
return null;
}

public void update(Submission s, String subreddit) {
this.subreddit = subreddit;
locked = s.isLocked();
archived = s.isArchived();
contest = s.getDataNode().get("contest_mode").asBoolean();
setupAdapter();
}

@Override
public void onDestroy() {
super.onDestroy();
if (asyncGetSubredditName != null) {
asyncGetSubredditName.cancel(true);
Alexendoo marked this conversation as resolved.
Show resolved Hide resolved
asyncGetSubredditName = null;
}
}

Expand Down
132 changes: 41 additions & 91 deletions app/src/main/java/me/ccrama/redditslide/Activities/Inbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,14 @@
import android.view.animation.LinearInterpolator;
import android.view.inputmethod.InputMethodManager;

import net.dean.jraw.managers.InboxManager;

import java.util.HashSet;
import java.util.Set;

import me.ccrama.redditslide.Authentication;
import me.ccrama.redditslide.Autocache.AutoCacheScheduler;
import me.ccrama.redditslide.ColorPreferences;
import me.ccrama.redditslide.ContentGrabber;
import me.ccrama.redditslide.Fragments.InboxPage;
import me.ccrama.redditslide.Fragments.SettingsGeneralFragment;
import me.ccrama.redditslide.Notifications.NotificationJobScheduler;
import me.ccrama.redditslide.R;
import me.ccrama.redditslide.Reddit;
import me.ccrama.redditslide.Services.InboxTask;
import me.ccrama.redditslide.SettingValues;
import me.ccrama.redditslide.UserSubscriptions;
import me.ccrama.redditslide.Visuals.Palette;
import me.ccrama.redditslide.util.LogUtil;

Expand All @@ -46,6 +38,8 @@ public class Inbox extends BaseActivityAnim {
public Inbox.OverviewPagerAdapter adapter;
private TabLayout tabs;
private ViewPager pager;
private InboxTask.AuthenticationVerify authenticationVerify;
private InboxTask.ReadStatus readStatus;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Expand All @@ -58,7 +52,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

private boolean changed;
//private boolean changed;
public long last;

@Override
Expand All @@ -77,43 +71,33 @@ public boolean onOptionsItemSelected(MenuItem item) {
startActivity(i);
break;
case (R.id.read):
changed = false;
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
new InboxManager(Authentication.reddit).setAllRead();
changed = true;
} catch (Exception ignored) {
ignored.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
if (changed) { //restart the fragment
adapter.notifyDataSetChanged();

try {
final int CURRENT_TAB = tabs.getSelectedTabPosition();
adapter = new OverviewPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
tabs.setupWithViewPager(pager);

scrollToTabAfterLayout(CURRENT_TAB);
pager.setCurrentItem(CURRENT_TAB);
} catch (Exception e) {

}
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
readStatus = new InboxTask.ReadStatus(this);
readStatus.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
break;
}
return super.onOptionsItemSelected(item);
}

/**
* Method to update the Inbox UI when user tap on read option
*
*/
public void updateInboxUI(){
adapter.notifyDataSetChanged();

try {
final int CURRENT_TAB = tabs.getSelectedTabPosition();
adapter = new Inbox.OverviewPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
tabs.setupWithViewPager(pager);

scrollToTabAfterLayout(CURRENT_TAB);
pager.setCurrentItem(CURRENT_TAB);
} catch (Exception e) {

}
}

/**
* Method to scroll the TabLayout to a specific index
*
Expand Down Expand Up @@ -142,56 +126,9 @@ public void onCreate(Bundle savedInstance) {
if (Authentication.reddit == null || !Authentication.reddit.isAuthenticated() || Authentication.me == null) {
LogUtil.v("Reauthenticating");

new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
if (Authentication.reddit == null) {
new Authentication(getApplicationContext());
}

try {
Authentication.me = Authentication.reddit.me();
Authentication.mod = Authentication.me.isMod();

Authentication.authentication.edit()
.putBoolean(Reddit.SHARED_PREF_IS_MOD, Authentication.mod)
.apply();

if (Reddit.notificationTime != -1) {
Reddit.notifications = new NotificationJobScheduler(Inbox.this);
Reddit.notifications.start(getApplicationContext());
}

if (Reddit.cachedData.contains("toCache")) {
Reddit.autoCache = new AutoCacheScheduler(Inbox.this);
Reddit.autoCache.start(getApplicationContext());
}

final String name = Authentication.me.getFullName();
Authentication.name = name;
LogUtil.v("AUTHENTICATED");
UserSubscriptions.doCachedModSubs();

if (Authentication.reddit.isAuthenticated()) {
final Set<String> accounts =
Authentication.authentication.getStringSet("accounts", new HashSet<String>());
if (accounts.contains(name)) { //convert to new system
accounts.remove(name);
accounts.add(name + ":" + Authentication.refresh);
Authentication.authentication.edit()
.putStringSet("accounts", accounts)
.apply(); //force commit
}
Authentication.isLoggedIn = true;
Reddit.notFirst = true;
}

} catch (Exception ignored){
authenticationVerify = new InboxTask.AuthenticationVerify(Inbox.this);
authenticationVerify.execute();

}
return null;
}
}.execute();
}

super.onCreate(savedInstance);
Expand Down Expand Up @@ -279,4 +216,17 @@ public void onResume(){
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(getWindow().getAttributes().token, 0);
}

@Override
public void onDestroy() {
super.onDestroy();
if (authenticationVerify != null) {
authenticationVerify.cancel(true);
authenticationVerify = null;
}
if (readStatus != null) {
readStatus.cancel(true);
readStatus = null;
}
}
}
Loading