From d1fc84f9d08f17d14fb1d86cddd9f0bea8ebde07 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 29 Jun 2020 12:00:06 +0800 Subject: Test WorkManager. Still implementing PM. --- .../Activity/ViewMessageActivity.java | 26 ++++++++++++++++++- .../infinityforreddit/FetchMessages.java | 30 ++++++++++++---------- .../docilealligator/infinityforreddit/Message.java | 11 ++++++++ .../infinityforreddit/PullNotificationWorker.java | 16 +++++++++--- 4 files changed, 66 insertions(+), 17 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMessageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMessageActivity.java index 8f049922..ecc33a6c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMessageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMessageActivity.java @@ -23,6 +23,7 @@ import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.tabs.TabLayout; import com.r0adkll.slidr.Slidr; +import com.r0adkll.slidr.model.SlidrInterface; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; @@ -75,6 +76,7 @@ public class ViewMessageActivity extends BaseActivity implements ActivityToolbar SharedPreferences mSharedPreferences; @Inject CustomThemeWrapper mCustomThemeWrapper; + private SlidrInterface mSlidrInterface; private SectionsPagerAdapter sectionsPagerAdapter; private boolean mNullAccessToken = false; private String mAccessToken; @@ -95,7 +97,7 @@ public class ViewMessageActivity extends BaseActivity implements ActivityToolbar applyCustomTheme(); if (mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_RIGHT_TO_GO_BACK_FROM_POST_DETAIL, true)) { - Slidr.attach(this); + mSlidrInterface = Slidr.attach(this); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { @@ -183,6 +185,16 @@ public class ViewMessageActivity extends BaseActivity implements ActivityToolbar private void bindView() { sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); + viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { + @Override + public void onPageSelected(int position) { + if (position == 0) { + unlockSwipeRightToGoBack(); + } else { + lockSwipeRightToGoBack(); + } + } + }); viewPager.setAdapter(sectionsPagerAdapter); viewPager.setOffscreenPageLimit(2); tabLayout.setupWithViewPager(viewPager); @@ -237,6 +249,18 @@ public class ViewMessageActivity extends BaseActivity implements ActivityToolbar } } + private void lockSwipeRightToGoBack() { + if (mSlidrInterface != null) { + mSlidrInterface.lock(); + } + } + + private void unlockSwipeRightToGoBack() { + if (mSlidrInterface != null) { + mSlidrInterface.unlock(); + } + } + private class SectionsPagerAdapter extends FragmentPagerAdapter { private ViewMessagesFragment tab1; private ViewMessagesFragment tab2; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMessages.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMessages.java index 942d7f7a..17937af2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMessages.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMessages.java @@ -15,8 +15,8 @@ import java.util.Calendar; import java.util.Locale; import ml.docilealligator.infinityforreddit.API.RedditAPI; -import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import ml.docilealligator.infinityforreddit.Utils.APIUtils; +import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; import retrofit2.Call; import retrofit2.Callback; @@ -54,15 +54,7 @@ public class FetchMessages { }); } - static ArrayList parseMessage(String response, Locale locale, int messageType) { - JSONArray messageArray; - try { - messageArray = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); - } catch (JSONException e) { - e.printStackTrace(); - return null; - } - + static ArrayList parseMessage(JSONArray messageArray, Locale locale, int messageType) { ArrayList messages = new ArrayList<>(); for (int i = 0; i < messageArray.length(); i++) { try { @@ -96,9 +88,20 @@ public class FetchMessages { String formattedTime = new SimpleDateFormat("MMM d, yyyy, HH:mm", locale).format(submitTimeCalendar.getTime()); - messages.add(new Message(kind, subredditName, subredditNamePrefixed, id, fullname, subject, + ArrayList replies = null; + if (!rawMessageJSON.isNull(JSONUtils.REPLIES_KEY) && rawMessageJSON.get(JSONUtils.REPLIES_KEY) instanceof JSONObject) { + JSONArray repliesArray = rawMessageJSON.getJSONObject(JSONUtils.REPLIES_KEY).getJSONObject(JSONUtils.DATA_KEY) + .getJSONArray(JSONUtils.CHILDREN_KEY); + replies = parseMessage(repliesArray, locale, messageType); + } + + Message message = new Message(kind, subredditName, subredditNamePrefixed, id, fullname, subject, author, parentFullname, title, body, context, distinguished, formattedTime, - wasComment, isNew, score, nComments, timeUTC)); + wasComment, isNew, score, nComments, timeUTC); + if (replies != null) { + message.setReplies(replies); + } + messages.add(message); } catch (JSONException e) { e.printStackTrace(); } @@ -131,8 +134,9 @@ public class FetchMessages { @Override protected Void doInBackground(Void... voids) { - messages = parseMessage(response, locale, messageType); try { + JSONArray messageArray = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); + messages = parseMessage(messageArray, locale, messageType); after = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); } catch (JSONException e) { e.printStackTrace(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Message.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Message.java index 1ba1aea9..d45385a1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Message.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Message.java @@ -1,5 +1,7 @@ package ml.docilealligator.infinityforreddit; +import java.util.ArrayList; + public class Message { static final String TYPE_COMMENT = "t1"; static final String TYPE_ACCOUNT = "t2"; @@ -26,6 +28,7 @@ public class Message { private int score; private int nComments; private long timeUTC; + private ArrayList replies; Message(String kind, String subredditName, String subredditNamePrefixed, String id, String fullname, String subject, String author, String parentFullName, String title, String body, String context, @@ -127,4 +130,12 @@ public class Message { public long getTimeUTC() { return timeUTC; } + + public ArrayList getReplies() { + return replies; + } + + public void setReplies(ArrayList replies) { + this.replies = replies; + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java index 2473d074..ef831e4b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java @@ -12,6 +12,7 @@ import androidx.core.app.NotificationManagerCompat; import androidx.work.Worker; import androidx.work.WorkerParameters; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -31,6 +32,7 @@ import ml.docilealligator.infinityforreddit.Activity.LinkResolverActivity; import ml.docilealligator.infinityforreddit.Activity.ViewMessageActivity; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Utils.APIUtils; +import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import retrofit2.Call; import retrofit2.Response; @@ -65,6 +67,13 @@ public class PullNotificationWorker extends Worker { try { List accounts = mRedditDataRoomDatabase.accountDao().getAllAccounts(); int color = mCustomThemeWrapper.getColorPrimaryLightTheme(); + NotificationManagerCompat testManager = NotificationUtils.getNotificationManager(context); + NotificationCompat.Builder test = NotificationUtils.buildNotification(testManager, + context, "Test", "Test body", "Test summary", + NotificationUtils.CHANNEL_ID_NEW_MESSAGES, + NotificationUtils.CHANNEL_NEW_MESSAGES, + NotificationUtils.getAccountGroupName("Test"), color); + testManager.notify(9765, test.build()); for (int accountIndex = 0; accountIndex < accounts.size(); accountIndex++) { Account account = accounts.get(accountIndex); @@ -72,12 +81,13 @@ public class PullNotificationWorker extends Worker { Response response = fetchMessages(account, 1); - if (response != null && response.isSuccessful()) { + if (response != null && response.isSuccessful() && response.body() != null) { String responseBody = response.body(); - ArrayList messages = FetchMessages.parseMessage(responseBody, + JSONArray messageArray = new JSONObject(responseBody).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); + ArrayList messages = FetchMessages.parseMessage(messageArray, context.getResources().getConfiguration().locale, FetchMessages.MESSAGE_TYPE_NOTIFICATION); - if (messages != null && !messages.isEmpty()) { + if (!messages.isEmpty()) { NotificationManagerCompat notificationManager = NotificationUtils.getNotificationManager(context); NotificationCompat.Builder summaryBuilder = NotificationUtils.buildSummaryNotification(context, -- cgit v1.2.3