From f0142843160a3308a4a2eb4fdbac61322c6b2e28 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Sun, 2 Aug 2020 15:16:05 +0200 Subject: [PATCH] Merge OPMLMatcher code into OPMLParser --- .../java/com/readrops/api/opml/OPMLParser.kt | 88 ++++++++++++++----- .../settings/AccountSettingsFragment.java | 11 +-- .../app/utils/matchers/OPMLMatcher.kt | 59 ------------- .../app/viewmodels/AccountViewModel.java | 11 +-- 4 files changed, 74 insertions(+), 95 deletions(-) delete mode 100644 app/src/main/java/com/readrops/app/utils/matchers/OPMLMatcher.kt diff --git a/api/src/main/java/com/readrops/api/opml/OPMLParser.kt b/api/src/main/java/com/readrops/api/opml/OPMLParser.kt index d3dd9c82..92d7b883 100644 --- a/api/src/main/java/com/readrops/api/opml/OPMLParser.kt +++ b/api/src/main/java/com/readrops/api/opml/OPMLParser.kt @@ -2,39 +2,87 @@ package com.readrops.api.opml import android.content.Context import android.net.Uri +import com.readrops.api.opml.model.Body +import com.readrops.api.opml.model.Head import com.readrops.api.opml.model.OPML +import com.readrops.api.opml.model.Outline import com.readrops.api.utils.LibUtils +import com.readrops.db.entities.Feed +import com.readrops.db.entities.Folder import io.reactivex.Completable import io.reactivex.Single import org.simpleframework.xml.Serializer import org.simpleframework.xml.core.Persister import java.io.OutputStream -class OPMLParser { +object OPMLParser { - companion object { - @JvmStatic - fun read(uri: Uri, context: Context): Single { - return Single.create { emitter -> - val fileString = LibUtils.fileToString(uri, context) - val serializer: Serializer = Persister() + @JvmStatic + fun read(uri: Uri, context: Context): Single>> { + return Single.create { emitter -> + val fileString = LibUtils.fileToString(uri, context) + val serializer: Serializer = Persister() - val opml: OPML = serializer.read(OPML::class.java, fileString) + val opml: OPML = serializer.read(OPML::class.java, fileString) - emitter.onSuccess(opml) - } - } - - @JvmStatic - fun write(opml: OPML, outputStream: OutputStream): Completable { - return Completable.create { emitter -> - val serializer: Serializer = Persister() - serializer.write(opml, outputStream) - - emitter.onComplete() - } + emitter.onSuccess(opmltoFoldersAndFeeds(opml)) } } + @JvmStatic + fun write(foldersAndFeeds: Map>, outputStream: OutputStream): Completable { + return Completable.create { emitter -> + val serializer: Serializer = Persister() + serializer.write(foldersAndFeedsToOPML(foldersAndFeeds), outputStream) + + emitter.onComplete() + } + } + + private fun opmltoFoldersAndFeeds(opml: OPML): Map> { + val foldersAndFeeds: MutableMap> = HashMap() + val body = opml.body!! + + body.outlines?.forEach { outline -> + val folder = Folder(outline.title) + + val feeds = arrayListOf() + outline.outlines?.forEach { feedOutline -> + val feed = Feed().apply { + name = feedOutline.title + url = feedOutline.xmlUrl + siteUrl = feedOutline.htmlUrl + } + + feeds.add(feed) + } + + foldersAndFeeds[folder] = feeds + } + + return foldersAndFeeds + } + + private fun foldersAndFeedsToOPML(foldersAndFeeds: Map>): OPML { + val outlines = arrayListOf() + for (folderAndFeeds in foldersAndFeeds) { + val outline = Outline(folderAndFeeds.key.name) + + val feedOutlines = arrayListOf() + folderAndFeeds.value.forEach { feed -> + val feedOutline = Outline(feed.name, feed.url, feed.siteUrl) + + feedOutlines.add(feedOutline) + } + + outline.outlines = feedOutlines + outlines.add(outline) + } + + val head = Head("Subscriptions") + val body = Body(outlines) + + return OPML("2.0", head, body) + } } \ No newline at end of file diff --git a/app/src/main/java/com/readrops/app/fragments/settings/AccountSettingsFragment.java b/app/src/main/java/com/readrops/app/fragments/settings/AccountSettingsFragment.java index 9cf70c46..a2221f56 100644 --- a/app/src/main/java/com/readrops/app/fragments/settings/AccountSettingsFragment.java +++ b/app/src/main/java/com/readrops/app/fragments/settings/AccountSettingsFragment.java @@ -22,6 +22,7 @@ import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; import com.afollestad.materialdialogs.MaterialDialog; +import com.readrops.api.opml.OPMLParser; import com.readrops.app.R; import com.readrops.app.ReadropsApp; import com.readrops.app.activities.AddAccountActivity; @@ -30,12 +31,9 @@ import com.readrops.app.activities.NotificationPermissionActivity; import com.readrops.app.utils.PermissionManager; import com.readrops.app.utils.SharedPreferencesManager; import com.readrops.app.utils.Utils; -import com.readrops.app.utils.matchers.OPMLMatcher; import com.readrops.app.viewmodels.AccountViewModel; import com.readrops.db.entities.account.Account; import com.readrops.db.entities.account.AccountType; -import com.readrops.api.opml.OPMLParser; -import com.readrops.api.opml.model.OPML; import java.io.File; import java.io.FileOutputStream; @@ -247,11 +245,8 @@ public class AccountSettingsFragment extends PreferenceFragmentCompat { final OutputStream outputStream = new FileOutputStream(file); viewModel.getFoldersWithFeeds() - .flatMapCompletable(folderListMap -> { - OPML opml = OPMLMatcher.INSTANCE.foldersAndFeedsToOPML(folderListMap, getContext()); - - return OPMLParser.write(opml, outputStream); - }).subscribeOn(Schedulers.io()) + .flatMapCompletable(folderListMap -> OPMLParser.write(folderListMap, outputStream)) + .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .doAfterTerminate(() -> { try { diff --git a/app/src/main/java/com/readrops/app/utils/matchers/OPMLMatcher.kt b/app/src/main/java/com/readrops/app/utils/matchers/OPMLMatcher.kt deleted file mode 100644 index d0cd1e42..00000000 --- a/app/src/main/java/com/readrops/app/utils/matchers/OPMLMatcher.kt +++ /dev/null @@ -1,59 +0,0 @@ -package com.readrops.app.utils.matchers - -import android.content.Context -import com.readrops.app.R -import com.readrops.db.entities.Feed -import com.readrops.db.entities.Folder -import com.readrops.api.opml.model.Body -import com.readrops.api.opml.model.Head -import com.readrops.api.opml.model.OPML -import com.readrops.api.opml.model.Outline - -object OPMLMatcher { - - fun opmltoFoldersAndFeeds(opml: OPML): Map> { - val foldersAndFeeds: MutableMap> = HashMap() - val body = opml.body!! - - body.outlines?.forEach { outline -> - val folder = Folder(outline.title) - - val feeds = arrayListOf() - outline.outlines?.forEach { feedOutline -> - val feed = Feed().apply { - name = feedOutline.title - url = feedOutline.xmlUrl - siteUrl = feedOutline.htmlUrl - } - - feeds.add(feed) - } - - foldersAndFeeds[folder] = feeds - } - - return foldersAndFeeds - } - - fun foldersAndFeedsToOPML(foldersAndFeeds: Map>, context: Context): OPML { - val outlines = arrayListOf() - for (folderAndFeeds in foldersAndFeeds) { - val outline = Outline(folderAndFeeds.key.name) - - val feedOutlines = arrayListOf() - folderAndFeeds.value.forEach { feed -> - val feedOutline = Outline(feed.name, feed.url, feed.siteUrl) - - feedOutlines.add(feedOutline) - } - - outline.outlines = feedOutlines - outlines.add(outline) - } - - val head = Head(context.getString(R.string.subscriptions)) - val body = Body(outlines) - - return OPML("2.0", head, body) - } -} \ No newline at end of file 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 40fc04dc..b84b4340 100644 --- a/app/src/main/java/com/readrops/app/viewmodels/AccountViewModel.java +++ b/app/src/main/java/com/readrops/app/viewmodels/AccountViewModel.java @@ -7,14 +7,13 @@ import android.util.Log; import androidx.annotation.NonNull; import androidx.lifecycle.AndroidViewModel; +import com.readrops.api.opml.OPMLParser; +import com.readrops.app.repositories.ARepository; import com.readrops.db.Database; import com.readrops.db.entities.Feed; import com.readrops.db.entities.Folder; import com.readrops.db.entities.account.Account; import com.readrops.db.entities.account.AccountType; -import com.readrops.app.repositories.ARepository; -import com.readrops.app.utils.matchers.OPMLMatcher; -import com.readrops.api.opml.OPMLParser; import java.util.List; import java.util.Map; @@ -73,10 +72,6 @@ public class AccountViewModel extends AndroidViewModel { public Completable parseOPMLFile(Uri uri) { return OPMLParser.read(uri, getApplication()) - .flatMapCompletable(opml -> { - Map> foldersAndFeeds = OPMLMatcher.INSTANCE.opmltoFoldersAndFeeds(opml); - - return repository.insertOPMLFoldersAndFeeds(foldersAndFeeds); - }); + .flatMapCompletable(foldersAndFeeds -> repository.insertOPMLFoldersAndFeeds(foldersAndFeeds)); } }