Add a repository factory to better handle repository instantiation
This commit is contained in:
parent
62eec1e077
commit
20ba537670
@ -102,4 +102,21 @@ public abstract class ARepository {
|
|||||||
feed.setBackgroundColor(palette.getMutedSwatch().getRgb());
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,6 @@ import androidx.lifecycle.AndroidViewModel;
|
|||||||
import com.readrops.app.database.Database;
|
import com.readrops.app.database.Database;
|
||||||
import com.readrops.app.database.entities.Account;
|
import com.readrops.app.database.entities.Account;
|
||||||
import com.readrops.app.repositories.ARepository;
|
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.Completable;
|
||||||
import io.reactivex.Single;
|
import io.reactivex.Single;
|
||||||
@ -26,16 +24,7 @@ public class AccountViewModel extends AndroidViewModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setAccountType(Account.AccountType accountType) throws Exception {
|
public void setAccountType(Account.AccountType accountType) throws Exception {
|
||||||
switch (accountType) {
|
repository = ARepository.repositoryFactory(null, accountType, getApplication());
|
||||||
case NEXTCLOUD_NEWS:
|
|
||||||
repository = new NextNewsRepository(getApplication(), null);
|
|
||||||
break;
|
|
||||||
case FRESHRSS:
|
|
||||||
repository = new FreshRSSRepository(getApplication(), null);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new Exception("unknown account type");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Single<Boolean> login(Account account, boolean insert) {
|
public Single<Boolean> login(Account account, boolean insert) {
|
||||||
|
@ -9,9 +9,6 @@ import androidx.lifecycle.LiveData;
|
|||||||
import com.readrops.app.database.Database;
|
import com.readrops.app.database.Database;
|
||||||
import com.readrops.app.database.entities.Account;
|
import com.readrops.app.database.entities.Account;
|
||||||
import com.readrops.app.repositories.ARepository;
|
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.FeedInsertionResult;
|
||||||
import com.readrops.app.utils.HtmlParser;
|
import com.readrops.app.utils.HtmlParser;
|
||||||
import com.readrops.app.utils.ParsingResult;
|
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) {
|
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results, Account account) {
|
||||||
switch (account.getAccountType()) {
|
try {
|
||||||
case LOCAL:
|
repository = ARepository.repositoryFactory(account, getApplication());
|
||||||
repository = new LocalFeedRepository(getApplication(), account);
|
|
||||||
break;
|
return repository.addFeeds(results);
|
||||||
case NEXTCLOUD_NEWS:
|
} catch (Exception e) {
|
||||||
repository = new NextNewsRepository(getApplication(), account);
|
e.printStackTrace();
|
||||||
break;
|
|
||||||
case FRESHRSS:
|
|
||||||
repository = new FreshRSSRepository(getApplication(), account);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return repository.addFeeds(results);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Single<List<ParsingResult>> parseUrl(String url) {
|
public Single<List<ParsingResult>> parseUrl(String url) {
|
||||||
|
@ -17,9 +17,6 @@ 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.pojo.ItemWithFeed;
|
import com.readrops.app.database.pojo.ItemWithFeed;
|
||||||
import com.readrops.app.repositories.ARepository;
|
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.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -54,23 +51,16 @@ public class MainViewModel extends AndroidViewModel {
|
|||||||
queryBuilder.setSortType(MainActivity.ListSortType.NEWEST_TO_OLDEST);
|
queryBuilder.setSortType(MainActivity.ListSortType.NEWEST_TO_OLDEST);
|
||||||
|
|
||||||
db = Database.getInstance(application);
|
db = Database.getInstance(application);
|
||||||
|
|
||||||
itemsWithFeed = new MediatorLiveData<>();
|
itemsWithFeed = new MediatorLiveData<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
//region main query
|
//region main query
|
||||||
|
|
||||||
private void setRepository(Account.AccountType accountType) {
|
private void setRepository() {
|
||||||
switch (accountType) {
|
try {
|
||||||
case LOCAL:
|
repository = ARepository.repositoryFactory(currentAccount, getApplication());
|
||||||
repository = new LocalFeedRepository(getApplication(), currentAccount);
|
} catch (Exception e) {
|
||||||
break;
|
e.printStackTrace();
|
||||||
case NEXTCLOUD_NEWS:
|
|
||||||
repository = new NextNewsRepository(getApplication(), currentAccount);
|
|
||||||
break;
|
|
||||||
case FRESHRSS:
|
|
||||||
repository = new FreshRSSRepository(getApplication(), currentAccount);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,7 +187,7 @@ public class MainViewModel extends AndroidViewModel {
|
|||||||
|
|
||||||
public void setCurrentAccount(Account currentAccount) {
|
public void setCurrentAccount(Account currentAccount) {
|
||||||
this.currentAccount = currentAccount;
|
this.currentAccount = currentAccount;
|
||||||
setRepository(currentAccount.getAccountType());
|
setRepository();
|
||||||
queryBuilder.setAccountId(currentAccount.getId());
|
queryBuilder.setAccountId(currentAccount.getId());
|
||||||
buildPagedList();
|
buildPagedList();
|
||||||
|
|
||||||
@ -228,7 +218,7 @@ public class MainViewModel extends AndroidViewModel {
|
|||||||
currentAccount = account1;
|
currentAccount = account1;
|
||||||
currentAccountExists = true;
|
currentAccountExists = true;
|
||||||
|
|
||||||
setRepository(currentAccount.getAccountType());
|
setRepository();
|
||||||
queryBuilder.setAccountId(currentAccount.getId());
|
queryBuilder.setAccountId(currentAccount.getId());
|
||||||
buildPagedList();
|
buildPagedList();
|
||||||
break;
|
break;
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package com.readrops.app.viewmodels;
|
package com.readrops.app.viewmodels;
|
||||||
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.lifecycle.AndroidViewModel;
|
import androidx.lifecycle.AndroidViewModel;
|
||||||
import androidx.lifecycle.LiveData;
|
import androidx.lifecycle.LiveData;
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
|
|
||||||
import com.readrops.app.database.Database;
|
import com.readrops.app.database.Database;
|
||||||
import com.readrops.app.database.entities.Account;
|
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.entities.Folder;
|
||||||
import com.readrops.app.database.pojo.FeedWithFolder;
|
import com.readrops.app.database.pojo.FeedWithFolder;
|
||||||
import com.readrops.app.repositories.ARepository;
|
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;
|
import java.util.List;
|
||||||
|
|
||||||
@ -35,20 +33,14 @@ public class ManageFeedsFoldersViewModel extends AndroidViewModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setup() {
|
private void setup() {
|
||||||
switch (account.getAccountType()) {
|
try {
|
||||||
case LOCAL:
|
repository = ARepository.repositoryFactory(account, getApplication());
|
||||||
repository = new LocalFeedRepository(getApplication(), account);
|
|
||||||
break;
|
|
||||||
case NEXTCLOUD_NEWS:
|
|
||||||
repository = new NextNewsRepository(getApplication(), account);
|
|
||||||
break;
|
|
||||||
case FRESHRSS:
|
|
||||||
repository = new FreshRSSRepository(getApplication(), account);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
feedsWithFolder = db.feedDao().getAllFeedsWithFolder(account.getId());
|
feedsWithFolder = db.feedDao().getAllFeedsWithFolder(account.getId());
|
||||||
folders = db.folderDao().getAllFolders(account.getId());
|
folders = db.folderDao().getAllFolders(account.getId());
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public LiveData<List<FeedWithFolder>> getFeedsWithFolder() {
|
public LiveData<List<FeedWithFolder>> getFeedsWithFolder() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user