mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-24 00:44:43 +01:00
Fetch all users' messages in PullNotificationWorker. Update the access token in database instead of inserting an account in AccessTokenAuthenticator to hopefully fix the problem that subscriptions database is cleared implicitly.
This commit is contained in:
parent
a83aaa671a
commit
85d232c7f6
@ -13,6 +13,9 @@ public interface AccountDao {
|
|||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
void insert(Account account);
|
void insert(Account account);
|
||||||
|
|
||||||
|
@Query("SELECT * FROM accounts")
|
||||||
|
List<Account> getAllAccounts();
|
||||||
|
|
||||||
@Query("SELECT * FROM accounts WHERE is_current_user = 0")
|
@Query("SELECT * FROM accounts WHERE is_current_user = 0")
|
||||||
List<Account> getAllNonCurrentAccounts();
|
List<Account> getAllNonCurrentAccounts();
|
||||||
|
|
||||||
@ -43,4 +46,7 @@ public interface AccountDao {
|
|||||||
|
|
||||||
@Query("UPDATE accounts SET is_current_user = 1 WHERE username = :username")
|
@Query("UPDATE accounts SET is_current_user = 1 WHERE username = :username")
|
||||||
void markAccountCurrent(String username);
|
void markAccountCurrent(String username);
|
||||||
|
|
||||||
|
@Query("UPDATE accounts SET access_token = :accessToken WHERE username = :username")
|
||||||
|
void changeAccessToken(String username, String accessToken);
|
||||||
}
|
}
|
||||||
|
@ -68,17 +68,15 @@ class AccessTokenAuthenticator implements Authenticator {
|
|||||||
Call<String> accessTokenCall = api.getAccessToken(RedditUtils.getHttpBasicAuthHeader(), params);
|
Call<String> accessTokenCall = api.getAccessToken(RedditUtils.getHttpBasicAuthHeader(), params);
|
||||||
try {
|
try {
|
||||||
retrofit2.Response response = accessTokenCall.execute();
|
retrofit2.Response response = accessTokenCall.execute();
|
||||||
if(response.body() == null) {
|
if(response.isSuccessful() && response.body() != null) {
|
||||||
return "";
|
JSONObject jsonObject = new JSONObject((String) response.body());
|
||||||
|
String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY);
|
||||||
|
mRedditDataRoomDatabase.accountDao().changeAccessToken(account.getUsername(), newAccessToken);
|
||||||
|
|
||||||
|
Log.i("access token", newAccessToken);
|
||||||
|
return newAccessToken;
|
||||||
}
|
}
|
||||||
|
return "";
|
||||||
JSONObject jsonObject = new JSONObject((String) response.body());
|
|
||||||
String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY);
|
|
||||||
account.setAccessToken(newAccessToken);
|
|
||||||
mRedditDataRoomDatabase.accountDao().insert(account);
|
|
||||||
|
|
||||||
Log.i("access token", newAccessToken);
|
|
||||||
return newAccessToken;
|
|
||||||
} catch (IOException | JSONException e) {
|
} catch (IOException | JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,16 @@ class AppModule {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Provides @Named("oauth_without_authenticator")
|
||||||
|
@Singleton
|
||||||
|
Retrofit provideOauthWithoutAuthenticatorRetrofit(OkHttpClient okHttpClient) {
|
||||||
|
return new Retrofit.Builder()
|
||||||
|
.baseUrl(RedditUtils.OAUTH_API_BASE_URI)
|
||||||
|
.client(okHttpClient)
|
||||||
|
.addConverterFactory(ScalarsConverterFactory.create())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
@Provides @Named("no_oauth")
|
@Provides @Named("no_oauth")
|
||||||
@Singleton
|
@Singleton
|
||||||
Retrofit provideRetrofit() {
|
Retrofit provideRetrofit() {
|
||||||
|
@ -17,6 +17,10 @@ import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_C
|
|||||||
|
|
||||||
public class LinkResolverActivity extends AppCompatActivity {
|
public class LinkResolverActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
static final String EXTRA_NOTIFICATION_FULLNAME = "ENF";
|
||||||
|
static final String EXTRA_SWITCH_ACCOUNT = "ESA";
|
||||||
|
static final String EXTRA_NEW_ACCOUNT_NAME = "ENAN";
|
||||||
|
|
||||||
private static final String POST_PATTERN = "/r/\\w+/comments/\\w+/{0,1}\\w+/{0,1}";
|
private static final String POST_PATTERN = "/r/\\w+/comments/\\w+/{0,1}\\w+/{0,1}";
|
||||||
private static final String COMMENT_PATTERN = "/r/\\w+/comments/\\w+/{0,1}\\w+/\\w+/{0,1}";
|
private static final String COMMENT_PATTERN = "/r/\\w+/comments/\\w+/{0,1}\\w+/\\w+/{0,1}";
|
||||||
private static final String SUBREDDIT_PATTERN = "/r/\\w+/*";
|
private static final String SUBREDDIT_PATTERN = "/r/\\w+/*";
|
||||||
@ -32,12 +36,18 @@ public class LinkResolverActivity extends AppCompatActivity {
|
|||||||
path = path.substring(0, path.length() - 1);
|
path = path.substring(0, path.length() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String notificationFullname = getIntent().getStringExtra(EXTRA_NOTIFICATION_FULLNAME);
|
||||||
|
boolean switchAccount = getIntent().getBooleanExtra(EXTRA_SWITCH_ACCOUNT, false);
|
||||||
|
String newAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME);
|
||||||
|
|
||||||
if(path.matches(POST_PATTERN)) {
|
if(path.matches(POST_PATTERN)) {
|
||||||
List<String> segments = uri.getPathSegments();
|
List<String> segments = uri.getPathSegments();
|
||||||
int commentsIndex = segments.lastIndexOf("comments");
|
int commentsIndex = segments.lastIndexOf("comments");
|
||||||
if(commentsIndex >=0 && commentsIndex < segments.size() - 1) {
|
if(commentsIndex >=0 && commentsIndex < segments.size() - 1) {
|
||||||
Intent intent = new Intent(this, ViewPostDetailActivity.class);
|
Intent intent = new Intent(this, ViewPostDetailActivity.class);
|
||||||
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, segments.get(commentsIndex + 1));
|
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, segments.get(commentsIndex + 1));
|
||||||
|
intent.putExtra(ViewPostDetailActivity.EXTRA_NOTIFICATION_FULLNAME, notificationFullname);
|
||||||
|
intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
} else {
|
} else {
|
||||||
deepLinkError(uri);
|
deepLinkError(uri);
|
||||||
@ -49,6 +59,8 @@ public class LinkResolverActivity extends AppCompatActivity {
|
|||||||
Intent intent = new Intent(this, ViewPostDetailActivity.class);
|
Intent intent = new Intent(this, ViewPostDetailActivity.class);
|
||||||
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, segments.get(commentsIndex + 1));
|
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, segments.get(commentsIndex + 1));
|
||||||
intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, segments.get(segments.size() - 1));
|
intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, segments.get(segments.size() - 1));
|
||||||
|
intent.putExtra(ViewPostDetailActivity.EXTRA_NOTIFICATION_FULLNAME, notificationFullname);
|
||||||
|
intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
} else {
|
} else {
|
||||||
deepLinkError(uri);
|
deepLinkError(uri);
|
||||||
@ -58,15 +70,21 @@ public class LinkResolverActivity extends AppCompatActivity {
|
|||||||
if(subredditName.equals("popular") || subredditName.equals("all")) {
|
if(subredditName.equals("popular") || subredditName.equals("all")) {
|
||||||
Intent intent = new Intent(this, MainActivity.class);
|
Intent intent = new Intent(this, MainActivity.class);
|
||||||
intent.putExtra(MainActivity.EXTRA_POST_TYPE, subredditName);
|
intent.putExtra(MainActivity.EXTRA_POST_TYPE, subredditName);
|
||||||
|
intent.putExtra(MainActivity.EXTRA_NOTIFICATION_FULLNAME, notificationFullname);
|
||||||
|
intent.putExtra(MainActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
} else {
|
} else {
|
||||||
Intent intent = new Intent(this, ViewSubredditDetailActivity.class);
|
Intent intent = new Intent(this, ViewSubredditDetailActivity.class);
|
||||||
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, path.substring(3));
|
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, path.substring(3));
|
||||||
|
intent.putExtra(ViewSubredditDetailActivity.EXTRA_NOTIFICATION_FULLNAME, notificationFullname);
|
||||||
|
intent.putExtra(ViewSubredditDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
} else if(path.matches(USER_PATTERN)) {
|
} else if(path.matches(USER_PATTERN)) {
|
||||||
Intent intent = new Intent(this, ViewUserDetailActivity.class);
|
Intent intent = new Intent(this, ViewUserDetailActivity.class);
|
||||||
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, path.substring(6));
|
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, path.substring(6));
|
||||||
|
intent.putExtra(ViewUserDetailActivity.EXTRA_NOTIFICATION_FULLNAME, notificationFullname);
|
||||||
|
intent.putExtra(ViewUserDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
} else {
|
} else {
|
||||||
deepLinkError(uri);
|
deepLinkError(uri);
|
||||||
|
@ -61,6 +61,8 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
PostTypeBottomSheetFragment.PostTypeSelectionCallback {
|
PostTypeBottomSheetFragment.PostTypeSelectionCallback {
|
||||||
|
|
||||||
static final String EXTRA_POST_TYPE = "EPT";
|
static final String EXTRA_POST_TYPE = "EPT";
|
||||||
|
static final String EXTRA_NOTIFICATION_FULLNAME = "ENF";
|
||||||
|
static final String EXTRA_NEW_ACCOUNT_NAME = "ENAN";
|
||||||
|
|
||||||
private static final String FETCH_USER_INFO_STATE = "FUIS";
|
private static final String FETCH_USER_INFO_STATE = "FUIS";
|
||||||
private static final String DRAWER_ON_ACCOUNT_SWITCH_STATE = "DOASS";
|
private static final String DRAWER_ON_ACCOUNT_SWITCH_STATE = "DOASS";
|
||||||
@ -71,6 +73,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
private static final String ACCOUNT_PROFILE_IMAGE_URL_STATE = "APIUS";
|
private static final String ACCOUNT_PROFILE_IMAGE_URL_STATE = "APIUS";
|
||||||
private static final String ACCOUNT_BANNER_IMAGE_URL_STATE = "ABIUS";
|
private static final String ACCOUNT_BANNER_IMAGE_URL_STATE = "ABIUS";
|
||||||
private static final String ACCOUNT_KARMA_STATE = "AKS";
|
private static final String ACCOUNT_KARMA_STATE = "AKS";
|
||||||
|
private static final String NEW_ACCOUNT_NAME_STATE = "NANS";
|
||||||
|
|
||||||
private static final int LOGIN_ACTIVITY_REQUEST_CODE = 0;
|
private static final int LOGIN_ACTIVITY_REQUEST_CODE = 0;
|
||||||
|
|
||||||
@ -117,6 +120,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
private int mKarma;
|
private int mKarma;
|
||||||
private boolean mFetchUserInfoSuccess = false;
|
private boolean mFetchUserInfoSuccess = false;
|
||||||
private boolean mDrawerOnAccountSwitch = false;
|
private boolean mDrawerOnAccountSwitch = false;
|
||||||
|
private String mNewAccountName;
|
||||||
|
|
||||||
private Menu mMenu;
|
private Menu mMenu;
|
||||||
|
|
||||||
@ -217,6 +221,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
mProfileImageUrl = savedInstanceState.getString(ACCOUNT_PROFILE_IMAGE_URL_STATE);
|
mProfileImageUrl = savedInstanceState.getString(ACCOUNT_PROFILE_IMAGE_URL_STATE);
|
||||||
mBannerImageUrl = savedInstanceState.getString(ACCOUNT_BANNER_IMAGE_URL_STATE);
|
mBannerImageUrl = savedInstanceState.getString(ACCOUNT_BANNER_IMAGE_URL_STATE);
|
||||||
mKarma = savedInstanceState.getInt(ACCOUNT_KARMA_STATE);
|
mKarma = savedInstanceState.getInt(ACCOUNT_KARMA_STATE);
|
||||||
|
mNewAccountName = savedInstanceState.getString(NEW_ACCOUNT_NAME_STATE);
|
||||||
|
|
||||||
if(!mNullAccessToken && mAccessToken == null) {
|
if(!mNullAccessToken && mAccessToken == null) {
|
||||||
getCurrentAccountAndBindView();
|
getCurrentAccountAndBindView();
|
||||||
@ -224,6 +229,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
bindView();
|
bindView();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
mNewAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME);
|
||||||
getCurrentAccountAndBindView();
|
getCurrentAccountAndBindView();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -231,32 +237,60 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void getCurrentAccountAndBindView() {
|
private void getCurrentAccountAndBindView() {
|
||||||
mNullAccessToken = true;
|
if(mNewAccountName != null) {
|
||||||
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
|
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, () -> {
|
||||||
if(account == null) {
|
mNewAccountName = null;
|
||||||
mNullAccessToken = true;
|
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
|
||||||
} else {
|
if(account == null) {
|
||||||
mNullAccessToken = false;
|
mNullAccessToken = true;
|
||||||
mAccessToken = account.getAccessToken();
|
} else {
|
||||||
mAccountName = account.getUsername();
|
mAccessToken = account.getAccessToken();
|
||||||
mProfileImageUrl = account.getProfileImageUrl();
|
mAccountName = account.getUsername();
|
||||||
mBannerImageUrl = account.getBannerImageUrl();
|
mProfileImageUrl = account.getProfileImageUrl();
|
||||||
mKarma = account.getKarma();
|
mBannerImageUrl = account.getBannerImageUrl();
|
||||||
|
mKarma = account.getKarma();
|
||||||
|
|
||||||
Constraints constraints = new Constraints.Builder()
|
Constraints constraints = new Constraints.Builder()
|
||||||
.setRequiredNetworkType(NetworkType.CONNECTED)
|
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||||
.build();
|
|
||||||
|
|
||||||
PeriodicWorkRequest pullNotificationRequest =
|
|
||||||
new PeriodicWorkRequest.Builder(PullNotificationWorker.class, 1, TimeUnit.HOURS)
|
|
||||||
.setConstraints(constraints)
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
|
PeriodicWorkRequest pullNotificationRequest =
|
||||||
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
|
new PeriodicWorkRequest.Builder(PullNotificationWorker.class, 1, TimeUnit.HOURS)
|
||||||
}
|
.setConstraints(constraints)
|
||||||
bindView();
|
.build();
|
||||||
}).execute();
|
|
||||||
|
WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
|
||||||
|
ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest);
|
||||||
|
}
|
||||||
|
bindView();
|
||||||
|
}).execute();
|
||||||
|
}).execute();
|
||||||
|
} else {
|
||||||
|
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
|
||||||
|
if(account == null) {
|
||||||
|
mNullAccessToken = true;
|
||||||
|
} else {
|
||||||
|
mAccessToken = account.getAccessToken();
|
||||||
|
mAccountName = account.getUsername();
|
||||||
|
mProfileImageUrl = account.getProfileImageUrl();
|
||||||
|
mBannerImageUrl = account.getBannerImageUrl();
|
||||||
|
mKarma = account.getKarma();
|
||||||
|
|
||||||
|
Constraints constraints = new Constraints.Builder()
|
||||||
|
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
PeriodicWorkRequest pullNotificationRequest =
|
||||||
|
new PeriodicWorkRequest.Builder(PullNotificationWorker.class, 1, TimeUnit.HOURS)
|
||||||
|
.setConstraints(constraints)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
|
||||||
|
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
|
||||||
|
}
|
||||||
|
bindView();
|
||||||
|
}).execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void bindView() {
|
private void bindView() {
|
||||||
@ -338,7 +372,8 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
accountViewModel.getAccountsExceptCurrentAccountLiveData().observe(this, adapter::changeAccountsDataset);
|
accountViewModel.getAccountsExceptCurrentAccountLiveData().observe(this, adapter::changeAccountsDataset);
|
||||||
|
|
||||||
if(getIntent().hasExtra(EXTRA_POST_TYPE)) {
|
if(getIntent().hasExtra(EXTRA_POST_TYPE)) {
|
||||||
if(getIntent().getExtras().getString(EXTRA_POST_TYPE).equals("popular")) {
|
String type = getIntent().getStringExtra(EXTRA_POST_TYPE);
|
||||||
|
if(type != null && type.equals("popular")) {
|
||||||
viewPager.setCurrentItem(1);
|
viewPager.setCurrentItem(1);
|
||||||
} else {
|
} else {
|
||||||
viewPager.setCurrentItem(2);
|
viewPager.setCurrentItem(2);
|
||||||
@ -610,6 +645,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
outState.putString(ACCOUNT_PROFILE_IMAGE_URL_STATE, mProfileImageUrl);
|
outState.putString(ACCOUNT_PROFILE_IMAGE_URL_STATE, mProfileImageUrl);
|
||||||
outState.putString(ACCOUNT_BANNER_IMAGE_URL_STATE, mBannerImageUrl);
|
outState.putString(ACCOUNT_BANNER_IMAGE_URL_STATE, mBannerImageUrl);
|
||||||
outState.putInt(ACCOUNT_KARMA_STATE, mKarma);
|
outState.putInt(ACCOUNT_KARMA_STATE, mKarma);
|
||||||
|
outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -11,10 +11,10 @@ class NotificationUtils {
|
|||||||
static final String CHANNEL_POST_MEDIA = "Post Media";
|
static final String CHANNEL_POST_MEDIA = "Post Media";
|
||||||
static final String CHANNEL_ID_NEW_COMMENTS = "new_comments";
|
static final String CHANNEL_ID_NEW_COMMENTS = "new_comments";
|
||||||
static final String CHANNEL_NEW_COMMENTS = "New Comments";
|
static final String CHANNEL_NEW_COMMENTS = "New Comments";
|
||||||
static final String GROUP_NEW_COMMENTS = "ml.docilealligator.infinityforreddit.NEW_COMMENTS";
|
static final int SUMMARY_BASE_ID_UNREAD_MESSAGE = 0;
|
||||||
static final int SUMMARY_ID_NEW_COMMENTS = 0;
|
static final int NOTIFICATION_BASE_ID_UNREAD_MESSAGE = 1;
|
||||||
|
|
||||||
static final int BASE_ID_UNREAD_MESSAGE = 1;
|
private static final String GROUP_USER_BASE = "ml.docilealligator.infinityforreddit.";
|
||||||
|
|
||||||
static NotificationCompat.Builder buildNotification(NotificationManagerCompat notificationManager,
|
static NotificationCompat.Builder buildNotification(NotificationManagerCompat notificationManager,
|
||||||
Context context, String title, String content,
|
Context context, String title, String content,
|
||||||
@ -57,4 +57,16 @@ class NotificationUtils {
|
|||||||
static NotificationManagerCompat getNotificationManager(Context context) {
|
static NotificationManagerCompat getNotificationManager(Context context) {
|
||||||
return NotificationManagerCompat.from(context);
|
return NotificationManagerCompat.from(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static String getAccountGroupName(String accountName) {
|
||||||
|
return GROUP_USER_BASE + accountName;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int getSummaryIdUnreadMessage(int accountIndex) {
|
||||||
|
return SUMMARY_BASE_ID_UNREAD_MESSAGE + accountIndex * 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int getNotificationIdUnreadMessage(int accountIndex, int messageIndex) {
|
||||||
|
return NOTIFICATION_BASE_ID_UNREAD_MESSAGE + accountIndex * 1000 + messageIndex;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,14 @@ import androidx.core.app.NotificationManagerCompat;
|
|||||||
import androidx.work.Worker;
|
import androidx.work.Worker;
|
||||||
import androidx.work.WorkerParameters;
|
import androidx.work.WorkerParameters;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
@ -26,10 +32,11 @@ public class PullNotificationWorker extends Worker {
|
|||||||
static final String WORKER_TAG = "PNWT";
|
static final String WORKER_TAG = "PNWT";
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
|
private RedditAPI api;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@Named("oauth")
|
@Named("oauth_without_authenticator")
|
||||||
Retrofit mOauthRetrofit;
|
Retrofit mOauthWithoutAuthenticatorRetrofit;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
RedditDataRoomDatabase redditDataRoomDatabase;
|
RedditDataRoomDatabase redditDataRoomDatabase;
|
||||||
@ -38,6 +45,7 @@ public class PullNotificationWorker extends Worker {
|
|||||||
super(context, workerParams);
|
super(context, workerParams);
|
||||||
this.context = context;
|
this.context = context;
|
||||||
((Infinity) context.getApplicationContext()).getAppComponent().inject(this);
|
((Infinity) context.getApplicationContext()).getAppComponent().inject(this);
|
||||||
|
api = mOauthWithoutAuthenticatorRetrofit.create(RedditAPI.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@ -46,109 +54,127 @@ public class PullNotificationWorker extends Worker {
|
|||||||
Log.i("workmanager", "do");
|
Log.i("workmanager", "do");
|
||||||
try {
|
try {
|
||||||
Log.i("workmanager", "before response");
|
Log.i("workmanager", "before response");
|
||||||
Account currentAccount = redditDataRoomDatabase.accountDao().getCurrentAccount();
|
|
||||||
Response<String> response = mOauthRetrofit.create(RedditAPI.class).getMessages(
|
|
||||||
RedditUtils.getOAuthHeader(currentAccount.getAccessToken()),
|
|
||||||
FetchMessages.WHERE_UNREAD, null).execute();
|
|
||||||
Log.i("workmanager", "has response");
|
|
||||||
if(response.isSuccessful()) {
|
|
||||||
String responseBody = response.body();
|
|
||||||
ArrayList<Message> messages = FetchMessages.parseMessage(responseBody, context.getResources().getConfiguration().locale);
|
|
||||||
|
|
||||||
if(messages != null && !messages.isEmpty()) {
|
String currentAccountName = redditDataRoomDatabase.accountDao().getCurrentAccount().getUsername();
|
||||||
NotificationManagerCompat notificationManager = NotificationUtils.getNotificationManager(context);
|
|
||||||
|
|
||||||
NotificationCompat.Builder summaryBuilder = NotificationUtils.buildSummaryNotification(context,
|
List<Account> accounts = redditDataRoomDatabase.accountDao().getAllAccounts();
|
||||||
notificationManager, currentAccount.getUsername(), messages.size() + " new comment replies",
|
for(int accountIndex = 0; accountIndex < accounts.size(); accountIndex++) {
|
||||||
NotificationUtils.CHANNEL_ID_NEW_COMMENTS, NotificationUtils.CHANNEL_NEW_COMMENTS,
|
Account account = accounts.get(accountIndex);
|
||||||
NotificationUtils.GROUP_NEW_COMMENTS);
|
Log.i("workmanager", account.getUsername() + " " + account.getAccessToken());
|
||||||
|
|
||||||
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
|
Response<String> response = fetchMessages(account);
|
||||||
|
|
||||||
int messageSize = messages.size() >= 5 ? 5 : messages.size();
|
if(response != null && response.isSuccessful()) {
|
||||||
|
Log.i("workmanager", "has response");
|
||||||
|
String responseBody = response.body();
|
||||||
|
ArrayList<Message> messages = FetchMessages.parseMessage(responseBody, context.getResources().getConfiguration().locale);
|
||||||
|
|
||||||
for(int i = messageSize - 1; i >= 0; i--) {
|
if(messages != null && !messages.isEmpty()) {
|
||||||
Message message = messages.get(i);
|
NotificationManagerCompat notificationManager = NotificationUtils.getNotificationManager(context);
|
||||||
|
|
||||||
inboxStyle.addLine(message.getAuthor() + " " + message.getBody());
|
NotificationCompat.Builder summaryBuilder = NotificationUtils.buildSummaryNotification(context,
|
||||||
|
notificationManager, account.getUsername(),
|
||||||
|
context.getString(R.string.notification_new_messages, messages.size()),
|
||||||
|
NotificationUtils.CHANNEL_ID_NEW_COMMENTS, NotificationUtils.CHANNEL_NEW_COMMENTS,
|
||||||
|
NotificationUtils.getAccountGroupName(account.getUsername()));
|
||||||
|
|
||||||
String kind = message.getKind();
|
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
|
||||||
String title;
|
|
||||||
String summary;
|
int messageSize = messages.size() >= 5 ? 5 : messages.size();
|
||||||
if(kind.equals(Message.TYPE_COMMENT) || kind.equals(Message.TYPE_LINK)) {
|
|
||||||
title = message.getAuthor();
|
for(int messageIndex = messageSize - 1; messageIndex >= 0; messageIndex--) {
|
||||||
summary = context.getString(R.string.notification_summary_comment);
|
Message message = messages.get(messageIndex);
|
||||||
} else {
|
|
||||||
title = message.getTitle();
|
inboxStyle.addLine(message.getAuthor() + " " + message.getBody());
|
||||||
if(kind.equals(Message.TYPE_ACCOUNT)) {
|
|
||||||
summary = context.getString(R.string.notification_summary_account);
|
String kind = message.getKind();
|
||||||
} else if(kind.equals(Message.TYPE_MESSAGE)) {
|
Log.i("workmanager", kind);
|
||||||
summary = context.getString(R.string.notification_summary_message);
|
String title;
|
||||||
} else if(kind.equals(Message.TYPE_SUBREDDIT)) {
|
String summary;
|
||||||
summary = context.getString(R.string.notification_summary_subreddit);
|
if(kind.equals(Message.TYPE_COMMENT) || kind.equals(Message.TYPE_LINK)) {
|
||||||
|
title = message.getAuthor();
|
||||||
|
summary = message.getSubject().substring(0, 1).toUpperCase() + message.getSubject().substring(1);
|
||||||
} else {
|
} else {
|
||||||
summary = context.getString(R.string.notification_summary_award);
|
title = message.getTitle() == null || message.getTitle().equals("") ? message.getSubject() : message.getTitle();
|
||||||
|
if(kind.equals(Message.TYPE_ACCOUNT)) {
|
||||||
|
summary = context.getString(R.string.notification_summary_account);
|
||||||
|
} else if(kind.equals(Message.TYPE_MESSAGE)) {
|
||||||
|
summary = context.getString(R.string.notification_summary_message);
|
||||||
|
} else if(kind.equals(Message.TYPE_SUBREDDIT)) {
|
||||||
|
summary = context.getString(R.string.notification_summary_subreddit);
|
||||||
|
} else {
|
||||||
|
summary = context.getString(R.string.notification_summary_award);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NotificationCompat.Builder builder = NotificationUtils.buildNotification(notificationManager,
|
||||||
|
context, title, message.getBody(), summary,
|
||||||
|
NotificationUtils.CHANNEL_ID_NEW_COMMENTS,
|
||||||
|
NotificationUtils.CHANNEL_NEW_COMMENTS,
|
||||||
|
NotificationUtils.getAccountGroupName(account.getUsername()));
|
||||||
|
|
||||||
|
if(kind.equals(Message.TYPE_COMMENT)) {
|
||||||
|
Intent intent = new Intent(context, LinkResolverActivity.class);
|
||||||
|
Uri uri = LinkResolverActivity.getRedditUriByPath(message.getContext());
|
||||||
|
intent.setData(uri);
|
||||||
|
if(!account.getUsername().equals(currentAccountName)) {
|
||||||
|
intent.putExtra(LinkResolverActivity.EXTRA_SWITCH_ACCOUNT, true);
|
||||||
|
intent.putExtra(LinkResolverActivity.EXTRA_NEW_ACCOUNT_NAME, account.getUsername());
|
||||||
|
}
|
||||||
|
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
||||||
|
builder.setContentIntent(pendingIntent);
|
||||||
|
} else if(kind.equals(Message.TYPE_ACCOUNT)) {
|
||||||
|
Intent intent = new Intent(context, ViewMessageActivity.class);
|
||||||
|
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
||||||
|
builder.setContentIntent(summaryPendingIntent);
|
||||||
|
} else if(kind.equals(Message.TYPE_LINK)) {
|
||||||
|
Intent intent = new Intent(context, LinkResolverActivity.class);
|
||||||
|
Uri uri = LinkResolverActivity.getRedditUriByPath(message.getContext());
|
||||||
|
intent.setData(uri);
|
||||||
|
if(!account.getUsername().equals(currentAccountName)) {
|
||||||
|
intent.putExtra(LinkResolverActivity.EXTRA_SWITCH_ACCOUNT, true);
|
||||||
|
intent.putExtra(LinkResolverActivity.EXTRA_NEW_ACCOUNT_NAME, account.getUsername());
|
||||||
|
}
|
||||||
|
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
||||||
|
builder.setContentIntent(pendingIntent);
|
||||||
|
} else if(kind.equals(Message.TYPE_MESSAGE)) {
|
||||||
|
Intent intent = new Intent(context, ViewMessageActivity.class);
|
||||||
|
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
||||||
|
builder.setContentIntent(summaryPendingIntent);
|
||||||
|
} else if(kind.equals(Message.TYPE_SUBREDDIT)) {
|
||||||
|
Intent intent = new Intent(context, ViewMessageActivity.class);
|
||||||
|
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
||||||
|
builder.setContentIntent(summaryPendingIntent);
|
||||||
|
} else {
|
||||||
|
Intent intent = new Intent(context, ViewMessageActivity.class);
|
||||||
|
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
||||||
|
builder.setContentIntent(summaryPendingIntent);
|
||||||
|
}
|
||||||
|
notificationManager.notify(NotificationUtils.getNotificationIdUnreadMessage(accountIndex, messageIndex), builder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
NotificationCompat.Builder builder = NotificationUtils.buildNotification(notificationManager,
|
inboxStyle.setBigContentTitle(context.getString(R.string.notification_new_messages, messages.size()))
|
||||||
context, title, message.getBody(), summary,
|
.setSummaryText(account.getUsername());
|
||||||
NotificationUtils.CHANNEL_ID_NEW_COMMENTS,
|
|
||||||
NotificationUtils.CHANNEL_NEW_COMMENTS, NotificationUtils.GROUP_NEW_COMMENTS);
|
|
||||||
|
|
||||||
if(kind.equals(Message.TYPE_COMMENT)) {
|
summaryBuilder.setStyle(inboxStyle);
|
||||||
Intent intent = new Intent(context, LinkResolverActivity.class);
|
|
||||||
Uri uri = LinkResolverActivity.getRedditUriByPath(message.getContext());
|
Intent summaryIntent = new Intent(context, ViewMessageActivity.class);
|
||||||
intent.setData(uri);
|
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, 0, summaryIntent, 0);
|
||||||
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
summaryBuilder.setContentIntent(summaryPendingIntent);
|
||||||
builder.setContentIntent(pendingIntent);
|
|
||||||
} else if(kind.equals(Message.TYPE_ACCOUNT)) {
|
notificationManager.notify(NotificationUtils.getSummaryIdUnreadMessage(accountIndex), summaryBuilder.build());
|
||||||
Intent intent = new Intent(context, ViewMessageActivity.class);
|
|
||||||
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
Log.i("workmanager", "message size " + messages.size());
|
||||||
builder.setContentIntent(summaryPendingIntent);
|
} else {
|
||||||
} else if(kind.equals(Message.TYPE_LINK)) {
|
Log.i("workmanager", "retry1");
|
||||||
Intent intent = new Intent(context, LinkResolverActivity.class);
|
return Result.retry();
|
||||||
Uri uri = LinkResolverActivity.getRedditUriByPath(message.getContext());
|
|
||||||
intent.setData(uri);
|
|
||||||
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
|
||||||
builder.setContentIntent(pendingIntent);
|
|
||||||
} else if(kind.equals(Message.TYPE_MESSAGE)) {
|
|
||||||
Intent intent = new Intent(context, ViewMessageActivity.class);
|
|
||||||
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
|
||||||
builder.setContentIntent(summaryPendingIntent);
|
|
||||||
} else if(kind.equals(Message.TYPE_SUBREDDIT)) {
|
|
||||||
Intent intent = new Intent(context, ViewMessageActivity.class);
|
|
||||||
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
|
||||||
builder.setContentIntent(summaryPendingIntent);
|
|
||||||
} else {
|
|
||||||
Intent intent = new Intent(context, ViewMessageActivity.class);
|
|
||||||
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
|
||||||
builder.setContentIntent(summaryPendingIntent);
|
|
||||||
}
|
|
||||||
notificationManager.notify(NotificationUtils.BASE_ID_UNREAD_MESSAGE + i, builder.build());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inboxStyle.setBigContentTitle(messages.size() + " New Messages")
|
|
||||||
.setSummaryText(currentAccount.getUsername());
|
|
||||||
|
|
||||||
summaryBuilder.setStyle(inboxStyle);
|
|
||||||
|
|
||||||
Intent summaryIntent = new Intent(context, ViewMessageActivity.class);
|
|
||||||
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, 0, summaryIntent, 0);
|
|
||||||
summaryBuilder.setContentIntent(summaryPendingIntent);
|
|
||||||
|
|
||||||
notificationManager.notify(NotificationUtils.SUMMARY_ID_NEW_COMMENTS, summaryBuilder.build());
|
|
||||||
|
|
||||||
Log.i("workmanager", "message size " + messages.size());
|
|
||||||
} else {
|
} else {
|
||||||
Log.i("workmanager", "retry1");
|
Log.i("workmanager", "retry2 " + response.code());
|
||||||
return Result.retry();
|
return Result.retry();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
Log.i("workmanager", "retry2 " + response.code());
|
|
||||||
return Result.retry();
|
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException | JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Log.i("workmanager", "retry3");
|
Log.i("workmanager", "retry3");
|
||||||
return Result.retry();
|
return Result.retry();
|
||||||
@ -157,4 +183,35 @@ public class PullNotificationWorker extends Worker {
|
|||||||
Log.i("workmanager", "success");
|
Log.i("workmanager", "success");
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Response<String> fetchMessages(Account account) throws IOException, JSONException {
|
||||||
|
Response<String> response = api.getMessages(
|
||||||
|
RedditUtils.getOAuthHeader(account.getAccessToken()),
|
||||||
|
FetchMessages.WHERE_INBOX, null).execute();
|
||||||
|
if(response.isSuccessful()) {
|
||||||
|
return response;
|
||||||
|
} else {
|
||||||
|
if(response.code() == 401) {
|
||||||
|
String refreshToken = account.getRefreshToken();
|
||||||
|
|
||||||
|
Map<String, String> params = new HashMap<>();
|
||||||
|
params.put(RedditUtils.GRANT_TYPE_KEY, RedditUtils.GRANT_TYPE_REFRESH_TOKEN);
|
||||||
|
params.put(RedditUtils.REFRESH_TOKEN_KEY, refreshToken);
|
||||||
|
|
||||||
|
Response accessTokenResponse = api.getAccessToken(RedditUtils.getHttpBasicAuthHeader(), params).execute();
|
||||||
|
if(accessTokenResponse.isSuccessful() && accessTokenResponse.body() != null) {
|
||||||
|
JSONObject jsonObject = new JSONObject((String) accessTokenResponse.body());
|
||||||
|
String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY);
|
||||||
|
account.setAccessToken(newAccessToken);
|
||||||
|
redditDataRoomDatabase.accountDao().changeAccessToken(account.getUsername(), newAccessToken);
|
||||||
|
|
||||||
|
return fetchMessages(account);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,6 +60,8 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
|
|||||||
static final String EXTRA_POST_LIST_POSITION = "EPLI";
|
static final String EXTRA_POST_LIST_POSITION = "EPLI";
|
||||||
static final String EXTRA_POST_ID = "EPI";
|
static final String EXTRA_POST_ID = "EPI";
|
||||||
static final String EXTRA_SINGLE_COMMENT_ID = "ESCI";
|
static final String EXTRA_SINGLE_COMMENT_ID = "ESCI";
|
||||||
|
static final String EXTRA_NOTIFICATION_FULLNAME = "ENI";
|
||||||
|
static final String EXTRA_NEW_ACCOUNT_NAME = "ENAN";
|
||||||
|
|
||||||
private static final int EDIT_POST_REQUEST_CODE = 2;
|
private static final int EDIT_POST_REQUEST_CODE = 2;
|
||||||
static final int EDIT_COMMENT_REQUEST_CODE = 3;
|
static final int EDIT_COMMENT_REQUEST_CODE = 3;
|
||||||
@ -96,6 +98,8 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
|
|||||||
boolean loadMoreChildrenSuccess = true;
|
boolean loadMoreChildrenSuccess = true;
|
||||||
@State
|
@State
|
||||||
boolean hasMoreChildren;
|
boolean hasMoreChildren;
|
||||||
|
@State
|
||||||
|
String mNewAccountName;
|
||||||
|
|
||||||
private boolean showToast = false;
|
private boolean showToast = false;
|
||||||
|
|
||||||
@ -190,8 +194,11 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
|
|||||||
mRecyclerView.setLayoutManager(mLinearLayoutManager);
|
mRecyclerView.setLayoutManager(mLinearLayoutManager);
|
||||||
|
|
||||||
mSingleCommentId = getIntent().hasExtra(EXTRA_SINGLE_COMMENT_ID) ? getIntent().getExtras().getString(EXTRA_SINGLE_COMMENT_ID) : null;
|
mSingleCommentId = getIntent().hasExtra(EXTRA_SINGLE_COMMENT_ID) ? getIntent().getExtras().getString(EXTRA_SINGLE_COMMENT_ID) : null;
|
||||||
if(savedInstanceState == null && mSingleCommentId != null) {
|
if(savedInstanceState == null) {
|
||||||
isSingleCommentThreadMode = true;
|
if(mSingleCommentId != null) {
|
||||||
|
isSingleCommentThreadMode = true;
|
||||||
|
}
|
||||||
|
mNewAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
orientation = getResources().getConfiguration().orientation;
|
orientation = getResources().getConfiguration().orientation;
|
||||||
@ -208,16 +215,32 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void getCurrentAccountAndBindView() {
|
private void getCurrentAccountAndBindView() {
|
||||||
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
|
if(mNewAccountName != null) {
|
||||||
if(account == null) {
|
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, () -> {
|
||||||
mNullAccessToken = true;
|
mNewAccountName = null;
|
||||||
} else {
|
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
|
||||||
mAccessToken = account.getAccessToken();
|
if(account == null) {
|
||||||
mAccountName = account.getUsername();
|
mNullAccessToken = true;
|
||||||
}
|
} else {
|
||||||
|
mAccessToken = account.getAccessToken();
|
||||||
|
mAccountName = account.getUsername();
|
||||||
|
}
|
||||||
|
|
||||||
bindView();
|
bindView();
|
||||||
}).execute();
|
}).execute();
|
||||||
|
}).execute();
|
||||||
|
} else {
|
||||||
|
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
|
||||||
|
if(account == null) {
|
||||||
|
mNullAccessToken = true;
|
||||||
|
} else {
|
||||||
|
mAccessToken = account.getAccessToken();
|
||||||
|
mAccountName = account.getUsername();
|
||||||
|
}
|
||||||
|
|
||||||
|
bindView();
|
||||||
|
}).execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void bindView() {
|
private void bindView() {
|
||||||
|
@ -46,7 +46,9 @@ import retrofit2.Retrofit;
|
|||||||
public class ViewSubredditDetailActivity extends AppCompatActivity implements SortTypeBottomSheetFragment.SortTypeSelectionCallback,
|
public class ViewSubredditDetailActivity extends AppCompatActivity implements SortTypeBottomSheetFragment.SortTypeSelectionCallback,
|
||||||
PostTypeBottomSheetFragment.PostTypeSelectionCallback {
|
PostTypeBottomSheetFragment.PostTypeSelectionCallback {
|
||||||
|
|
||||||
public static final String EXTRA_SUBREDDIT_NAME_KEY = "ESN";
|
static final String EXTRA_SUBREDDIT_NAME_KEY = "ESN";
|
||||||
|
static final String EXTRA_NOTIFICATION_FULLNAME = "ENF";
|
||||||
|
static final String EXTRA_NEW_ACCOUNT_NAME = "ENAN";
|
||||||
|
|
||||||
private static final String FETCH_SUBREDDIT_INFO_STATE = "FSIS";
|
private static final String FETCH_SUBREDDIT_INFO_STATE = "FSIS";
|
||||||
private static final String FRAGMENT_OUT_STATE_KEY = "FOSK";
|
private static final String FRAGMENT_OUT_STATE_KEY = "FOSK";
|
||||||
@ -54,6 +56,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
|
|||||||
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
|
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
|
||||||
private static final String ACCESS_TOKEN_STATE = "ATS";
|
private static final String ACCESS_TOKEN_STATE = "ATS";
|
||||||
private static final String ACCOUNT_NAME_STATE = "ANS";
|
private static final String ACCOUNT_NAME_STATE = "ANS";
|
||||||
|
private static final String NEW_ACCOUNT_NAME_STATE = "NANS";
|
||||||
|
|
||||||
@BindView(R.id.coordinator_layout_view_subreddit_detail_activity) CoordinatorLayout coordinatorLayout;
|
@BindView(R.id.coordinator_layout_view_subreddit_detail_activity) CoordinatorLayout coordinatorLayout;
|
||||||
@BindView(R.id.appbar_layout_view_subreddit_detail) AppBarLayout appBarLayout;
|
@BindView(R.id.appbar_layout_view_subreddit_detail) AppBarLayout appBarLayout;
|
||||||
@ -76,6 +79,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
|
|||||||
private boolean subscriptionReady = false;
|
private boolean subscriptionReady = false;
|
||||||
private boolean isInLazyMode = false;
|
private boolean isInLazyMode = false;
|
||||||
private boolean showToast = false;
|
private boolean showToast = false;
|
||||||
|
private String mNewAccountName;
|
||||||
|
|
||||||
private RequestManager glide;
|
private RequestManager glide;
|
||||||
private Fragment mFragment;
|
private Fragment mFragment;
|
||||||
@ -149,6 +153,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
|
|||||||
subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY);
|
subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY);
|
||||||
|
|
||||||
if(savedInstanceState == null) {
|
if(savedInstanceState == null) {
|
||||||
|
mNewAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME);
|
||||||
getCurrentAccountAndBindView();
|
getCurrentAccountAndBindView();
|
||||||
} else {
|
} else {
|
||||||
mFetchSubredditInfoSuccess = savedInstanceState.getBoolean(FETCH_SUBREDDIT_INFO_STATE);
|
mFetchSubredditInfoSuccess = savedInstanceState.getBoolean(FETCH_SUBREDDIT_INFO_STATE);
|
||||||
@ -156,6 +161,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
|
|||||||
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
|
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
|
||||||
mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
|
mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
|
||||||
isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE);
|
isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE);
|
||||||
|
mNewAccountName = savedInstanceState.getString(NEW_ACCOUNT_NAME_STATE);
|
||||||
|
|
||||||
if(!mNullAccessToken && mAccessToken == null) {
|
if(!mNullAccessToken && mAccessToken == null) {
|
||||||
getCurrentAccountAndBindView();
|
getCurrentAccountAndBindView();
|
||||||
@ -257,15 +263,30 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void getCurrentAccountAndBindView() {
|
private void getCurrentAccountAndBindView() {
|
||||||
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
|
if(mNewAccountName != null) {
|
||||||
if(account == null) {
|
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, () -> {
|
||||||
mNullAccessToken = true;
|
mNewAccountName = null;
|
||||||
} else {
|
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
|
||||||
mAccessToken = account.getAccessToken();
|
if(account == null) {
|
||||||
mAccountName = account.getUsername();
|
mNullAccessToken = true;
|
||||||
}
|
} else {
|
||||||
bindView(true);
|
mAccessToken = account.getAccessToken();
|
||||||
}).execute();
|
mAccountName = account.getUsername();
|
||||||
|
}
|
||||||
|
bindView(true);
|
||||||
|
}).execute();
|
||||||
|
}).execute();
|
||||||
|
} else {
|
||||||
|
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
|
||||||
|
if(account == null) {
|
||||||
|
mNullAccessToken = true;
|
||||||
|
} else {
|
||||||
|
mAccessToken = account.getAccessToken();
|
||||||
|
mAccountName = account.getUsername();
|
||||||
|
}
|
||||||
|
bindView(true);
|
||||||
|
}).execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fetchSubredditData() {
|
private void fetchSubredditData() {
|
||||||
@ -430,6 +451,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
|
|||||||
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
|
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
|
||||||
outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
|
outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
|
||||||
outState.putString(ACCOUNT_NAME_STATE, mAccountName);
|
outState.putString(ACCOUNT_NAME_STATE, mAccountName);
|
||||||
|
outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName);
|
||||||
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE_KEY, mFragment);
|
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE_KEY, mFragment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,13 +50,16 @@ import retrofit2.Retrofit;
|
|||||||
|
|
||||||
public class ViewUserDetailActivity extends AppCompatActivity implements UserThingSortTypeBottomSheetFragment.UserThingSortTypeSelectionCallback {
|
public class ViewUserDetailActivity extends AppCompatActivity implements UserThingSortTypeBottomSheetFragment.UserThingSortTypeSelectionCallback {
|
||||||
|
|
||||||
public static final String EXTRA_USER_NAME_KEY = "EUNK";
|
static final String EXTRA_USER_NAME_KEY = "EUNK";
|
||||||
|
static final String EXTRA_NOTIFICATION_FULLNAME = "ENF";
|
||||||
|
static final String EXTRA_NEW_ACCOUNT_NAME = "ENAN";
|
||||||
|
|
||||||
private static final String FETCH_USER_INFO_STATE = "FSIS";
|
private static final String FETCH_USER_INFO_STATE = "FSIS";
|
||||||
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
|
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
|
||||||
private static final String ACCESS_TOKEN_STATE = "ATS";
|
private static final String ACCESS_TOKEN_STATE = "ATS";
|
||||||
private static final String ACCOUNT_NAME_STATE = "ANS";
|
private static final String ACCOUNT_NAME_STATE = "ANS";
|
||||||
private static final String IS_IN_LAZY_MODE_STATE = "IILMS";
|
private static final String IS_IN_LAZY_MODE_STATE = "IILMS";
|
||||||
|
private static final String NEW_ACCOUNT_NAME_STATE = "NANS";
|
||||||
|
|
||||||
@BindView(R.id.coordinator_layout_view_user_detail_activity) CoordinatorLayout coordinatorLayout;
|
@BindView(R.id.coordinator_layout_view_user_detail_activity) CoordinatorLayout coordinatorLayout;
|
||||||
@BindView(R.id.view_pager_view_user_detail_activity) ViewPager viewPager;
|
@BindView(R.id.view_pager_view_user_detail_activity) ViewPager viewPager;
|
||||||
@ -93,6 +96,7 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
|
|||||||
private int collapsedTabBackgroundColor;
|
private int collapsedTabBackgroundColor;
|
||||||
private int collapsedTabIndicatorColor;
|
private int collapsedTabIndicatorColor;
|
||||||
private boolean showToast = false;
|
private boolean showToast = false;
|
||||||
|
private String mNewAccountName;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@Named("no_oauth")
|
@Named("no_oauth")
|
||||||
@ -114,9 +118,10 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
|
|||||||
|
|
||||||
((Infinity) getApplication()).getAppComponent().inject(this);
|
((Infinity) getApplication()).getAppComponent().inject(this);
|
||||||
|
|
||||||
username = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY);
|
username = getIntent().getStringExtra(EXTRA_USER_NAME_KEY);
|
||||||
|
|
||||||
if (savedInstanceState == null) {
|
if (savedInstanceState == null) {
|
||||||
|
mNewAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME);
|
||||||
getCurrentAccountAndInitializeViewPager();
|
getCurrentAccountAndInitializeViewPager();
|
||||||
} else {
|
} else {
|
||||||
mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE);
|
mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE);
|
||||||
@ -124,6 +129,7 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
|
|||||||
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
|
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
|
||||||
mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
|
mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
|
||||||
isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE);
|
isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE);
|
||||||
|
mNewAccountName = savedInstanceState.getString(NEW_ACCOUNT_NAME_STATE);
|
||||||
|
|
||||||
if (!mNullAccessToken && mAccessToken == null) {
|
if (!mNullAccessToken && mAccessToken == null) {
|
||||||
getCurrentAccountAndInitializeViewPager();
|
getCurrentAccountAndInitializeViewPager();
|
||||||
@ -337,15 +343,30 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void getCurrentAccountAndInitializeViewPager() {
|
private void getCurrentAccountAndInitializeViewPager() {
|
||||||
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
|
if(mNewAccountName != null) {
|
||||||
if (account == null) {
|
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, () -> {
|
||||||
mNullAccessToken = true;
|
mNewAccountName = null;
|
||||||
} else {
|
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
|
||||||
mAccessToken = account.getAccessToken();
|
if (account == null) {
|
||||||
mAccountName = account.getUsername();
|
mNullAccessToken = true;
|
||||||
}
|
} else {
|
||||||
initializeViewPager();
|
mAccessToken = account.getAccessToken();
|
||||||
}).execute();
|
mAccountName = account.getUsername();
|
||||||
|
}
|
||||||
|
initializeViewPager();
|
||||||
|
}).execute();
|
||||||
|
}).execute();
|
||||||
|
} else {
|
||||||
|
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
|
||||||
|
if (account == null) {
|
||||||
|
mNullAccessToken = true;
|
||||||
|
} else {
|
||||||
|
mAccessToken = account.getAccessToken();
|
||||||
|
mAccountName = account.getUsername();
|
||||||
|
}
|
||||||
|
initializeViewPager();
|
||||||
|
}).execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeViewPager() {
|
private void initializeViewPager() {
|
||||||
@ -484,6 +505,7 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
|
|||||||
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
|
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
|
||||||
outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
|
outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
|
||||||
outState.putString(ACCOUNT_NAME_STATE, mAccountName);
|
outState.putString(ACCOUNT_NAME_STATE, mAccountName);
|
||||||
|
outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showMessage(int resId, boolean retry) {
|
private void showMessage(int resId, boolean retry) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user