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 06f0edb2..dfdf0ccb 100644 --- a/app/src/main/java/com/readrops/app/activities/MainActivity.java +++ b/app/src/main/java/com/readrops/app/activities/MainActivity.java @@ -37,10 +37,10 @@ import com.mikepenz.materialdrawer.model.SecondaryDrawerItem; import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem; import com.readrops.app.R; import com.readrops.app.adapters.MainItemListAdapter; -import com.readrops.app.database.entities.account.Account; -import com.readrops.app.database.entities.account.AccountType; import com.readrops.app.database.entities.Feed; import com.readrops.app.database.entities.Folder; +import com.readrops.app.database.entities.account.Account; +import com.readrops.app.database.entities.account.AccountType; import com.readrops.app.database.pojo.ItemWithFeed; import com.readrops.app.fragments.settings.AccountSettingsFragment; import com.readrops.app.utils.DrawerManager; @@ -177,6 +177,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou WeakReference accountWeakReference = new WeakReference<>(currentAccount); viewModel.getAllAccounts().observe(this, accounts -> { + getAccountCredentials(accounts); viewModel.setAccounts(accounts); if (drawer == null) { @@ -204,6 +205,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou }); } + private void handleDrawerClick(IDrawerItem drawerItem) { if (drawerItem instanceof PrimaryDrawerItem) { drawer.closeDrawer(); @@ -528,13 +530,6 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou } private void sync(@Nullable List feeds) { - Account account = viewModel.getCurrentAccount(); - if (account.getLogin() == null) - account.setLogin(SharedPreferencesManager.readString(this, account.getLoginKey())); - - if (viewModel.getCurrentAccount().getPassword() == null) - account.setPassword(SharedPreferencesManager.readString(this, account.getPasswordKey())); - viewModel.sync(feeds) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) @@ -652,6 +647,17 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou .show(); } + private void getAccountCredentials(List accounts) { + for (Account account : accounts) { + if (account.getLogin() == null) + account.setLogin(SharedPreferencesManager.readString(this, account.getLoginKey())); + + if (account.getPassword() == null) + account.setPassword(SharedPreferencesManager.readString(this, account.getPasswordKey())); + } + } + + @Override protected void onDestroy() { if (syncDisposable != null && !syncDisposable.isDisposed()) diff --git a/app/src/main/java/com/readrops/app/repositories/ARepository.java b/app/src/main/java/com/readrops/app/repositories/ARepository.java index bcd54d01..af9fc038 100644 --- a/app/src/main/java/com/readrops/app/repositories/ARepository.java +++ b/app/src/main/java/com/readrops/app/repositories/ARepository.java @@ -27,11 +27,13 @@ import io.reactivex.Observable; import io.reactivex.Single; import io.reactivex.schedulers.Schedulers; -public abstract class ARepository { +public abstract class ARepository { protected Database database; protected Account account; + protected T api; + protected ARepository(@NonNull Application application, @Nullable Account account) { this.database = Database.getInstance(application); this.account = account; diff --git a/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java b/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java index 65baaba5..fc17e367 100644 --- a/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java +++ b/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java @@ -31,15 +31,21 @@ import io.reactivex.Completable; import io.reactivex.Observable; import io.reactivex.Single; -public class FreshRSSRepository extends ARepository { +public class FreshRSSRepository extends ARepository { public FreshRSSRepository(@NonNull Application application, @Nullable Account account) { super(application, account); + + if (account != null) + api = new FreshRSSAPI(account.toCredentials()); } @Override public Single login(Account account, boolean insert) { - FreshRSSAPI api = new FreshRSSAPI(new FreshRSSCredentials(null, account.getUrl())); + if (api == null) + api = new FreshRSSAPI(account.toCredentials()); + else + api.setCredentials(account.toCredentials()); return api.login(account.getLogin(), account.getPassword()) .flatMap(token -> { @@ -65,8 +71,6 @@ public class FreshRSSRepository extends ARepository { @Override public Observable sync(List feeds) { - FreshRSSAPI api = new FreshRSSAPI(account.toCredentials()); - FreshRSSSyncData syncData = new FreshRSSSyncData(); SyncType syncType; @@ -98,8 +102,6 @@ public class FreshRSSRepository extends ARepository { @Override public Single> addFeeds(List results) { - FreshRSSAPI api = new FreshRSSAPI(account.toCredentials()); - List completableList = new ArrayList<>(); for (ParsingResult result : results) { @@ -113,8 +115,6 @@ public class FreshRSSRepository extends ARepository { @Override public Completable updateFeed(Feed feed) { - FreshRSSAPI api = new FreshRSSAPI(account.toCredentials()); - return Single.create(emitter -> { Folder folder = feed.getFolderId() == null ? null : database.folderDao().select(feed.getFolderId()); emitter.onSuccess(folder); @@ -126,31 +126,23 @@ public class FreshRSSRepository extends ARepository { @Override public Completable deleteFeed(Feed feed) { - FreshRSSAPI api = new FreshRSSAPI(account.toCredentials()); - return api.deleteFeed(account.getWriteToken(), feed.getUrl()) .andThen(super.deleteFeed(feed)); } @Override public Completable addFolder(Folder folder) { - FreshRSSAPI api = new FreshRSSAPI(account.toCredentials()); - return api.createFolder(account.getWriteToken(), folder.getName()); } @Override public Completable updateFolder(Folder folder) { - FreshRSSAPI api = new FreshRSSAPI(account.toCredentials()); - return api.updateFolder(account.getWriteToken(), folder.getRemoteId(), folder.getName()) .andThen(super.updateFolder(folder)); } @Override public Completable deleteFolder(Folder folder) { - FreshRSSAPI api = new FreshRSSAPI(account.toCredentials()); - return api.deleteFolder(account.getWriteToken(), folder.getRemoteId()) .andThen(super.deleteFolder(folder)); } diff --git a/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java b/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java index 54aaf773..db702233 100644 --- a/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java +++ b/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java @@ -37,7 +37,7 @@ import java.util.List; import io.reactivex.Observable; import io.reactivex.Single; -public class LocalFeedRepository extends ARepository { +public class LocalFeedRepository extends ARepository { private static final String TAG = LocalFeedRepository.class.getSimpleName(); diff --git a/app/src/main/java/com/readrops/app/repositories/NextNewsRepository.java b/app/src/main/java/com/readrops/app/repositories/NextNewsRepository.java index b2c3961d..417ebdce 100644 --- a/app/src/main/java/com/readrops/app/repositories/NextNewsRepository.java +++ b/app/src/main/java/com/readrops/app/repositories/NextNewsRepository.java @@ -40,19 +40,26 @@ import io.reactivex.Completable; import io.reactivex.Observable; import io.reactivex.Single; -public class NextNewsRepository extends ARepository { +public class NextNewsRepository extends ARepository { private static final String TAG = NextNewsRepository.class.getSimpleName(); public NextNewsRepository(@NonNull Application application, @Nullable Account account) { super(application, account); + + if (account != null) + api = new NextNewsAPI(account.toCredentials()); } @Override public Single login(Account account, boolean insert) { return Single.create(emitter -> { - NextNewsAPI newsAPI = new NextNewsAPI(account.toCredentials()); - NextNewsUser user = newsAPI.login(); + if (api == null) + api = new NextNewsAPI(account.toCredentials()); + else + api.setCredentials(account.toCredentials()); + + NextNewsUser user = api.login(); if (user != null) { account.setDisplayedName(user.getDisplayName()); @@ -70,7 +77,6 @@ public class NextNewsRepository extends ARepository { public Observable sync(List feeds) { return Observable.create(emitter -> { try { - NextNewsAPI newsAPI = new NextNewsAPI(account.toCredentials()); long lastModified = LocalDateTime.now().toDateTime().getMillis(); SyncType syncType; @@ -88,7 +94,7 @@ public class NextNewsRepository extends ARepository { } TimingLogger timings = new TimingLogger(TAG, "nextcloud news " + syncType.name().toLowerCase()); - NextNewsSyncResult syncResult = newsAPI.sync(syncType, syncData); + NextNewsSyncResult syncResult = api.sync(syncType, syncData); timings.addSplit("server queries"); if (!syncResult.isError()) { @@ -122,13 +128,12 @@ public class NextNewsRepository extends ARepository { public Single> addFeeds(List results) { return Single.create(emitter -> { List feedInsertionResults = new ArrayList<>(); - NextNewsAPI newsAPI = new NextNewsAPI(account.toCredentials()); for (ParsingResult result : results) { FeedInsertionResult insertionResult = new FeedInsertionResult(); try { - NextNewsFeeds nextNewsFeeds = newsAPI.createFeed(result.getUrl(), 0); + NextNewsFeeds nextNewsFeeds = api.createFeed(result.getUrl(), 0); if (nextNewsFeeds != null) { List newFeeds = insertFeeds(nextNewsFeeds.getFeeds()); @@ -159,8 +164,6 @@ public class NextNewsRepository extends ARepository { @Override public Completable updateFeed(Feed feed) { return Completable.create(emitter -> { - NextNewsAPI api = new NextNewsAPI(account.toCredentials()); - Folder folder = feed.getFolderId() == null ? null : database.folderDao().select(feed.getFolderId()); NextNewsRenameFeed newsRenameFeed = new NextNewsRenameFeed(Integer.parseInt(feed.getRemoteId()), feed.getName()); @@ -184,8 +187,6 @@ public class NextNewsRepository extends ARepository { @Override public Completable deleteFeed(Feed feed) { return Completable.create(emitter -> { - NextNewsAPI api = new NextNewsAPI(account.toCredentials()); - try { if (api.deleteFeed(Integer.parseInt(feed.getRemoteId()))) { emitter.onComplete(); @@ -202,8 +203,6 @@ public class NextNewsRepository extends ARepository { @Override public Completable addFolder(Folder folder) { return Completable.create(emitter -> { - NextNewsAPI api = new NextNewsAPI(account.toCredentials()); - try { int folderRemoteId = folder.getRemoteId() == null ? 0 : Integer.parseInt(folder.getRemoteId()); NextNewsFolders folders = api.createFolder(new NextNewsFolder(folderRemoteId, folder.getName())); @@ -227,8 +226,6 @@ public class NextNewsRepository extends ARepository { @Override public Completable updateFolder(Folder folder) { return Completable.create(emitter -> { - NextNewsAPI api = new NextNewsAPI(account.toCredentials()); - try { if (api.renameFolder(new NextNewsFolder(Integer.parseInt(folder.getRemoteId()), folder.getName()))) { emitter.onComplete(); @@ -246,8 +243,6 @@ public class NextNewsRepository extends ARepository { @Override public Completable deleteFolder(Folder folder) { return Completable.create(emitter -> { - NextNewsAPI api = new NextNewsAPI(account.toCredentials()); - try { if (api.deleteFolder(new NextNewsFolder(Integer.parseInt(folder.getRemoteId()), folder.getName()))) { emitter.onComplete();