From 76e126093f5fc35805a847ca2a6cc4575e0704e5 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Mon, 20 Apr 2020 22:47:29 +0200 Subject: [PATCH] Set the good account when opening the app from a notification --- .../com/readrops/app/SyncResultAnalyserTest.kt | 7 +++++++ .../com/readrops/app/activities/MainActivity.java | 9 ++++++++- .../com/readrops/app/utils/DrawerManager.java | 15 ++++++++++----- .../com/readrops/app/utils/SyncResultAnalyser.kt | 2 ++ .../com/readrops/app/utils/SyncResultDebugData.kt | 11 ++++------- .../readrops/app/utils/SyncResultNotifContent.kt | 1 + .../java/com/readrops/app/utils/SyncWorker.kt | 2 ++ .../com/readrops/readropsdb/dao/AccountDao.java | 3 +++ 8 files changed, 37 insertions(+), 13 deletions(-) diff --git a/app/src/androidTest/java/com/readrops/app/SyncResultAnalyserTest.kt b/app/src/androidTest/java/com/readrops/app/SyncResultAnalyserTest.kt index 5730d854..ab555e68 100644 --- a/app/src/androidTest/java/com/readrops/app/SyncResultAnalyserTest.kt +++ b/app/src/androidTest/java/com/readrops/app/SyncResultAnalyserTest.kt @@ -50,12 +50,15 @@ class SyncResultAnalyserTest { var account1Id = 0 database.accountDao().insert(account1).subscribe { id -> account1Id = id.toInt() } + account1.id = account1Id var account2Id = 0 database.accountDao().insert(account2).subscribe { id -> account2Id = id.toInt() } + account2.id = account2Id var account3Id = 0 database.accountDao().insert(account3).subscribe { id -> account3Id = id.toInt() } + account3.id = account3Id val accountIds = listOf(account1Id, account2Id, account3Id) for (i in 0..2) { @@ -94,6 +97,7 @@ class SyncResultAnalyserTest { assertEquals("caseOneElementEveryWhere", notifContent.content) assertEquals("feed 1", notifContent.title) assertTrue(notifContent.largeIcon != null) + assertTrue(notifContent.accountId!! > 0) database.itemDao() .delete(item) @@ -113,6 +117,7 @@ class SyncResultAnalyserTest { assertEquals(context.getString(R.string.new_items, 3), notifContent.content) assertEquals("feed 1", notifContent.title) assertTrue(notifContent.largeIcon != null) + assertTrue(notifContent.accountId!! > 0) } @Test @@ -126,6 +131,7 @@ class SyncResultAnalyserTest { assertEquals(context.getString(R.string.new_items, 2), notifContent.content) assertEquals(account1.accountName, notifContent.title) assertTrue(notifContent.largeIcon != null) + assertTrue(notifContent.accountId!! > 0) } @Test @@ -293,6 +299,7 @@ class SyncResultAnalyserTest { assertEquals("feed 1", notifContent.title) assertTrue(notifContent.largeIcon != null) assertTrue(notifContent.item != null) + assertTrue(notifContent.accountId!! > 0) database.itemDao().delete(item1).subscribe() } diff --git a/app/src/main/java/com/readrops/app/activities/MainActivity.java b/app/src/main/java/com/readrops/app/activities/MainActivity.java index e1517530..4f77a897 100644 --- a/app/src/main/java/com/readrops/app/activities/MainActivity.java +++ b/app/src/main/java/com/readrops/app/activities/MainActivity.java @@ -69,6 +69,7 @@ import io.reactivex.observers.DisposableSingleObserver; import io.reactivex.schedulers.Schedulers; import static com.readrops.app.utils.ReadropsKeys.ACCOUNT; +import static com.readrops.app.utils.ReadropsKeys.ACCOUNT_ID; import static com.readrops.app.utils.ReadropsKeys.FEEDS; import static com.readrops.app.utils.ReadropsKeys.FROM_MAIN_ACTIVITY; import static com.readrops.app.utils.ReadropsKeys.IMAGE_URL; @@ -204,7 +205,13 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou // the activity was just opened if (drawer == null) { - drawer = drawerManager.buildDrawer(accounts); + int currentAccountId = 0; + if (getIntent().hasExtra(ACCOUNT_ID)) { // coming from a notification + currentAccountId = getIntent().getIntExtra(ACCOUNT_ID, 1); + viewModel.setCurrentAccount(currentAccountId); + } + + drawer = drawerManager.buildDrawer(accounts, currentAccountId); drawer.setSelection(DrawerManager.ARTICLES_ITEM_ID); updateDrawerFeeds(); diff --git a/app/src/main/java/com/readrops/app/utils/DrawerManager.java b/app/src/main/java/com/readrops/app/utils/DrawerManager.java index 8fe17f0e..fab1f6a6 100644 --- a/app/src/main/java/com/readrops/app/utils/DrawerManager.java +++ b/app/src/main/java/com/readrops/app/utils/DrawerManager.java @@ -64,8 +64,8 @@ public class DrawerManager { this.headerListener = headerListener; } - public Drawer buildDrawer(List accounts) { - createAccountHeader(accounts); + public Drawer buildDrawer(List accounts, int currentAccountId) { + createAccountHeader(accounts, currentAccountId); drawer = new DrawerBuilder() .withActivity(activity) @@ -129,14 +129,14 @@ public class DrawerManager { } } - private void createAccountHeader(List accounts) { + private void createAccountHeader(List accounts, int currentAccountId) { ProfileDrawerItem[] profileItems = new ProfileDrawerItem[accounts.size()]; - int currentAccountId = 1; for (int i = 0; i < accounts.size(); i++) { Account account = accounts.get(i); - if (account.isCurrentAccount()) + // if currentAccount > 0, it means that the current account is no longer + if (account.isCurrentAccount() && currentAccountId == 0) currentAccountId = account.getId(); ProfileDrawerItem profileItem = createProfileItem(account); @@ -156,6 +156,7 @@ public class DrawerManager { .build(); addProfileSettingItems(); + header.setActiveProfile(currentAccountId); } @@ -250,6 +251,10 @@ public class DrawerManager { header.setActiveProfile(profileItem.getIdentifier()); } + public void setAccount(int accountId) { + header.setActiveProfile(accountId); + } + public void updateHeader(List accounts) { header.clear(); addProfileSettingItems(); diff --git a/app/src/main/java/com/readrops/app/utils/SyncResultAnalyser.kt b/app/src/main/java/com/readrops/app/utils/SyncResultAnalyser.kt index c7463ce5..dbd4f36b 100644 --- a/app/src/main/java/com/readrops/app/utils/SyncResultAnalyser.kt +++ b/app/src/main/java/com/readrops/app/utils/SyncResultAnalyser.kt @@ -35,6 +35,8 @@ class SyncResultAnalyser(val context: Context, private val syncResults: Map { - val account1 = Account().apply { - id = 1 - accountType = AccountType.FRESHRSS - isNotificationsEnabled = true - } - val database = Database.getInstance(context) - val item = database.itemDao().select(5056) + val account1 = database.accountDao().select(2) + + + val item = database.itemDao().select(5000) // database.feedDao().updateNotificationState(item.feedId, false).subscribe() return mutableMapOf().apply { diff --git a/app/src/main/java/com/readrops/app/utils/SyncResultNotifContent.kt b/app/src/main/java/com/readrops/app/utils/SyncResultNotifContent.kt index 86e799e4..454b8dc1 100644 --- a/app/src/main/java/com/readrops/app/utils/SyncResultNotifContent.kt +++ b/app/src/main/java/com/readrops/app/utils/SyncResultNotifContent.kt @@ -8,4 +8,5 @@ class SyncResultNotifContent { var content: String? = null var largeIcon: Bitmap? = null var item: Item? = null + var accountId: Int? = null } \ No newline at end of file diff --git a/app/src/main/java/com/readrops/app/utils/SyncWorker.kt b/app/src/main/java/com/readrops/app/utils/SyncWorker.kt index d65af30e..872a84fc 100644 --- a/app/src/main/java/com/readrops/app/utils/SyncWorker.kt +++ b/app/src/main/java/com/readrops/app/utils/SyncWorker.kt @@ -75,6 +75,8 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex if (notifContent.item != null) { putExtra(ReadropsKeys.ITEM_ID, notifContent.item?.id) putExtra(ReadropsKeys.IMAGE_URL, notifContent.item?.imageLink) + + if (notifContent.accountId != null) putExtra(ReadropsKeys.ACCOUNT_ID, notifContent.accountId!!) } } diff --git a/readropsdb/src/main/java/com/readrops/readropsdb/dao/AccountDao.java b/readropsdb/src/main/java/com/readrops/readropsdb/dao/AccountDao.java index e6b10ed6..e296ce2b 100644 --- a/readropsdb/src/main/java/com/readrops/readropsdb/dao/AccountDao.java +++ b/readropsdb/src/main/java/com/readrops/readropsdb/dao/AccountDao.java @@ -23,6 +23,9 @@ public interface AccountDao extends BaseDao { @Query("Select * from Account") List selectAll(); + @Query("Select * From Account Where id = :accountId") + Account select(int accountId); + @Query("Update Account set last_modified = :lastModified Where id = :accountId") void updateLastModified(int accountId, long lastModified);