Generify Repository to have only one instance of API per repository

This commit is contained in:
Shinokuni 2019-08-23 22:40:29 +02:00
parent a585d7442a
commit 421794f995
5 changed files with 39 additions and 44 deletions

View File

@ -37,10 +37,10 @@ import com.mikepenz.materialdrawer.model.SecondaryDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem; import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.readrops.app.R; import com.readrops.app.R;
import com.readrops.app.adapters.MainItemListAdapter; 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.Feed;
import com.readrops.app.database.entities.Folder; 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.database.pojo.ItemWithFeed;
import com.readrops.app.fragments.settings.AccountSettingsFragment; import com.readrops.app.fragments.settings.AccountSettingsFragment;
import com.readrops.app.utils.DrawerManager; import com.readrops.app.utils.DrawerManager;
@ -177,6 +177,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
WeakReference<Account> accountWeakReference = new WeakReference<>(currentAccount); WeakReference<Account> accountWeakReference = new WeakReference<>(currentAccount);
viewModel.getAllAccounts().observe(this, accounts -> { viewModel.getAllAccounts().observe(this, accounts -> {
getAccountCredentials(accounts);
viewModel.setAccounts(accounts); viewModel.setAccounts(accounts);
if (drawer == null) { if (drawer == null) {
@ -204,6 +205,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
}); });
} }
private void handleDrawerClick(IDrawerItem drawerItem) { private void handleDrawerClick(IDrawerItem drawerItem) {
if (drawerItem instanceof PrimaryDrawerItem) { if (drawerItem instanceof PrimaryDrawerItem) {
drawer.closeDrawer(); drawer.closeDrawer();
@ -528,13 +530,6 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
} }
private void sync(@Nullable List<Feed> feeds) { private void sync(@Nullable List<Feed> 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) viewModel.sync(feeds)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
@ -652,6 +647,17 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
.show(); .show();
} }
private void getAccountCredentials(List<Account> 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 @Override
protected void onDestroy() { protected void onDestroy() {
if (syncDisposable != null && !syncDisposable.isDisposed()) if (syncDisposable != null && !syncDisposable.isDisposed())

View File

@ -27,11 +27,13 @@ import io.reactivex.Observable;
import io.reactivex.Single; import io.reactivex.Single;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
public abstract class ARepository { public abstract class ARepository<T> {
protected Database database; protected Database database;
protected Account account; protected Account account;
protected T api;
protected ARepository(@NonNull Application application, @Nullable Account account) { protected ARepository(@NonNull Application application, @Nullable Account account) {
this.database = Database.getInstance(application); this.database = Database.getInstance(application);
this.account = account; this.account = account;

View File

@ -31,15 +31,21 @@ import io.reactivex.Completable;
import io.reactivex.Observable; import io.reactivex.Observable;
import io.reactivex.Single; import io.reactivex.Single;
public class FreshRSSRepository extends ARepository { public class FreshRSSRepository extends ARepository<FreshRSSAPI> {
public FreshRSSRepository(@NonNull Application application, @Nullable Account account) { public FreshRSSRepository(@NonNull Application application, @Nullable Account account) {
super(application, account); super(application, account);
if (account != null)
api = new FreshRSSAPI(account.toCredentials());
} }
@Override @Override
public Single<Boolean> login(Account account, boolean insert) { public Single<Boolean> 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()) return api.login(account.getLogin(), account.getPassword())
.flatMap(token -> { .flatMap(token -> {
@ -65,8 +71,6 @@ public class FreshRSSRepository extends ARepository {
@Override @Override
public Observable<Feed> sync(List<Feed> feeds) { public Observable<Feed> sync(List<Feed> feeds) {
FreshRSSAPI api = new FreshRSSAPI(account.toCredentials());
FreshRSSSyncData syncData = new FreshRSSSyncData(); FreshRSSSyncData syncData = new FreshRSSSyncData();
SyncType syncType; SyncType syncType;
@ -98,8 +102,6 @@ public class FreshRSSRepository extends ARepository {
@Override @Override
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results) { public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results) {
FreshRSSAPI api = new FreshRSSAPI(account.toCredentials());
List<Completable> completableList = new ArrayList<>(); List<Completable> completableList = new ArrayList<>();
for (ParsingResult result : results) { for (ParsingResult result : results) {
@ -113,8 +115,6 @@ public class FreshRSSRepository extends ARepository {
@Override @Override
public Completable updateFeed(Feed feed) { public Completable updateFeed(Feed feed) {
FreshRSSAPI api = new FreshRSSAPI(account.toCredentials());
return Single.<Folder>create(emitter -> { return Single.<Folder>create(emitter -> {
Folder folder = feed.getFolderId() == null ? null : database.folderDao().select(feed.getFolderId()); Folder folder = feed.getFolderId() == null ? null : database.folderDao().select(feed.getFolderId());
emitter.onSuccess(folder); emitter.onSuccess(folder);
@ -126,31 +126,23 @@ public class FreshRSSRepository extends ARepository {
@Override @Override
public Completable deleteFeed(Feed feed) { public Completable deleteFeed(Feed feed) {
FreshRSSAPI api = new FreshRSSAPI(account.toCredentials());
return api.deleteFeed(account.getWriteToken(), feed.getUrl()) return api.deleteFeed(account.getWriteToken(), feed.getUrl())
.andThen(super.deleteFeed(feed)); .andThen(super.deleteFeed(feed));
} }
@Override @Override
public Completable addFolder(Folder folder) { public Completable addFolder(Folder folder) {
FreshRSSAPI api = new FreshRSSAPI(account.toCredentials());
return api.createFolder(account.getWriteToken(), folder.getName()); return api.createFolder(account.getWriteToken(), folder.getName());
} }
@Override @Override
public Completable updateFolder(Folder folder) { public Completable updateFolder(Folder folder) {
FreshRSSAPI api = new FreshRSSAPI(account.toCredentials());
return api.updateFolder(account.getWriteToken(), folder.getRemoteId(), folder.getName()) return api.updateFolder(account.getWriteToken(), folder.getRemoteId(), folder.getName())
.andThen(super.updateFolder(folder)); .andThen(super.updateFolder(folder));
} }
@Override @Override
public Completable deleteFolder(Folder folder) { public Completable deleteFolder(Folder folder) {
FreshRSSAPI api = new FreshRSSAPI(account.toCredentials());
return api.deleteFolder(account.getWriteToken(), folder.getRemoteId()) return api.deleteFolder(account.getWriteToken(), folder.getRemoteId())
.andThen(super.deleteFolder(folder)); .andThen(super.deleteFolder(folder));
} }

View File

@ -37,7 +37,7 @@ import java.util.List;
import io.reactivex.Observable; import io.reactivex.Observable;
import io.reactivex.Single; import io.reactivex.Single;
public class LocalFeedRepository extends ARepository { public class LocalFeedRepository extends ARepository<Void> {
private static final String TAG = LocalFeedRepository.class.getSimpleName(); private static final String TAG = LocalFeedRepository.class.getSimpleName();

View File

@ -40,19 +40,26 @@ import io.reactivex.Completable;
import io.reactivex.Observable; import io.reactivex.Observable;
import io.reactivex.Single; import io.reactivex.Single;
public class NextNewsRepository extends ARepository { public class NextNewsRepository extends ARepository<NextNewsAPI> {
private static final String TAG = NextNewsRepository.class.getSimpleName(); private static final String TAG = NextNewsRepository.class.getSimpleName();
public NextNewsRepository(@NonNull Application application, @Nullable Account account) { public NextNewsRepository(@NonNull Application application, @Nullable Account account) {
super(application, account); super(application, account);
if (account != null)
api = new NextNewsAPI(account.toCredentials());
} }
@Override @Override
public Single<Boolean> login(Account account, boolean insert) { public Single<Boolean> login(Account account, boolean insert) {
return Single.create(emitter -> { return Single.create(emitter -> {
NextNewsAPI newsAPI = new NextNewsAPI(account.toCredentials()); if (api == null)
NextNewsUser user = newsAPI.login(); api = new NextNewsAPI(account.toCredentials());
else
api.setCredentials(account.toCredentials());
NextNewsUser user = api.login();
if (user != null) { if (user != null) {
account.setDisplayedName(user.getDisplayName()); account.setDisplayedName(user.getDisplayName());
@ -70,7 +77,6 @@ public class NextNewsRepository extends ARepository {
public Observable<Feed> sync(List<Feed> feeds) { public Observable<Feed> sync(List<Feed> feeds) {
return Observable.create(emitter -> { return Observable.create(emitter -> {
try { try {
NextNewsAPI newsAPI = new NextNewsAPI(account.toCredentials());
long lastModified = LocalDateTime.now().toDateTime().getMillis(); long lastModified = LocalDateTime.now().toDateTime().getMillis();
SyncType syncType; SyncType syncType;
@ -88,7 +94,7 @@ public class NextNewsRepository extends ARepository {
} }
TimingLogger timings = new TimingLogger(TAG, "nextcloud news " + syncType.name().toLowerCase()); 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"); timings.addSplit("server queries");
if (!syncResult.isError()) { if (!syncResult.isError()) {
@ -122,13 +128,12 @@ public class NextNewsRepository extends ARepository {
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results) { public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results) {
return Single.create(emitter -> { return Single.create(emitter -> {
List<FeedInsertionResult> feedInsertionResults = new ArrayList<>(); List<FeedInsertionResult> feedInsertionResults = new ArrayList<>();
NextNewsAPI newsAPI = new NextNewsAPI(account.toCredentials());
for (ParsingResult result : results) { for (ParsingResult result : results) {
FeedInsertionResult insertionResult = new FeedInsertionResult(); FeedInsertionResult insertionResult = new FeedInsertionResult();
try { try {
NextNewsFeeds nextNewsFeeds = newsAPI.createFeed(result.getUrl(), 0); NextNewsFeeds nextNewsFeeds = api.createFeed(result.getUrl(), 0);
if (nextNewsFeeds != null) { if (nextNewsFeeds != null) {
List<Feed> newFeeds = insertFeeds(nextNewsFeeds.getFeeds()); List<Feed> newFeeds = insertFeeds(nextNewsFeeds.getFeeds());
@ -159,8 +164,6 @@ public class NextNewsRepository extends ARepository {
@Override @Override
public Completable updateFeed(Feed feed) { public Completable updateFeed(Feed feed) {
return Completable.create(emitter -> { return Completable.create(emitter -> {
NextNewsAPI api = new NextNewsAPI(account.toCredentials());
Folder folder = feed.getFolderId() == null ? null : database.folderDao().select(feed.getFolderId()); Folder folder = feed.getFolderId() == null ? null : database.folderDao().select(feed.getFolderId());
NextNewsRenameFeed newsRenameFeed = new NextNewsRenameFeed(Integer.parseInt(feed.getRemoteId()), feed.getName()); NextNewsRenameFeed newsRenameFeed = new NextNewsRenameFeed(Integer.parseInt(feed.getRemoteId()), feed.getName());
@ -184,8 +187,6 @@ public class NextNewsRepository extends ARepository {
@Override @Override
public Completable deleteFeed(Feed feed) { public Completable deleteFeed(Feed feed) {
return Completable.create(emitter -> { return Completable.create(emitter -> {
NextNewsAPI api = new NextNewsAPI(account.toCredentials());
try { try {
if (api.deleteFeed(Integer.parseInt(feed.getRemoteId()))) { if (api.deleteFeed(Integer.parseInt(feed.getRemoteId()))) {
emitter.onComplete(); emitter.onComplete();
@ -202,8 +203,6 @@ public class NextNewsRepository extends ARepository {
@Override @Override
public Completable addFolder(Folder folder) { public Completable addFolder(Folder folder) {
return Completable.create(emitter -> { return Completable.create(emitter -> {
NextNewsAPI api = new NextNewsAPI(account.toCredentials());
try { try {
int folderRemoteId = folder.getRemoteId() == null ? 0 : Integer.parseInt(folder.getRemoteId()); int folderRemoteId = folder.getRemoteId() == null ? 0 : Integer.parseInt(folder.getRemoteId());
NextNewsFolders folders = api.createFolder(new NextNewsFolder(folderRemoteId, folder.getName())); NextNewsFolders folders = api.createFolder(new NextNewsFolder(folderRemoteId, folder.getName()));
@ -227,8 +226,6 @@ public class NextNewsRepository extends ARepository {
@Override @Override
public Completable updateFolder(Folder folder) { public Completable updateFolder(Folder folder) {
return Completable.create(emitter -> { return Completable.create(emitter -> {
NextNewsAPI api = new NextNewsAPI(account.toCredentials());
try { try {
if (api.renameFolder(new NextNewsFolder(Integer.parseInt(folder.getRemoteId()), folder.getName()))) { if (api.renameFolder(new NextNewsFolder(Integer.parseInt(folder.getRemoteId()), folder.getName()))) {
emitter.onComplete(); emitter.onComplete();
@ -246,8 +243,6 @@ public class NextNewsRepository extends ARepository {
@Override @Override
public Completable deleteFolder(Folder folder) { public Completable deleteFolder(Folder folder) {
return Completable.create(emitter -> { return Completable.create(emitter -> {
NextNewsAPI api = new NextNewsAPI(account.toCredentials());
try { try {
if (api.deleteFolder(new NextNewsFolder(Integer.parseInt(folder.getRemoteId()), folder.getName()))) { if (api.deleteFolder(new NextNewsFolder(Integer.parseInt(folder.getRemoteId()), folder.getName()))) {
emitter.onComplete(); emitter.onComplete();