Test WorkManager. Still implementing PM.

This commit is contained in:
Alex Ning 2020-06-29 12:00:06 +08:00
parent 3c482c63ec
commit d1fc84f9d0
4 changed files with 66 additions and 17 deletions

View File

@ -23,6 +23,7 @@ import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayout;
import com.r0adkll.slidr.Slidr; import com.r0adkll.slidr.Slidr;
import com.r0adkll.slidr.model.SlidrInterface;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.Subscribe;
@ -75,6 +76,7 @@ public class ViewMessageActivity extends BaseActivity implements ActivityToolbar
SharedPreferences mSharedPreferences; SharedPreferences mSharedPreferences;
@Inject @Inject
CustomThemeWrapper mCustomThemeWrapper; CustomThemeWrapper mCustomThemeWrapper;
private SlidrInterface mSlidrInterface;
private SectionsPagerAdapter sectionsPagerAdapter; private SectionsPagerAdapter sectionsPagerAdapter;
private boolean mNullAccessToken = false; private boolean mNullAccessToken = false;
private String mAccessToken; private String mAccessToken;
@ -95,7 +97,7 @@ public class ViewMessageActivity extends BaseActivity implements ActivityToolbar
applyCustomTheme(); applyCustomTheme();
if (mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_RIGHT_TO_GO_BACK_FROM_POST_DETAIL, true)) { 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) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
@ -183,6 +185,16 @@ public class ViewMessageActivity extends BaseActivity implements ActivityToolbar
private void bindView() { private void bindView() {
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 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.setAdapter(sectionsPagerAdapter);
viewPager.setOffscreenPageLimit(2); viewPager.setOffscreenPageLimit(2);
tabLayout.setupWithViewPager(viewPager); 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 class SectionsPagerAdapter extends FragmentPagerAdapter {
private ViewMessagesFragment tab1; private ViewMessagesFragment tab1;
private ViewMessagesFragment tab2; private ViewMessagesFragment tab2;

View File

@ -15,8 +15,8 @@ import java.util.Calendar;
import java.util.Locale; import java.util.Locale;
import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.API.RedditAPI;
import ml.docilealligator.infinityforreddit.Utils.JSONUtils;
import ml.docilealligator.infinityforreddit.Utils.APIUtils; import ml.docilealligator.infinityforreddit.Utils.APIUtils;
import ml.docilealligator.infinityforreddit.Utils.JSONUtils;
import ml.docilealligator.infinityforreddit.Utils.Utils; import ml.docilealligator.infinityforreddit.Utils.Utils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
@ -54,15 +54,7 @@ public class FetchMessages {
}); });
} }
static ArrayList<Message> parseMessage(String response, Locale locale, int messageType) { static ArrayList<Message> parseMessage(JSONArray messageArray, 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;
}
ArrayList<Message> messages = new ArrayList<>(); ArrayList<Message> messages = new ArrayList<>();
for (int i = 0; i < messageArray.length(); i++) { for (int i = 0; i < messageArray.length(); i++) {
try { try {
@ -96,9 +88,20 @@ public class FetchMessages {
String formattedTime = new SimpleDateFormat("MMM d, yyyy, HH:mm", String formattedTime = new SimpleDateFormat("MMM d, yyyy, HH:mm",
locale).format(submitTimeCalendar.getTime()); locale).format(submitTimeCalendar.getTime());
messages.add(new Message(kind, subredditName, subredditNamePrefixed, id, fullname, subject, ArrayList<Message> 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, 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) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -131,8 +134,9 @@ public class FetchMessages {
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
messages = parseMessage(response, locale, messageType);
try { 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); after = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -1,5 +1,7 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import java.util.ArrayList;
public class Message { public class Message {
static final String TYPE_COMMENT = "t1"; static final String TYPE_COMMENT = "t1";
static final String TYPE_ACCOUNT = "t2"; static final String TYPE_ACCOUNT = "t2";
@ -26,6 +28,7 @@ public class Message {
private int score; private int score;
private int nComments; private int nComments;
private long timeUTC; private long timeUTC;
private ArrayList<Message> replies;
Message(String kind, String subredditName, String subredditNamePrefixed, String id, String fullname, Message(String kind, String subredditName, String subredditNamePrefixed, String id, String fullname,
String subject, String author, String parentFullName, String title, String body, String context, String subject, String author, String parentFullName, String title, String body, String context,
@ -127,4 +130,12 @@ public class Message {
public long getTimeUTC() { public long getTimeUTC() {
return timeUTC; return timeUTC;
} }
public ArrayList<Message> getReplies() {
return replies;
}
public void setReplies(ArrayList<Message> replies) {
this.replies = replies;
}
} }

View File

@ -12,6 +12,7 @@ import androidx.core.app.NotificationManagerCompat;
import androidx.work.Worker; import androidx.work.Worker;
import androidx.work.WorkerParameters; import androidx.work.WorkerParameters;
import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -31,6 +32,7 @@ import ml.docilealligator.infinityforreddit.Activity.LinkResolverActivity;
import ml.docilealligator.infinityforreddit.Activity.ViewMessageActivity; import ml.docilealligator.infinityforreddit.Activity.ViewMessageActivity;
import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.Utils.APIUtils; import ml.docilealligator.infinityforreddit.Utils.APIUtils;
import ml.docilealligator.infinityforreddit.Utils.JSONUtils;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Response; import retrofit2.Response;
@ -65,6 +67,13 @@ public class PullNotificationWorker extends Worker {
try { try {
List<Account> accounts = mRedditDataRoomDatabase.accountDao().getAllAccounts(); List<Account> accounts = mRedditDataRoomDatabase.accountDao().getAllAccounts();
int color = mCustomThemeWrapper.getColorPrimaryLightTheme(); 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++) { for (int accountIndex = 0; accountIndex < accounts.size(); accountIndex++) {
Account account = accounts.get(accountIndex); Account account = accounts.get(accountIndex);
@ -72,12 +81,13 @@ public class PullNotificationWorker extends Worker {
Response<String> response = fetchMessages(account, 1); Response<String> response = fetchMessages(account, 1);
if (response != null && response.isSuccessful()) { if (response != null && response.isSuccessful() && response.body() != null) {
String responseBody = response.body(); String responseBody = response.body();
ArrayList<Message> messages = FetchMessages.parseMessage(responseBody, JSONArray messageArray = new JSONObject(responseBody).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
ArrayList<Message> messages = FetchMessages.parseMessage(messageArray,
context.getResources().getConfiguration().locale, FetchMessages.MESSAGE_TYPE_NOTIFICATION); context.getResources().getConfiguration().locale, FetchMessages.MESSAGE_TYPE_NOTIFICATION);
if (messages != null && !messages.isEmpty()) { if (!messages.isEmpty()) {
NotificationManagerCompat notificationManager = NotificationUtils.getNotificationManager(context); NotificationManagerCompat notificationManager = NotificationUtils.getNotificationManager(context);
NotificationCompat.Builder summaryBuilder = NotificationUtils.buildSummaryNotification(context, NotificationCompat.Builder summaryBuilder = NotificationUtils.buildSummaryNotification(context,