From 84370395f292f6e4037b9c20da7d01a2087ce3c5 Mon Sep 17 00:00:00 2001 From: Ash Date: Wed, 30 Mar 2022 00:16:07 +0800 Subject: [PATCH] Fix import from OPML --- .../java/me/ash/reader/data/group/GroupDao.kt | 8 ++++ .../reader/data/repository/OpmlRepository.kt | 8 +++- .../reader/data/source/OpmlLocalDataSource.kt | 42 ++++++++++++++----- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/me/ash/reader/data/group/GroupDao.kt b/app/src/main/java/me/ash/reader/data/group/GroupDao.kt index 5da24e0..9d2e5cc 100644 --- a/app/src/main/java/me/ash/reader/data/group/GroupDao.kt +++ b/app/src/main/java/me/ash/reader/data/group/GroupDao.kt @@ -5,6 +5,14 @@ import kotlinx.coroutines.flow.Flow @Dao interface GroupDao { + @Query( + """ + SELECT * FROM `group` + WHERE id = :id + """ + ) + fun queryById(id: String): Group? + @Transaction @Query( """ diff --git a/app/src/main/java/me/ash/reader/data/repository/OpmlRepository.kt b/app/src/main/java/me/ash/reader/data/repository/OpmlRepository.kt index 68ce39b..e69f9fb 100644 --- a/app/src/main/java/me/ash/reader/data/repository/OpmlRepository.kt +++ b/app/src/main/java/me/ash/reader/data/repository/OpmlRepository.kt @@ -16,9 +16,13 @@ class OpmlRepository @Inject constructor( ) { suspend fun saveToDatabase(inputStream: InputStream) { try { - val groupWithFeedList = opmlLocalDataSource.parseFileInputStream(inputStream) + val defaultGroup = groupDao.queryById(opmlLocalDataSource.getDefaultGroupId())!! + val groupWithFeedList = + opmlLocalDataSource.parseFileInputStream(inputStream, defaultGroup) groupWithFeedList.forEach { groupWithFeed -> - groupDao.insert(groupWithFeed.group) + if (groupWithFeed.group != defaultGroup) { + groupDao.insert(groupWithFeed.group) + } val repeatList = mutableListOf() groupWithFeed.feeds.forEach { it.groupId = groupWithFeed.group.id diff --git a/app/src/main/java/me/ash/reader/data/source/OpmlLocalDataSource.kt b/app/src/main/java/me/ash/reader/data/source/OpmlLocalDataSource.kt index 1c1f43d..6858611 100644 --- a/app/src/main/java/me/ash/reader/data/source/OpmlLocalDataSource.kt +++ b/app/src/main/java/me/ash/reader/data/source/OpmlLocalDataSource.kt @@ -4,10 +4,11 @@ import android.content.Context import android.util.Log import android.util.Xml import dagger.hilt.android.qualifiers.ApplicationContext -import me.ash.reader.currentAccountId +import me.ash.reader.* import me.ash.reader.data.feed.Feed import me.ash.reader.data.group.Group import me.ash.reader.data.group.GroupWithFeed +import me.ash.reader.data.repository.StringsRepository import org.xmlpull.v1.XmlPullParser import java.io.InputStream import java.util.* @@ -16,9 +17,18 @@ import javax.inject.Inject class OpmlLocalDataSource @Inject constructor( @ApplicationContext private val context: Context, + private val stringsRepository: StringsRepository, ) { -// @Throws(XmlPullParserException::class, IOException::class) - fun parseFileInputStream(inputStream: InputStream): List { + fun getDefaultGroupId(): String { + val readYouString = stringsRepository.getString(R.string.read_you) + val defaultString = stringsRepository.getString(R.string.defaults) + return context.dataStore + .get(DataStoreKeys.CurrentAccountId)!! + .spacerDollar(readYouString + defaultString) + } + + // @Throws(XmlPullParserException::class, IOException::class) + fun parseFileInputStream(inputStream: InputStream, defaultGroup: Group): List { val groupWithFeedList = mutableListOf() val accountId = context.currentAccountId inputStream.use { @@ -37,15 +47,25 @@ class OpmlLocalDataSource @Inject constructor( val title = parser.getAttributeValue(null, "title") val xmlUrl = parser.getAttributeValue(null, "xmlUrl") Log.i("RLog", "rss: ${title} , ${xmlUrl}") - groupWithFeedList.last().feeds.add( - Feed( - id = UUID.randomUUID().toString(), - name = title, - url = xmlUrl, - groupId = UUID.randomUUID().toString(), - accountId = accountId, + if (groupWithFeedList.isEmpty()) { + groupWithFeedList.add( + GroupWithFeed( + group = defaultGroup, + feeds = mutableListOf() + ) ) - ) + } + groupWithFeedList.last().let { groupWithFeed -> + groupWithFeed.feeds.add( + Feed( + id = UUID.randomUUID().toString(), + name = title, + url = xmlUrl, + groupId = groupWithFeed.group.id, + accountId = accountId, + ) + ) + } } else { val title = parser.getAttributeValue(null, "title") Log.i("RLog", "title: ${title}")