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 59fd4b22..da0edd9c 100644 --- a/app/src/main/java/com/readrops/app/repositories/ARepository.java +++ b/app/src/main/java/com/readrops/app/repositories/ARepository.java @@ -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); + } } diff --git a/app/src/main/java/com/readrops/app/viewmodels/AccountViewModel.java b/app/src/main/java/com/readrops/app/viewmodels/AccountViewModel.java index 32acbfe5..f22d206f 100644 --- a/app/src/main/java/com/readrops/app/viewmodels/AccountViewModel.java +++ b/app/src/main/java/com/readrops/app/viewmodels/AccountViewModel.java @@ -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 login(Account account, boolean insert) { diff --git a/app/src/main/java/com/readrops/app/viewmodels/AddFeedsViewModel.java b/app/src/main/java/com/readrops/app/viewmodels/AddFeedsViewModel.java index d7dbf9cc..b9cd4945 100644 --- a/app/src/main/java/com/readrops/app/viewmodels/AddFeedsViewModel.java +++ b/app/src/main/java/com/readrops/app/viewmodels/AddFeedsViewModel.java @@ -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> addFeeds(List 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 repository.addFeeds(results); + return null; } public Single> parseUrl(String url) { diff --git a/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java b/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java index 5a110767..23e7cef7 100644 --- a/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java +++ b/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java @@ -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; diff --git a/app/src/main/java/com/readrops/app/viewmodels/ManageFeedsFoldersViewModel.java b/app/src/main/java/com/readrops/app/viewmodels/ManageFeedsFoldersViewModel.java index cb000e4d..d7135ee2 100644 --- a/app/src/main/java/com/readrops/app/viewmodels/ManageFeedsFoldersViewModel.java +++ b/app/src/main/java/com/readrops/app/viewmodels/ManageFeedsFoldersViewModel.java @@ -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()); + feedsWithFolder = db.feedDao().getAllFeedsWithFolder(account.getId()); + folders = db.folderDao().getAllFolders(account.getId()); + } catch (Exception e) { + e.printStackTrace(); + } } public LiveData> getFeedsWithFolder() {