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.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<Account> 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<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)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
@ -652,6 +647,17 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
.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
protected void onDestroy() {
if (syncDisposable != null && !syncDisposable.isDisposed())

View File

@ -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<T> {
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;

View File

@ -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<FreshRSSAPI> {
public FreshRSSRepository(@NonNull Application application, @Nullable Account account) {
super(application, account);
if (account != null)
api = new FreshRSSAPI(account.toCredentials());
}
@Override
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())
.flatMap(token -> {
@ -65,8 +71,6 @@ public class FreshRSSRepository extends ARepository {
@Override
public Observable<Feed> sync(List<Feed> 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<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results) {
FreshRSSAPI api = new FreshRSSAPI(account.toCredentials());
List<Completable> 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.<Folder>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));
}

View File

@ -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<Void> {
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.Single;
public class NextNewsRepository extends ARepository {
public class NextNewsRepository extends ARepository<NextNewsAPI> {
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<Boolean> 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<Feed> sync(List<Feed> 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<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results) {
return Single.create(emitter -> {
List<FeedInsertionResult> 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<Feed> 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();