Fix import from OPML

This commit is contained in:
Ash 2022-03-30 00:16:07 +08:00
parent 794d898514
commit 84370395f2
3 changed files with 45 additions and 13 deletions

View File

@ -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(
"""

View File

@ -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 ->
if (groupWithFeed.group != defaultGroup) {
groupDao.insert(groupWithFeed.group)
}
val repeatList = mutableListOf<Feed>()
groupWithFeed.feeds.forEach {
it.groupId = groupWithFeed.group.id

View File

@ -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,
) {
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): List<GroupWithFeed> {
fun parseFileInputStream(inputStream: InputStream, defaultGroup: Group): List<GroupWithFeed> {
val groupWithFeedList = mutableListOf<GroupWithFeed>()
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(
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 = UUID.randomUUID().toString(),
groupId = groupWithFeed.group.id,
accountId = accountId,
)
)
}
} else {
val title = parser.getAttributeValue(null, "title")
Log.i("RLog", "title: ${title}")