Add a repository factory to better handle repository instantiation

This commit is contained in:
Shinokuni 2019-08-17 17:38:56 +02:00
parent 62eec1e077
commit 20ba537670
5 changed files with 41 additions and 60 deletions

View File

@ -102,4 +102,21 @@ public abstract class ARepository {
feed.setBackgroundColor(palette.getMutedSwatch().getRgb());
}
}
public static ARepository repositoryFactory(Account account, Account.AccountType accountType, Application application) throws Exception {
switch (accountType) {
case LOCAL:
return new LocalFeedRepository(application, account);
case NEXTCLOUD_NEWS:
return new NextNewsRepository(application, account);
case FRESHRSS:
return new FreshRSSRepository(application, account);
default:
throw new Exception("account type not supported");
}
}
public static ARepository repositoryFactory(Account account, Application application) throws Exception {
return ARepository.repositoryFactory(account, account.getAccountType(), application);
}
}

View File

@ -8,8 +8,6 @@ import androidx.lifecycle.AndroidViewModel;
import com.readrops.app.database.Database;
import com.readrops.app.database.entities.Account;
import com.readrops.app.repositories.ARepository;
import com.readrops.app.repositories.FreshRSSRepository;
import com.readrops.app.repositories.NextNewsRepository;
import io.reactivex.Completable;
import io.reactivex.Single;
@ -26,16 +24,7 @@ public class AccountViewModel extends AndroidViewModel {
}
public void setAccountType(Account.AccountType accountType) throws Exception {
switch (accountType) {
case NEXTCLOUD_NEWS:
repository = new NextNewsRepository(getApplication(), null);
break;
case FRESHRSS:
repository = new FreshRSSRepository(getApplication(), null);
break;
default:
throw new Exception("unknown account type");
}
repository = ARepository.repositoryFactory(null, accountType, getApplication());
}
public Single<Boolean> login(Account account, boolean insert) {

View File

@ -9,9 +9,6 @@ import androidx.lifecycle.LiveData;
import com.readrops.app.database.Database;
import com.readrops.app.database.entities.Account;
import com.readrops.app.repositories.ARepository;
import com.readrops.app.repositories.FreshRSSRepository;
import com.readrops.app.repositories.LocalFeedRepository;
import com.readrops.app.repositories.NextNewsRepository;
import com.readrops.app.utils.FeedInsertionResult;
import com.readrops.app.utils.HtmlParser;
import com.readrops.app.utils.ParsingResult;
@ -34,19 +31,15 @@ public class AddFeedsViewModel extends AndroidViewModel {
}
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results, Account account) {
switch (account.getAccountType()) {
case LOCAL:
repository = new LocalFeedRepository(getApplication(), account);
break;
case NEXTCLOUD_NEWS:
repository = new NextNewsRepository(getApplication(), account);
break;
case FRESHRSS:
repository = new FreshRSSRepository(getApplication(), account);
break;
}
try {
repository = ARepository.repositoryFactory(account, getApplication());
return repository.addFeeds(results);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Single<List<ParsingResult>> parseUrl(String url) {

View File

@ -17,9 +17,6 @@ import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Folder;
import com.readrops.app.database.pojo.ItemWithFeed;
import com.readrops.app.repositories.ARepository;
import com.readrops.app.repositories.FreshRSSRepository;
import com.readrops.app.repositories.LocalFeedRepository;
import com.readrops.app.repositories.NextNewsRepository;
import java.util.ArrayList;
import java.util.Arrays;
@ -54,23 +51,16 @@ public class MainViewModel extends AndroidViewModel {
queryBuilder.setSortType(MainActivity.ListSortType.NEWEST_TO_OLDEST);
db = Database.getInstance(application);
itemsWithFeed = new MediatorLiveData<>();
}
//region main query
private void setRepository(Account.AccountType accountType) {
switch (accountType) {
case LOCAL:
repository = new LocalFeedRepository(getApplication(), currentAccount);
break;
case NEXTCLOUD_NEWS:
repository = new NextNewsRepository(getApplication(), currentAccount);
break;
case FRESHRSS:
repository = new FreshRSSRepository(getApplication(), currentAccount);
break;
private void setRepository() {
try {
repository = ARepository.repositoryFactory(currentAccount, getApplication());
} catch (Exception e) {
e.printStackTrace();
}
}
@ -197,7 +187,7 @@ public class MainViewModel extends AndroidViewModel {
public void setCurrentAccount(Account currentAccount) {
this.currentAccount = currentAccount;
setRepository(currentAccount.getAccountType());
setRepository();
queryBuilder.setAccountId(currentAccount.getId());
buildPagedList();
@ -228,7 +218,7 @@ public class MainViewModel extends AndroidViewModel {
currentAccount = account1;
currentAccountExists = true;
setRepository(currentAccount.getAccountType());
setRepository();
queryBuilder.setAccountId(currentAccount.getId());
buildPagedList();
break;

View File

@ -1,9 +1,10 @@
package com.readrops.app.viewmodels;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.annotation.NonNull;
import com.readrops.app.database.Database;
import com.readrops.app.database.entities.Account;
@ -11,9 +12,6 @@ import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Folder;
import com.readrops.app.database.pojo.FeedWithFolder;
import com.readrops.app.repositories.ARepository;
import com.readrops.app.repositories.FreshRSSRepository;
import com.readrops.app.repositories.LocalFeedRepository;
import com.readrops.app.repositories.NextNewsRepository;
import java.util.List;
@ -35,20 +33,14 @@ public class ManageFeedsFoldersViewModel extends AndroidViewModel {
}
private void setup() {
switch (account.getAccountType()) {
case LOCAL:
repository = new LocalFeedRepository(getApplication(), account);
break;
case NEXTCLOUD_NEWS:
repository = new NextNewsRepository(getApplication(), account);
break;
case FRESHRSS:
repository = new FreshRSSRepository(getApplication(), account);
break;
}
try {
repository = ARepository.repositoryFactory(account, getApplication());
feedsWithFolder = db.feedDao().getAllFeedsWithFolder(account.getId());
folders = db.folderDao().getAllFolders(account.getId());
} catch (Exception e) {
e.printStackTrace();
}
}
public LiveData<List<FeedWithFolder>> getFeedsWithFolder() {