use of net.dean.jraw.managers.AccountManager in project Slide by ccrama.
the class PopulateSubmissionViewHolder method categorizeSaved.
private void categorizeSaved(final Submission submission, View itemView, final Context mContext) {
new AsyncTask<Void, Void, List<String>>() {
Dialog d;
@Override
public void onPreExecute() {
d = new MaterialDialog.Builder(mContext).progress(true, 100).title(R.string.profile_category_loading).content(R.string.misc_please_wait).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");
}
};
// sub probably has no flairs?
}
}
@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(submission, 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();
}
}
}
}.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 AccountManager(Authentication.reddit).save(submission, 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();
}
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
}).show();
if (d != null) {
d.dismiss();
}
} catch (Exception ignored) {
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Aggregations