Set the good account when opening the app from a notification

This commit is contained in:
Shinokuni 2020-04-20 22:47:29 +02:00
parent 3c40d8eb23
commit 76e126093f
8 changed files with 37 additions and 13 deletions

View File

@ -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()
}

View File

@ -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();

View File

@ -64,8 +64,8 @@ public class DrawerManager {
this.headerListener = headerListener;
}
public Drawer buildDrawer(List<Account> accounts) {
createAccountHeader(accounts);
public Drawer buildDrawer(List<Account> accounts, int currentAccountId) {
createAccountHeader(accounts, currentAccountId);
drawer = new DrawerBuilder()
.withActivity(activity)
@ -129,14 +129,14 @@ public class DrawerManager {
}
}
private void createAccountHeader(List<Account> accounts) {
private void createAccountHeader(List<Account> 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<Account> accounts) {
header.clear();
addProfileSettingItems();

View File

@ -35,6 +35,8 @@ class SyncResultAnalyser(val context: Context, private val syncResults: Map<Acco
val account = syncResultMap.keys.first()
val feedsIdsForNewItems = getFeedsIdsForNewItems(syncResult)
notifContent.accountId = account.id
if (account.isNotificationsEnabled) {
val feeds = database.feedDao().selectFromIdList(feedsIdsForNewItems)

View File

@ -14,14 +14,11 @@ class SyncResultDebugData {
@TestOnly
fun oneAccountOneFeedOneItem(context: Context): Map<Account, SyncResult> {
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<Account, SyncResult>().apply {

View File

@ -8,4 +8,5 @@ class SyncResultNotifContent {
var content: String? = null
var largeIcon: Bitmap? = null
var item: Item? = null
var accountId: Int? = null
}

View File

@ -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!!)
}
}

View File

@ -23,6 +23,9 @@ public interface AccountDao extends BaseDao<Account> {
@Query("Select * from Account")
List<Account> 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);