use of com.winsonchiu.reader.data.reddit.Thing in project Reader by TheKeeperOfPie.
the class ControllerSearch method setNsfwLinks.
public void setNsfwLinks(String name, boolean over18) {
for (int index = 0; index < links.getChildren().size(); index++) {
Thing thing = links.getChildren().get(index);
if (thing.getName().equals(name)) {
((Link) thing).setOver18(over18);
eventHolder.callLinks(new RxAdapterEvent<>(getLinksModel(), RxAdapterEvent.Type.CHANGE, index + 1));
return;
}
}
}
use of com.winsonchiu.reader.data.reddit.Thing in project Reader by TheKeeperOfPie.
the class ControllerProfile method getViewType.
public int getViewType(int position) {
List<Thing> children = data.getChildren();
if (position < 0 || position >= children.size()) {
throw new IndexOutOfBoundsException("ControllerProfile position invalid: " + position);
}
Thing thing = children.get(position);
if (thing instanceof Link) {
return VIEW_TYPE_LINK;
} else if (thing instanceof Comment) {
return VIEW_TYPE_COMMENT;
}
throw new IllegalStateException(thing + " is not a valid view type");
}
use of com.winsonchiu.reader.data.reddit.Thing in project Reader by TheKeeperOfPie.
the class ControllerInbox method setReplyText.
public boolean setReplyText(String name, String text, boolean collapsed) {
for (int index = 0; index < data.getChildren().size(); index++) {
Thing thing = data.getChildren().get(index);
if (thing.getName().equals(name)) {
((Replyable) thing).setReplyText(text);
((Replyable) thing).setReplyExpanded(!collapsed);
for (Listener listener : listeners) {
listener.getAdapter().notifyItemChanged(index);
}
return true;
}
}
return false;
}
use of com.winsonchiu.reader.data.reddit.Thing in project Reader by TheKeeperOfPie.
the class Receiver method checkInbox.
public void checkInbox(final Context context, @Nullable final ArrayList<String> names) {
final ArrayList<String> readNames;
if (names == null) {
readNames = new ArrayList<>();
} else {
readNames = names;
}
new Thread(new Runnable() {
@Override
public void run() {
final Listing messages = new Listing();
Account[] accounts = accountManager.getAccountsByType(Reddit.ACCOUNT_TYPE);
for (Account account : accounts) {
final AccountManagerFuture<Bundle> futureAuth = accountManager.getAuthToken(account, Reddit.AUTH_TOKEN_FULL_ACCESS, null, true, null, null);
try {
Bundle bundle = futureAuth.getResult();
final String tokenAuth = bundle.getString(AccountManager.KEY_AUTHTOKEN);
Request request = new Request.Builder().url(Reddit.OAUTH_URL + "/message/unread").header(Reddit.USER_AGENT, Reddit.CUSTOM_USER_AGENT).header(Reddit.AUTHORIZATION, Reddit.BEARER + tokenAuth).header(Reddit.CONTENT_TYPE, Reddit.CONTENT_TYPE_APP_JSON).get().build();
String response = okHttpClient.newCall(request).execute().body().string();
Listing listing = Listing.fromJson(ComponentStatic.getObjectMapper().readValue(response, JsonNode.class));
messages.addChildren(listing.getChildren());
Log.d(TAG, account.name + " checkInbox response: " + response);
} catch (OperationCanceledException | AuthenticatorException | IOException e) {
e.printStackTrace();
}
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
Thing thing = null;
for (int index = 0; index < messages.getChildren().size(); index++) {
thing = messages.getChildren().get(index);
if (readNames.contains(thing.getName())) {
reddit.markRead(thing.getName()).subscribe(new ObserverEmpty<>());
thing = null;
} else {
readNames.add(thing.getName());
break;
}
}
if (thing == null) {
notificationManager.cancel(NOTIFICATION_INBOX);
return;
}
int titleSuffixResource = messages.getChildren().size() == 1 ? R.string.new_message : R.string.new_messages;
CharSequence content = "";
CharSequence author = "";
CharSequence dest = "";
if (thing instanceof Message) {
content = ((Message) thing).getBodyHtml();
author = ((Message) thing).getAuthor();
dest = ((Message) thing).getDest();
} else if (thing instanceof Comment) {
content = ((Comment) thing).getBodyHtml();
author = ((Comment) thing).getAuthor();
dest = ((Comment) thing).getDest();
}
Intent intentActivity = new Intent(context, ActivityMain.class);
intentActivity.putExtra(ActivityMain.ACCOUNT, dest);
intentActivity.putExtra(ActivityMain.NAV_ID, R.id.item_inbox);
intentActivity.putExtra(ActivityMain.NAV_PAGE, ControllerInbox.UNREAD);
PendingIntent pendingIntentActivity = PendingIntent.getActivity(context, 0, intentActivity, PendingIntent.FLAG_CANCEL_CURRENT);
Intent intentRecheckInbox = new Intent(INTENT_INBOX);
intentRecheckInbox.putExtra(READ_NAMES, readNames);
PendingIntent pendingIntentRecheckInbox = PendingIntent.getBroadcast(context, 0, intentRecheckInbox, PendingIntent.FLAG_CANCEL_CURRENT);
Themer themer = new Themer(context);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setSmallIcon(R.mipmap.app_icon_white_outline).setContentTitle(messages.getChildren().size() + " " + context.getResources().getString(titleSuffixResource)).setContentText(context.getString(R.string.expand_to_read_first_message)).setStyle(new NotificationCompat.BigTextStyle().setSummaryText(context.getString(R.string.from) + " /u/" + author).bigText(content)).setContentIntent(pendingIntentActivity).addAction(new NotificationCompat.Action(R.drawable.ic_check_white_24dp, context.getString(R.string.mark_read), pendingIntentRecheckInbox)).setDeleteIntent(pendingIntentRecheckInbox).setAutoCancel(true).setCategory(NotificationCompat.CATEGORY_EMAIL).setColor(themer.getColorPrimary()).setLights(themer.getColorPrimary(), LED_MS_ON, LED_MS_OFF);
notificationManager.notify(NOTIFICATION_INBOX, builder.build());
}
}).start();
}
use of com.winsonchiu.reader.data.reddit.Thing in project Reader by TheKeeperOfPie.
the class ControllerSearch method reloadSubreddits.
public void reloadSubreddits() {
if (subscriptionSubreddits != null && !subscriptionSubreddits.isUnsubscribed()) {
subscriptionSubreddits.unsubscribe();
subscriptionSubreddits = null;
}
// TODO: Move this to asynchronous with Thread cancelling
String queryTrimmed = query.toLowerCase().replaceAll("\\s", "");
Listing subscribedResults = new Listing();
List<Thing> resultsThatContainQueryInTitle = new ArrayList<>();
List<Thing> resultsThatContainQueryInDescription = new ArrayList<>();
for (Thing thing : subredditsSubscribed.getChildren()) {
Subreddit subreddit = (Subreddit) thing;
if (subreddit.getDisplayName().toLowerCase().startsWith(queryTrimmed)) {
subscribedResults.getChildren().add(subreddit);
} else if (subreddit.getDisplayName().toLowerCase().contains(queryTrimmed)) {
resultsThatContainQueryInTitle.add(subreddit);
} else if (subreddit.getPublicDescription().toLowerCase().contains(queryTrimmed)) {
resultsThatContainQueryInDescription.add(subreddit);
}
}
subscribedResults.addChildren(resultsThatContainQueryInTitle);
subscribedResults.addChildren(resultsThatContainQueryInDescription);
subreddits = subscribedResults;
for (Listener listener : listeners) {
listener.getAdapterSearchSubreddits().notifyDataSetChanged();
}
try {
subscriptionSubreddits = reddit.subredditsSearch(URLEncoder.encode(query, Reddit.UTF_8).replaceAll("\\s", ""), sort.toString()).flatMap(UtilsRx.flatMapWrapError(response -> Listing.fromJson(ComponentStatic.getObjectMapper().readValue(response, JsonNode.class)))).subscribe(new Observer<Listing>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(Listing listing) {
Iterator<Thing> iterator = listing.getChildren().iterator();
while (iterator.hasNext()) {
Subreddit subreddit = (Subreddit) iterator.next();
if (subreddit.getSubredditType().equalsIgnoreCase(Subreddit.PRIVATE) && !subreddit.isUserIsContributor()) {
iterator.remove();
}
}
subreddits.addChildren(listing.getChildren());
for (final Listener listener : listeners) {
listener.getAdapterSearchSubreddits().notifyDataSetChanged();
}
}
});
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
Aggregations