use of net.dean.jraw.managers.AccountManager in project Slide by ccrama.
the class MainActivity method doSubOnlyStuff.
public void doSubOnlyStuff(final Subreddit subreddit) {
findViewById(R.id.loader).setVisibility(View.GONE);
if (subreddit.getSubredditType() != null) {
canSubmit = !subreddit.getSubredditType().equals("RESTRICTED");
} else {
canSubmit = true;
}
if (subreddit.getSidebar() != null && !subreddit.getSidebar().isEmpty()) {
findViewById(R.id.sidebar_text).setVisibility(View.VISIBLE);
final String text = subreddit.getDataNode().get("description_html").asText().trim();
setViews(text, subreddit.getDisplayName(), sidebarBody, sidebarOverflow);
// get all subs that have Notifications enabled
ArrayList<String> rawSubs = Reddit.stringToArray(Reddit.appRestart.getString(CheckForMail.SUBS_TO_GET, ""));
HashMap<String, Integer> subThresholds = new HashMap<>();
for (String s : rawSubs) {
try {
String[] split = s.split(":");
subThresholds.put(split[0].toLowerCase(Locale.ENGLISH), Integer.valueOf(split[1]));
} catch (Exception ignored) {
// do nothing
}
}
// whether or not this subreddit was in the keySet
boolean isNotified = subThresholds.keySet().contains(subreddit.getDisplayName().toLowerCase(Locale.ENGLISH));
((AppCompatCheckBox) findViewById(R.id.notify_posts_state)).setChecked(isNotified);
} else {
findViewById(R.id.sidebar_text).setVisibility(View.GONE);
}
{
View collection = findViewById(R.id.collection);
if (Authentication.isLoggedIn) {
collection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<Void, Void, Void>() {
HashMap<String, MultiReddit> multis = new HashMap<String, MultiReddit>();
@Override
protected Void doInBackground(Void... params) {
if (UserSubscriptions.multireddits == null) {
UserSubscriptions.syncMultiReddits(MainActivity.this);
}
for (MultiReddit r : UserSubscriptions.multireddits) {
multis.put(r.getDisplayName(), r);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
new MaterialDialog.Builder(MainActivity.this).title(getString(R.string.multi_add_to, subreddit.getDisplayName())).items(multis.keySet()).itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View itemView, final int which, CharSequence text) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
final String multiName = multis.keySet().toArray(new String[multis.size()])[which];
List<String> subs = new ArrayList<String>();
for (MultiSubreddit sub : multis.get(multiName).getSubreddits()) {
subs.add(sub.getDisplayName());
}
subs.add(subreddit.getDisplayName());
new MultiRedditManager(Authentication.reddit).createOrUpdate(new MultiRedditUpdateRequest.Builder(Authentication.name, multiName).subreddits(subs).build());
UserSubscriptions.syncMultiReddits(MainActivity.this);
runOnUiThread(new Runnable() {
@Override
public void run() {
drawerLayout.closeDrawers();
Snackbar s = Snackbar.make(mToolbar, getString(R.string.multi_subreddit_added, multiName), Snackbar.LENGTH_LONG);
View view = s.getView();
TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
});
} catch (final NetworkException | ApiException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Snackbar.make(mToolbar, getString(R.string.multi_error), Snackbar.LENGTH_LONG).setAction(R.string.btn_ok, new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}).show();
}
});
}
});
e.printStackTrace();
}
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}).show();
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
} else {
collection.setVisibility(View.GONE);
}
}
{
final AppCompatCheckBox notifyStateCheckBox = (AppCompatCheckBox) findViewById(R.id.notify_posts_state);
assert notifyStateCheckBox != null;
notifyStateCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
final String sub = subreddit.getDisplayName();
if (!sub.equalsIgnoreCase("all") && !sub.equalsIgnoreCase("frontpage") && !sub.equalsIgnoreCase("friends") && !sub.equalsIgnoreCase("mod") && !sub.contains("+") && !sub.contains(".") && !sub.contains("/m/")) {
new AlertDialogWrapper.Builder(MainActivity.this).setTitle(getString(R.string.sub_post_notifs_title, sub)).setMessage(R.string.sub_post_notifs_msg).setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new MaterialDialog.Builder(MainActivity.this).title(R.string.sub_post_notifs_threshold).items(new String[] { "1", "5", "10", "20", "40", "50" }).alwaysCallSingleChoiceCallback().itemsCallbackSingleChoice(0, new MaterialDialog.ListCallbackSingleChoice() {
@Override
public boolean onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
ArrayList<String> subs = Reddit.stringToArray(Reddit.appRestart.getString(CheckForMail.SUBS_TO_GET, ""));
subs.add(sub + ":" + text);
Reddit.appRestart.edit().putString(CheckForMail.SUBS_TO_GET, Reddit.arrayToString(subs)).commit();
return true;
}
}).cancelable(false).show();
}
}).setNegativeButton(R.string.btn_cancel, null).setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
notifyStateCheckBox.setChecked(false);
}
}).setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
notifyStateCheckBox.setChecked(false);
}
}).show();
} else {
notifyStateCheckBox.setChecked(false);
Toast.makeText(MainActivity.this, R.string.sub_post_notifs_err, Toast.LENGTH_SHORT).show();
}
} else {
Intent cancelIntent = new Intent(MainActivity.this, CancelSubNotifs.class);
cancelIntent.putExtra(CancelSubNotifs.EXTRA_SUB, subreddit.getDisplayName());
startActivity(cancelIntent);
}
}
});
}
{
final TextView subscribe = (TextView) findViewById(R.id.subscribe);
currentlySubbed = (!Authentication.isLoggedIn && usedArray.contains(subreddit.getDisplayName().toLowerCase(Locale.ENGLISH))) || subreddit.isUserSubscriber();
doSubscribeButtonText(currentlySubbed, subscribe);
assert subscribe != null;
subscribe.setOnClickListener(new View.OnClickListener() {
private void doSubscribe() {
if (Authentication.isLoggedIn) {
new AlertDialogWrapper.Builder(MainActivity.this).setTitle(getString(R.string.subscribe_to, subreddit.getDisplayName())).setPositiveButton(R.string.reorder_add_subscribe, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new AsyncTask<Void, Void, Boolean>() {
@Override
public void onPostExecute(Boolean success) {
if (!success) {
// If subreddit was removed from account or not
new AlertDialogWrapper.Builder(MainActivity.this).setTitle(R.string.force_change_subscription).setMessage(R.string.force_change_subscription_desc).setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
changeSubscription(subreddit, // Force add the subscription
true);
Snackbar s = Snackbar.make(mToolbar, getString(R.string.misc_subscribed), Snackbar.LENGTH_LONG);
View view = s.getView();
TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
}).setNegativeButton(R.string.btn_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setCancelable(false).show();
} else {
changeSubscription(subreddit, true);
}
}
@Override
protected Boolean doInBackground(Void... params) {
try {
new AccountManager(Authentication.reddit).subscribe(subreddit);
} catch (NetworkException e) {
// Either network crashed or trying to unsubscribe to a subreddit that the account isn't subscribed to
return false;
}
return true;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}).setNeutralButton(R.string.btn_add_to_sublist, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
changeSubscription(subreddit, // Force add the subscription
true);
Snackbar s = Snackbar.make(mToolbar, R.string.sub_added, Snackbar.LENGTH_LONG);
View view = s.getView();
TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
}).setNegativeButton(R.string.btn_cancel, null).show();
} else {
changeSubscription(subreddit, true);
}
}
private void doUnsubscribe() {
if (Authentication.didOnline) {
new AlertDialogWrapper.Builder(MainActivity.this).setTitle(getString(R.string.unsubscribe_from, subreddit.getDisplayName())).setPositiveButton(R.string.reorder_remove_unsubsribe, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new AsyncTask<Void, Void, Boolean>() {
@Override
public void onPostExecute(Boolean success) {
if (!success) {
// If subreddit was removed from account or not
new AlertDialogWrapper.Builder(MainActivity.this).setTitle(R.string.force_change_subscription).setMessage(R.string.force_change_subscription_desc).setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
changeSubscription(subreddit, // Force add the subscription
false);
Snackbar s = Snackbar.make(mToolbar, getString(R.string.misc_unsubscribed), Snackbar.LENGTH_LONG);
View view = s.getView();
TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
}).setNegativeButton(R.string.btn_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setCancelable(false).show();
} else {
changeSubscription(subreddit, false);
}
}
@Override
protected Boolean doInBackground(Void... params) {
try {
new AccountManager(Authentication.reddit).unsubscribe(subreddit);
} catch (NetworkException e) {
// Either network crashed or trying to unsubscribe to a subreddit that the account isn't subscribed to
return false;
}
return true;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}).setNeutralButton(R.string.just_unsub, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
changeSubscription(subreddit, // Force add the subscription
false);
Snackbar s = Snackbar.make(mToolbar, R.string.misc_unsubscribed, Snackbar.LENGTH_LONG);
View view = s.getView();
TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
}).setNegativeButton(R.string.btn_cancel, null).show();
} else {
changeSubscription(subreddit, false);
}
}
@Override
public void onClick(View v) {
if (!currentlySubbed) {
doSubscribe();
doSubscribeButtonText(currentlySubbed, subscribe);
} else {
doUnsubscribe();
doSubscribeButtonText(currentlySubbed, subscribe);
}
}
});
}
if (!subreddit.getPublicDescription().isEmpty()) {
findViewById(R.id.sub_title).setVisibility(View.VISIBLE);
setViews(subreddit.getDataNode().get("public_description_html").asText(), subreddit.getDisplayName().toLowerCase(Locale.ENGLISH), ((SpoilerRobotoTextView) findViewById(R.id.sub_title)), (CommentOverflow) findViewById(R.id.sub_title_overflow));
} else {
findViewById(R.id.sub_title).setVisibility(View.GONE);
}
((ImageView) findViewById(R.id.subimage)).setImageResource(0);
if (subreddit.getDataNode().has("icon_img") && !subreddit.getDataNode().get("icon_img").asText().isEmpty()) {
findViewById(R.id.subimage).setVisibility(View.VISIBLE);
((Reddit) getApplication()).getImageLoader().displayImage(subreddit.getDataNode().get("icon_img").asText(), (ImageView) findViewById(R.id.subimage));
} else {
findViewById(R.id.subimage).setVisibility(View.GONE);
}
((TextView) findViewById(R.id.subscribers)).setText(getString(R.string.subreddit_subscribers_string, subreddit.getLocalizedSubscriberCount()));
findViewById(R.id.subscribers).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.active_users)).setText(getString(R.string.subreddit_active_users_string_new, subreddit.getLocalizedAccountsActive()));
findViewById(R.id.active_users).setVisibility(View.VISIBLE);
}
use of net.dean.jraw.managers.AccountManager in project Slide by ccrama.
the class MainActivity method doSubSidebar.
public void doSubSidebar(final String subreddit) {
if (mAsyncGetSubreddit != null) {
mAsyncGetSubreddit.cancel(true);
}
findViewById(R.id.loader).setVisibility(View.VISIBLE);
invalidateOptionsMenu();
if (!subreddit.equalsIgnoreCase("all") && !subreddit.equalsIgnoreCase("frontpage") && !subreddit.equalsIgnoreCase("friends") && !subreddit.equalsIgnoreCase("mod") && !subreddit.contains("+") && !subreddit.contains(".") && !subreddit.contains("/m/")) {
if (drawerLayout != null) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, GravityCompat.END);
}
mAsyncGetSubreddit = new AsyncGetSubreddit();
mAsyncGetSubreddit.execute(subreddit);
final View dialoglayout = findViewById(R.id.sidebarsub);
{
View submit = (dialoglayout.findViewById(R.id.submit));
if (!Authentication.isLoggedIn || !Authentication.didOnline) {
submit.setVisibility(View.GONE);
}
if (SettingValues.fab && SettingValues.fabType == Constants.FAB_POST) {
submit.setVisibility(View.GONE);
}
submit.setOnClickListener(new OnSingleClickListener() {
@Override
public void onSingleClick(View view) {
Intent inte = new Intent(MainActivity.this, Submit.class);
if (!subreddit.contains("/m/") && canSubmit) {
inte.putExtra(Submit.EXTRA_SUBREDDIT, subreddit);
}
MainActivity.this.startActivity(inte);
}
});
}
dialoglayout.findViewById(R.id.wiki).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Wiki.class);
i.putExtra(Wiki.EXTRA_SUBREDDIT, subreddit);
startActivity(i);
}
});
dialoglayout.findViewById(R.id.syncflair).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageFlairs.syncFlairs(MainActivity.this, subreddit);
}
});
dialoglayout.findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Submit.class);
if ((!subreddit.contains("/m/") || !subreddit.contains(".")) && canSubmit) {
i.putExtra(Submit.EXTRA_SUBREDDIT, subreddit);
}
startActivity(i);
}
});
final TextView sort = dialoglayout.findViewById(R.id.sort);
Sorting sortingis = Sorting.HOT;
if (SettingValues.hasSort(subreddit)) {
sortingis = SettingValues.getBaseSubmissionSort(subreddit);
sort.setText(sortingis.name() + ((sortingis == Sorting.CONTROVERSIAL || sortingis == Sorting.TOP) ? " of " + SettingValues.getBaseTimePeriod(subreddit).name() : ""));
} else {
sort.setText("Set default sorting");
}
final int sortid = Reddit.getSortingId(sortingis);
dialoglayout.findViewById(R.id.sorting).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final DialogInterface.OnClickListener l2 = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
switch(i) {
case 0:
sorts = Sorting.HOT;
break;
case 1:
sorts = Sorting.NEW;
break;
case 2:
sorts = Sorting.RISING;
break;
case 3:
sorts = Sorting.TOP;
askTimePeriod(sorts, subreddit, dialoglayout);
return;
case 4:
sorts = Sorting.CONTROVERSIAL;
askTimePeriod(sorts, subreddit, dialoglayout);
return;
}
SettingValues.setSubSorting(sorts, time, subreddit);
Sorting sortingis = SettingValues.getBaseSubmissionSort(subreddit);
sort.setText(sortingis.name() + ((sortingis == Sorting.CONTROVERSIAL || sortingis == Sorting.TOP) ? " of " + SettingValues.getBaseTimePeriod(subreddit).name() : ""));
reloadSubs();
}
};
AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(MainActivity.this);
builder.setTitle(R.string.sorting_choose);
builder.setSingleChoiceItems(Reddit.getSortingStrings(getBaseContext()), sortid, l2);
builder.setNegativeButton("Reset default sorting", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SettingValues.prefs.edit().remove("defaultSort" + subreddit.toLowerCase(Locale.ENGLISH)).apply();
SettingValues.prefs.edit().remove("defaultTime" + subreddit.toLowerCase(Locale.ENGLISH)).apply();
final TextView sort = dialoglayout.findViewById(R.id.sort);
if (SettingValues.hasSort(subreddit)) {
Sorting sortingis = SettingValues.getBaseSubmissionSort(subreddit);
sort.setText(sortingis.name() + ((sortingis == Sorting.CONTROVERSIAL || sortingis == Sorting.TOP) ? " of " + SettingValues.getBaseTimePeriod(subreddit).name() : ""));
} else {
sort.setText("Set default sorting");
}
reloadSubs();
}
});
builder.show();
}
});
dialoglayout.findViewById(R.id.theme).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int style = new ColorPreferences(MainActivity.this).getThemeSubreddit(subreddit);
final Context contextThemeWrapper = new ContextThemeWrapper(MainActivity.this, style);
LayoutInflater localInflater = getLayoutInflater().cloneInContext(contextThemeWrapper);
final View dialoglayout = localInflater.inflate(R.layout.colorsub, null);
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add(subreddit);
SettingsSubAdapter.showSubThemeEditor(arrayList, MainActivity.this, dialoglayout);
}
});
dialoglayout.findViewById(R.id.mods).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog d = new MaterialDialog.Builder(MainActivity.this).title(R.string.sidebar_findingmods).cancelable(true).content(R.string.misc_please_wait).progress(true, 100).show();
new AsyncTask<Void, Void, Void>() {
ArrayList<UserRecord> mods;
@Override
protected Void doInBackground(Void... params) {
mods = new ArrayList<>();
UserRecordPaginator paginator = new UserRecordPaginator(Authentication.reddit, subreddit, "moderators");
paginator.setSorting(Sorting.HOT);
paginator.setTimePeriod(TimePeriod.ALL);
while (paginator.hasNext()) {
mods.addAll(paginator.next());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
final ArrayList<String> names = new ArrayList<>();
for (UserRecord rec : mods) {
names.add(rec.getFullName());
}
d.dismiss();
new MaterialDialog.Builder(MainActivity.this).title(getString(R.string.sidebar_submods, subreddit)).items(names).itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
Intent i = new Intent(MainActivity.this, Profile.class);
i.putExtra(Profile.EXTRA_PROFILE, names.get(which));
startActivity(i);
}
}).positiveText(R.string.btn_message).onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Intent i = new Intent(MainActivity.this, SendMessage.class);
i.putExtra(SendMessage.EXTRA_NAME, "/r/" + subreddit);
startActivity(i);
}
}).show();
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
dialoglayout.findViewById(R.id.flair).setVisibility(View.GONE);
if (Authentication.didOnline && Authentication.isLoggedIn) {
if (currentFlair != null)
currentFlair.cancel(true);
currentFlair = new AsyncTask<View, Void, View>() {
List<FlairTemplate> flairs;
ArrayList<String> flairText;
String current;
AccountManager m;
@Override
protected View doInBackground(View... params) {
try {
m = new AccountManager(Authentication.reddit);
JsonNode node = m.getFlairChoicesRootNode(subreddit, null);
flairs = m.getFlairChoices(subreddit, node);
FlairTemplate currentF = m.getCurrentFlair(subreddit, node);
if (currentF != null) {
if (currentF.getText().isEmpty()) {
current = ("[" + currentF.getCssClass() + "]");
} else {
current = (currentF.getText());
}
}
flairText = new ArrayList<>();
for (FlairTemplate temp : flairs) {
if (temp.getText().isEmpty()) {
flairText.add("[" + temp.getCssClass() + "]");
} else {
flairText.add(temp.getText());
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
return params[0];
}
@Override
protected void onPostExecute(View flair) {
if (flairs != null && !flairs.isEmpty() && flairText != null && !flairText.isEmpty()) {
flair.setVisibility(View.VISIBLE);
if (current != null) {
((TextView) dialoglayout.findViewById(R.id.flair_text)).setText(getString(R.string.sidebar_flair, current));
}
flair.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MaterialDialog.Builder(MainActivity.this).items(flairText).title(R.string.sidebar_select_flair).itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
final FlairTemplate t = flairs.get(which);
if (t.isTextEditable()) {
new MaterialDialog.Builder(MainActivity.this).title(R.string.sidebar_select_flair_text).input(getString(R.string.mod_flair_hint), t.getText(), true, new MaterialDialog.InputCallback() {
@Override
public void onInput(MaterialDialog dialog, CharSequence input) {
}
}).positiveText(R.string.btn_set).onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(MaterialDialog dialog, DialogAction which) {
final String flair = dialog.getInputEditText().getText().toString();
new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
try {
new ModerationManager(Authentication.reddit).setFlair(subreddit, t, flair, Authentication.name);
FlairTemplate currentF = m.getCurrentFlair(subreddit);
if (currentF.getText().isEmpty()) {
current = ("[" + currentF.getCssClass() + "]");
} else {
current = (currentF.getText());
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
protected void onPostExecute(Boolean done) {
Snackbar s;
if (done) {
if (current != null) {
((TextView) dialoglayout.findViewById(R.id.flair_text)).setText(getString(R.string.sidebar_flair, current));
}
s = Snackbar.make(mToolbar, R.string.snackbar_flair_success, Snackbar.LENGTH_SHORT);
} else {
s = Snackbar.make(mToolbar, R.string.snackbar_flair_error, Snackbar.LENGTH_SHORT);
}
if (s != null) {
View view = s.getView();
TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}).negativeText(R.string.btn_cancel).show();
} else {
new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
try {
new ModerationManager(Authentication.reddit).setFlair(subreddit, t, null, Authentication.name);
FlairTemplate currentF = m.getCurrentFlair(subreddit);
if (currentF.getText().isEmpty()) {
current = ("[" + currentF.getCssClass() + "]");
} else {
current = (currentF.getText());
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
protected void onPostExecute(Boolean done) {
Snackbar s;
if (done) {
if (current != null) {
((TextView) dialoglayout.findViewById(R.id.flair_text)).setText(getString(R.string.sidebar_flair, current));
}
s = Snackbar.make(mToolbar, R.string.snackbar_flair_success, Snackbar.LENGTH_SHORT);
} else {
s = Snackbar.make(mToolbar, R.string.snackbar_flair_error, Snackbar.LENGTH_SHORT);
}
if (s != null) {
View view = s.getView();
TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
}).show();
}
});
}
}
};
currentFlair.execute(dialoglayout.findViewById(R.id.flair));
}
} else {
if (drawerLayout != null) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END);
}
}
}
use of net.dean.jraw.managers.AccountManager in project Slide by ccrama.
the class Profile method onOptionsItemSelected.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case (android.R.id.home):
onBackPressed();
break;
case (R.id.category):
new AsyncTask<Void, Void, List<String>>() {
Dialog d;
@Override
public void onPreExecute() {
d = new MaterialDialog.Builder(Profile.this).progress(true, 100).content(R.string.misc_please_wait).title(R.string.profile_category_loading).show();
}
@Override
protected List<String> doInBackground(Void... params) {
try {
List<String> categories = new ArrayList<>(new AccountManager(Authentication.reddit).getSavedCategories());
categories.add(0, "No category");
return categories;
} catch (Exception e) {
e.printStackTrace();
// probably has no categories?
return new ArrayList<String>() {
{
add(0, "No category");
}
};
}
}
@Override
public void onPostExecute(final List<String> data) {
try {
new MaterialDialog.Builder(Profile.this).items(data).title(R.string.profile_category_select).itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, final View itemView, int which, CharSequence text) {
final String t = data.get(which);
if (which == 0)
category = null;
else
category = t;
int current = pager.getCurrentItem();
ProfilePagerAdapter adapter = new ProfilePagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
pager.setOffscreenPageLimit(1);
tabs.setupWithViewPager(pager);
pager.setCurrentItem(current);
}
}).show();
if (d != null) {
d.dismiss();
}
} catch (Exception ignored) {
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
break;
case (R.id.info):
if (account != null && trophyCase != null) {
LayoutInflater inflater = getLayoutInflater();
final View dialoglayout = inflater.inflate(R.layout.colorprofile, null);
AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(Profile.this);
final TextView title = (TextView) dialoglayout.findViewById(R.id.title);
title.setText(name);
dialoglayout.findViewById(R.id.share).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Reddit.defaultShareText(getString(R.string.profile_share, name), "https://www.reddit.com/u/" + name, Profile.this);
}
});
final int currentColor = Palette.getColorUser(name);
title.setBackgroundColor(currentColor);
String info = getString(R.string.profile_age, TimeUtils.getTimeSince(account.getCreated().getTime(), Profile.this));
/*todo better if (account.hasGold() &&account.getDataNode().has("gold_expiration") ) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(account.getDataNode().get("gold_expiration").asLong());
info.append("Gold expires on " + new SimpleDateFormat("dd/MM/yy").format(c.getTime()));
}*/
((TextView) dialoglayout.findViewById(R.id.moreinfo)).setText(info);
String tag = UserTags.getUserTag(name);
if (tag.isEmpty()) {
tag = getString(R.string.profile_tag_user);
} else {
tag = getString(R.string.profile_tag_user_existing, tag);
}
((TextView) dialoglayout.findViewById(R.id.tagged)).setText(tag);
LinearLayout l = (LinearLayout) dialoglayout.findViewById(R.id.trophies_inner);
dialoglayout.findViewById(R.id.tag).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MaterialDialog.Builder b = new MaterialDialog.Builder(Profile.this).title(getString(R.string.profile_tag_set, name)).input(getString(R.string.profile_tag), UserTags.getUserTag(name), false, new MaterialDialog.InputCallback() {
@Override
public void onInput(MaterialDialog dialog, CharSequence input) {
}
}).positiveText(R.string.profile_btn_tag).neutralText(R.string.btn_cancel);
if (UserTags.isUserTagged(name)) {
b.negativeText(R.string.profile_btn_untag);
}
b.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(MaterialDialog dialog, DialogAction which) {
UserTags.setUserTag(name, dialog.getInputEditText().getText().toString());
String tag = UserTags.getUserTag(name);
if (tag.isEmpty()) {
tag = getString(R.string.profile_tag_user);
} else {
tag = getString(R.string.profile_tag_user_existing, tag);
}
((TextView) dialoglayout.findViewById(R.id.tagged)).setText(tag);
}
}).onNeutral(null).onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(MaterialDialog dialog, DialogAction which) {
UserTags.removeUserTag(name);
String tag = UserTags.getUserTag(name);
if (tag.isEmpty()) {
tag = getString(R.string.profile_tag_user);
} else {
tag = getString(R.string.profile_tag_user_existing, tag);
}
((TextView) dialoglayout.findViewById(R.id.tagged)).setText(tag);
}
}).show();
}
});
if (trophyCase.isEmpty()) {
dialoglayout.findViewById(R.id.trophies).setVisibility(View.GONE);
} else {
for (final Trophy t : trophyCase) {
View view = getLayoutInflater().inflate(R.layout.trophy, null);
((Reddit) getApplicationContext()).getImageLoader().displayImage(t.getIcon(), ((ImageView) view.findViewById(R.id.image)));
((TextView) view.findViewById(R.id.trophyTitle)).setText(t.getFullName());
if (t.getAboutUrl() != null && !t.getAboutUrl().equalsIgnoreCase("null")) {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinkUtil.openUrl("https://reddit.com" + t.getAboutUrl(), Palette.getColorUser(account.getFullName()), Profile.this);
}
});
}
l.addView(view);
}
}
if (Authentication.isLoggedIn) {
dialoglayout.findViewById(R.id.pm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Profile.this, SendMessage.class);
i.putExtra(SendMessage.EXTRA_NAME, name);
startActivity(i);
}
});
friend = account.isFriend();
if (friend) {
((TextView) dialoglayout.findViewById(R.id.friend)).setText(R.string.profile_remove_friend);
} else {
((TextView) dialoglayout.findViewById(R.id.friend)).setText(R.string.profile_add_friend);
}
dialoglayout.findViewById(R.id.friend_body).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
if (friend) {
try {
new AccountManager(Authentication.reddit).deleteFriend(name);
} catch (Exception ignored) {
// Will throw java.lang.IllegalStateException: No Content-Type header was found, but it still works.
}
friend = false;
} else {
new AccountManager(Authentication.reddit).updateFriend(name);
friend = true;
}
return null;
}
@Override
public void onPostExecute(Void voids) {
if (friend) {
((TextView) dialoglayout.findViewById(R.id.friend)).setText(R.string.profile_remove_friend);
} else {
((TextView) dialoglayout.findViewById(R.id.friend)).setText(R.string.profile_add_friend);
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
dialoglayout.findViewById(R.id.block_body).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
Map<String, String> map = new HashMap();
map.put("account_id", "t2_" + account.getId());
try {
Authentication.reddit.execute(Authentication.reddit.request().post(map).path(String.format("/api/block_user")).build());
} catch (Exception ex) {
return false;
}
return true;
}
@Override
public void onPostExecute(Boolean blocked) {
if (!blocked) {
Toast.makeText(getBaseContext(), getString(R.string.err_block_user), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), getString(R.string.success_block_user), Toast.LENGTH_LONG).show();
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
} else {
dialoglayout.findViewById(R.id.pm).setVisibility(View.GONE);
}
dialoglayout.findViewById(R.id.multi_body).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent inte = new Intent(Profile.this, MultiredditOverview.class);
inte.putExtra(EXTRA_PROFILE, name);
Profile.this.startActivity(inte);
}
});
final View body = dialoglayout.findViewById(R.id.body2);
body.setVisibility(View.INVISIBLE);
final View center = dialoglayout.findViewById(R.id.colorExpandFrom);
dialoglayout.findViewById(R.id.color).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int cx = center.getWidth() / 2;
int cy = center.getHeight() / 2;
int finalRadius = Math.max(body.getWidth(), body.getHeight());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Animator anim = ViewAnimationUtils.createCircularReveal(body, cx, cy, 0, finalRadius);
body.setVisibility(View.VISIBLE);
anim.start();
} else {
body.setVisibility(View.VISIBLE);
}
}
});
LineColorPicker colorPicker = (LineColorPicker) dialoglayout.findViewById(R.id.picker);
final LineColorPicker colorPicker2 = (LineColorPicker) dialoglayout.findViewById(R.id.picker2);
colorPicker.setColors(ColorPreferences.getBaseColors(Profile.this));
colorPicker.setOnColorChangedListener(new OnColorChangedListener() {
@Override
public void onColorChanged(int c) {
colorPicker2.setColors(ColorPreferences.getColors(getBaseContext(), c));
colorPicker2.setSelectedColor(c);
}
});
for (int i : colorPicker.getColors()) {
for (int i2 : ColorPreferences.getColors(getBaseContext(), i)) {
if (i2 == currentColor) {
colorPicker.setSelectedColor(i);
colorPicker2.setColors(ColorPreferences.getColors(getBaseContext(), i));
colorPicker2.setSelectedColor(i2);
break;
}
}
}
colorPicker2.setOnColorChangedListener(new OnColorChangedListener() {
@Override
public void onColorChanged(int i) {
findViewById(R.id.header).setBackgroundColor(colorPicker2.getColor());
if (mToolbar != null)
mToolbar.setBackgroundColor(colorPicker2.getColor());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.setStatusBarColor(Palette.getDarkerColor(colorPicker2.getColor()));
}
title.setBackgroundColor(colorPicker2.getColor());
}
});
{
TextView dialogButton = (TextView) dialoglayout.findViewById(R.id.ok);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Palette.setColorUser(name, colorPicker2.getColor());
int cx = center.getWidth() / 2;
int cy = center.getHeight() / 2;
int initialRadius = body.getWidth();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Animator anim = ViewAnimationUtils.createCircularReveal(body, cx, cy, initialRadius, 0);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
body.setVisibility(View.GONE);
}
});
anim.start();
} else {
body.setVisibility(View.GONE);
}
}
});
}
{
final TextView dialogButton = (TextView) dialoglayout.findViewById(R.id.reset);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Palette.removeUserColor(name);
Snackbar.make(dialogButton, "User color removed", Snackbar.LENGTH_SHORT).show();
int cx = center.getWidth() / 2;
int cy = center.getHeight() / 2;
int initialRadius = body.getWidth();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Animator anim = ViewAnimationUtils.createCircularReveal(body, cx, cy, initialRadius, 0);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
body.setVisibility(View.GONE);
}
});
anim.start();
} else {
body.setVisibility(View.GONE);
}
}
});
}
((TextView) dialoglayout.findViewById(R.id.commentkarma)).setText(String.format(Locale.getDefault(), "%d", account.getCommentKarma()));
((TextView) dialoglayout.findViewById(R.id.linkkarma)).setText(String.format(Locale.getDefault(), "%d", account.getLinkKarma()));
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
findViewById(R.id.header).setBackgroundColor(currentColor);
if (mToolbar != null)
mToolbar.setBackgroundColor(currentColor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.setStatusBarColor(Palette.getDarkerColor(currentColor));
}
}
});
builder.setView(dialoglayout);
builder.show();
}
return true;
case (R.id.sort):
openPopup();
return true;
}
return false;
}
use of net.dean.jraw.managers.AccountManager in project Slide by ccrama.
the class SubredditView method doSubOnlyStuff.
private void doSubOnlyStuff(final Subreddit subreddit) {
if (!isFinishing()) {
findViewById(R.id.loader).setVisibility(View.GONE);
if (subreddit.getDataNode().has("subreddit_type") && !subreddit.getDataNode().get("subreddit_type").isNull()) {
canSubmit = !subreddit.getDataNode().get("subreddit_type").asText().toUpperCase().equals("RESTRICTED");
}
if (subreddit.getSidebar() != null && !subreddit.getSidebar().isEmpty()) {
findViewById(R.id.sidebar_text).setVisibility(View.VISIBLE);
final String text = subreddit.getDataNode().get("description_html").asText().trim();
final SpoilerRobotoTextView body = (SpoilerRobotoTextView) findViewById(R.id.sidebar_text);
CommentOverflow overflow = (CommentOverflow) findViewById(R.id.commentOverflow);
setViews(text, subreddit.getDisplayName(), body, overflow);
// get all subs that have Notifications enabled
ArrayList<String> rawSubs = Reddit.stringToArray(Reddit.appRestart.getString(CheckForMail.SUBS_TO_GET, ""));
HashMap<String, Integer> subThresholds = new HashMap<>();
for (String s : rawSubs) {
try {
String[] split = s.split(":");
subThresholds.put(split[0].toLowerCase(Locale.ENGLISH), Integer.valueOf(split[1]));
} catch (Exception ignored) {
// do nothing
}
}
// whether or not this subreddit was in the keySet
boolean isNotified = subThresholds.keySet().contains(subreddit.getDisplayName().toLowerCase(Locale.ENGLISH));
((AppCompatCheckBox) findViewById(R.id.notify_posts_state)).setChecked(isNotified);
} else {
findViewById(R.id.sidebar_text).setVisibility(View.GONE);
}
View collection = findViewById(R.id.collection);
if (Authentication.isLoggedIn) {
collection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<Void, Void, Void>() {
HashMap<String, MultiReddit> multis = new HashMap<String, MultiReddit>();
@Override
protected Void doInBackground(Void... params) {
if (UserSubscriptions.multireddits == null) {
UserSubscriptions.syncMultiReddits(SubredditView.this);
}
for (MultiReddit r : UserSubscriptions.multireddits) {
multis.put(r.getDisplayName(), r);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
new MaterialDialog.Builder(SubredditView.this).title("Add /r/" + subreddit.getDisplayName() + " to").items(multis.keySet()).itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View itemView, final int which, CharSequence text) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
final String multiName = multis.keySet().toArray(new String[multis.size()])[which];
List<String> subs = new ArrayList<String>();
for (MultiSubreddit sub : multis.get(multiName).getSubreddits()) {
subs.add(sub.getDisplayName());
}
subs.add(subreddit.getDisplayName());
new MultiRedditManager(Authentication.reddit).createOrUpdate(new MultiRedditUpdateRequest.Builder(Authentication.name, multiName).subreddits(subs).build());
UserSubscriptions.syncMultiReddits(SubredditView.this);
runOnUiThread(new Runnable() {
@Override
public void run() {
drawerLayout.closeDrawers();
Snackbar s = Snackbar.make(mToolbar, getString(R.string.multi_subreddit_added, multiName), Snackbar.LENGTH_LONG);
View view = s.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
});
} catch (final NetworkException | ApiException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Snackbar.make(mToolbar, getString(R.string.multi_error), Snackbar.LENGTH_LONG).setAction(R.string.btn_ok, new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}).show();
}
});
}
});
e.printStackTrace();
}
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}).show();
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
} else {
collection.setVisibility(View.GONE);
}
{
final TextView subscribe = (TextView) findViewById(R.id.subscribe);
currentlySubbed = (!Authentication.isLoggedIn && UserSubscriptions.getSubscriptions(this).contains(subreddit.getDisplayName().toLowerCase(Locale.ENGLISH))) || (Authentication.isLoggedIn && subreddit.isUserSubscriber());
doSubscribeButtonText(currentlySubbed, subscribe);
assert subscribe != null;
subscribe.setOnClickListener(new View.OnClickListener() {
private void doSubscribe() {
if (Authentication.isLoggedIn) {
new AlertDialogWrapper.Builder(SubredditView.this).setTitle(getString(R.string.subscribe_to, subreddit.getDisplayName())).setPositiveButton(R.string.reorder_add_subscribe, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new AsyncTask<Void, Void, Boolean>() {
@Override
public void onPostExecute(Boolean success) {
if (!success) {
// If subreddit was removed from account or not
new AlertDialogWrapper.Builder(SubredditView.this).setTitle(R.string.force_change_subscription).setMessage(R.string.force_change_subscription_desc).setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
changeSubscription(subreddit, // Force add the subscription
true);
Snackbar s = Snackbar.make(mToolbar, getString(R.string.misc_subscribed), Snackbar.LENGTH_SHORT);
View view = s.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
}).setNegativeButton(R.string.btn_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setCancelable(false).show();
} else {
changeSubscription(subreddit, true);
}
}
@Override
protected Boolean doInBackground(Void... params) {
try {
new AccountManager(Authentication.reddit).subscribe(subreddit);
} catch (NetworkException e) {
// Either network crashed or trying to unsubscribe to a subreddit that the account isn't subscribed to
return false;
}
return true;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}).setNegativeButton(R.string.btn_cancel, null).setNeutralButton(R.string.btn_add_to_sublist, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
changeSubscription(subreddit, // Force add the subscription
true);
Snackbar s = Snackbar.make(mToolbar, R.string.sub_added, Snackbar.LENGTH_SHORT);
View view = s.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
}).show();
} else {
changeSubscription(subreddit, true);
}
}
@Override
public void onClick(View v) {
if (!currentlySubbed) {
doSubscribe();
doSubscribeButtonText(currentlySubbed, subscribe);
} else {
doUnsubscribe();
doSubscribeButtonText(currentlySubbed, subscribe);
}
}
private void doUnsubscribe() {
if (Authentication.didOnline) {
new AlertDialogWrapper.Builder(SubredditView.this).setTitle(getString(R.string.unsubscribe_from, subreddit.getDisplayName())).setPositiveButton(R.string.reorder_remove_unsubsribe, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new AsyncTask<Void, Void, Boolean>() {
@Override
public void onPostExecute(Boolean success) {
if (!success) {
// If subreddit was removed from account or not
new AlertDialogWrapper.Builder(SubredditView.this).setTitle(R.string.force_change_subscription).setMessage(R.string.force_change_subscription_desc).setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
changeSubscription(subreddit, // Force add the subscription
false);
Snackbar s = Snackbar.make(mToolbar, getString(R.string.misc_unsubscribed), Snackbar.LENGTH_SHORT);
View view = s.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
}).setNegativeButton(R.string.btn_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setCancelable(false).show();
} else {
changeSubscription(subreddit, false);
}
}
@Override
protected Boolean doInBackground(Void... params) {
try {
new AccountManager(Authentication.reddit).unsubscribe(subreddit);
} catch (NetworkException e) {
// Either network crashed or trying to unsubscribe to a subreddit that the account isn't subscribed to
return false;
}
return true;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}).setNeutralButton(R.string.just_unsub, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
changeSubscription(subreddit, // Force add the subscription
false);
Snackbar s = Snackbar.make(mToolbar, R.string.misc_unsubscribed, Snackbar.LENGTH_SHORT);
View view = s.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
}).setNegativeButton(R.string.btn_cancel, null).show();
} else {
changeSubscription(subreddit, false);
}
}
});
}
{
final AppCompatCheckBox notifyStateCheckBox = (AppCompatCheckBox) findViewById(R.id.notify_posts_state);
assert notifyStateCheckBox != null;
notifyStateCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
final String sub = subreddit.getDisplayName();
if (!sub.equalsIgnoreCase("all") && !sub.equalsIgnoreCase("frontpage") && !sub.equalsIgnoreCase("friends") && !sub.equalsIgnoreCase("mod") && !sub.contains("+") && !sub.contains(".") && !sub.contains("/m/")) {
new AlertDialogWrapper.Builder(SubredditView.this).setTitle(getString(R.string.sub_post_notifs_title, sub)).setMessage(R.string.sub_post_notifs_msg).setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new MaterialDialog.Builder(SubredditView.this).title(R.string.sub_post_notifs_threshold).items(new String[] { "1", "5", "10", "20", "40", "50" }).alwaysCallSingleChoiceCallback().itemsCallbackSingleChoice(0, new MaterialDialog.ListCallbackSingleChoice() {
@Override
public boolean onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
ArrayList<String> subs = Reddit.stringToArray(Reddit.appRestart.getString(CheckForMail.SUBS_TO_GET, ""));
subs.add(sub + ":" + text);
Reddit.appRestart.edit().putString(CheckForMail.SUBS_TO_GET, Reddit.arrayToString(subs)).commit();
return true;
}
}).cancelable(false).show();
}
}).setNegativeButton(R.string.btn_cancel, null).setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
notifyStateCheckBox.setChecked(false);
}
}).setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
notifyStateCheckBox.setChecked(false);
}
}).show();
} else {
notifyStateCheckBox.setChecked(false);
Toast.makeText(SubredditView.this, R.string.sub_post_notifs_err, Toast.LENGTH_SHORT).show();
}
} else {
Intent cancelIntent = new Intent(SubredditView.this, CancelSubNotifs.class);
cancelIntent.putExtra(CancelSubNotifs.EXTRA_SUB, subreddit.getDisplayName());
startActivity(cancelIntent);
}
}
});
}
if (!subreddit.getPublicDescription().isEmpty()) {
findViewById(R.id.sub_title).setVisibility(View.VISIBLE);
setViews(subreddit.getDataNode().get("public_description_html").asText(), subreddit.getDisplayName().toLowerCase(Locale.ENGLISH), ((SpoilerRobotoTextView) findViewById(R.id.sub_title)), (CommentOverflow) findViewById(R.id.sub_title_overflow));
} else {
findViewById(R.id.sub_title).setVisibility(View.GONE);
}
if (subreddit.getDataNode().has("icon_img") && !subreddit.getDataNode().get("icon_img").asText().isEmpty()) {
((Reddit) getApplication()).getImageLoader().displayImage(subreddit.getDataNode().get("icon_img").asText(), (ImageView) findViewById(R.id.subimage));
} else {
findViewById(R.id.subimage).setVisibility(View.GONE);
}
((TextView) findViewById(R.id.subscribers)).setText(getString(R.string.subreddit_subscribers_string, subreddit.getLocalizedSubscriberCount()));
findViewById(R.id.subscribers).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.active_users)).setText(getString(R.string.subreddit_active_users_string_new, subreddit.getLocalizedAccountsActive()));
findViewById(R.id.active_users).setVisibility(View.VISIBLE);
}
}
use of net.dean.jraw.managers.AccountManager in project Slide by ccrama.
the class CommentAdapterHelper method categorizeComment.
private static void categorizeComment(final Comment comment, final Context mContext) {
new AsyncTask<Void, Void, List<String>>() {
Dialog d;
@Override
public void onPreExecute() {
d = new MaterialDialog.Builder(mContext).progress(true, 100).content(R.string.misc_please_wait).title(R.string.profile_category_loading).show();
}
@Override
protected List<String> doInBackground(Void... params) {
try {
List<String> categories = new ArrayList<String>(new AccountManager(Authentication.reddit).getSavedCategories());
categories.add("New category");
return categories;
} catch (Exception e) {
e.printStackTrace();
return new ArrayList<String>() {
{
add("New category");
}
};
}
}
@Override
public void onPostExecute(final List<String> data) {
try {
new MaterialDialog.Builder(mContext).items(data).title(R.string.sidebar_select_flair).itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, final View itemView, int which, CharSequence text) {
final String t = data.get(which);
if (which == data.size() - 1) {
new MaterialDialog.Builder(mContext).title(R.string.category_set_name).input(mContext.getString(R.string.category_set_name_hint), null, false, new MaterialDialog.InputCallback() {
@Override
public void onInput(MaterialDialog dialog, CharSequence input) {
}
}).positiveText(R.string.btn_set).onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(MaterialDialog dialog, DialogAction which) {
final String flair = dialog.getInputEditText().getText().toString();
new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
try {
new AccountManager(Authentication.reddit).save(comment, flair);
return true;
} catch (ApiException e) {
e.printStackTrace();
return false;
}
}
@Override
protected void onPostExecute(Boolean done) {
Snackbar s;
if (done) {
if (itemView != null) {
s = Snackbar.make(itemView, R.string.submission_info_saved, Snackbar.LENGTH_SHORT);
View view = s.getView();
TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
} else {
if (itemView != null) {
s = Snackbar.make(itemView, R.string.category_set_error, Snackbar.LENGTH_SHORT);
View view = s.getView();
TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
}
}
}.execute();
}
}).negativeText(R.string.btn_cancel).show();
} else {
new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
try {
new AccountManager(Authentication.reddit).save(comment, t);
return true;
} catch (ApiException e) {
e.printStackTrace();
return false;
}
}
@Override
protected void onPostExecute(Boolean done) {
Snackbar s;
if (done) {
if (itemView != null) {
s = Snackbar.make(itemView, R.string.submission_info_saved, Snackbar.LENGTH_SHORT);
View view = s.getView();
TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
} else {
if (itemView != null) {
s = Snackbar.make(itemView, R.string.category_set_error, Snackbar.LENGTH_SHORT);
View view = s.getView();
TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
s.show();
}
}
}
}.execute();
}
}
}).show();
if (d != null) {
d.dismiss();
}
} catch (Exception ignored) {
}
}
}.execute();
}
Aggregations