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 @Dao
interface GroupDao { interface GroupDao {
@Query(
"""
SELECT * FROM `group`
WHERE id = :id
"""
)
fun queryById(id: String): Group?
@Transaction @Transaction
@Query( @Query(
""" """

View File

@ -16,9 +16,13 @@ class OpmlRepository @Inject constructor(
) { ) {
suspend fun saveToDatabase(inputStream: InputStream) { suspend fun saveToDatabase(inputStream: InputStream) {
try { try {
val groupWithFeedList = opmlLocalDataSource.parseFileInputStream(inputStream) val defaultGroup = groupDao.queryById(opmlLocalDataSource.getDefaultGroupId())!!
val groupWithFeedList =
opmlLocalDataSource.parseFileInputStream(inputStream, defaultGroup)
groupWithFeedList.forEach { groupWithFeed -> groupWithFeedList.forEach { groupWithFeed ->
groupDao.insert(groupWithFeed.group) if (groupWithFeed.group != defaultGroup) {
groupDao.insert(groupWithFeed.group)
}
val repeatList = mutableListOf<Feed>() val repeatList = mutableListOf<Feed>()
groupWithFeed.feeds.forEach { groupWithFeed.feeds.forEach {
it.groupId = groupWithFeed.group.id it.groupId = groupWithFeed.group.id

View File

@ -4,10 +4,11 @@ import android.content.Context
import android.util.Log import android.util.Log
import android.util.Xml import android.util.Xml
import dagger.hilt.android.qualifiers.ApplicationContext 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.feed.Feed
import me.ash.reader.data.group.Group import me.ash.reader.data.group.Group
import me.ash.reader.data.group.GroupWithFeed import me.ash.reader.data.group.GroupWithFeed
import me.ash.reader.data.repository.StringsRepository
import org.xmlpull.v1.XmlPullParser import org.xmlpull.v1.XmlPullParser
import java.io.InputStream import java.io.InputStream
import java.util.* import java.util.*
@ -16,9 +17,18 @@ import javax.inject.Inject
class OpmlLocalDataSource @Inject constructor( class OpmlLocalDataSource @Inject constructor(
@ApplicationContext @ApplicationContext
private val context: Context, private val context: Context,
private val stringsRepository: StringsRepository,
) { ) {
// @Throws(XmlPullParserException::class, IOException::class) fun getDefaultGroupId(): String {
fun parseFileInputStream(inputStream: InputStream): List<GroupWithFeed> { 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<GroupWithFeed> {
val groupWithFeedList = mutableListOf<GroupWithFeed>() val groupWithFeedList = mutableListOf<GroupWithFeed>()
val accountId = context.currentAccountId val accountId = context.currentAccountId
inputStream.use { inputStream.use {
@ -37,15 +47,25 @@ class OpmlLocalDataSource @Inject constructor(
val title = parser.getAttributeValue(null, "title") val title = parser.getAttributeValue(null, "title")
val xmlUrl = parser.getAttributeValue(null, "xmlUrl") val xmlUrl = parser.getAttributeValue(null, "xmlUrl")
Log.i("RLog", "rss: ${title} , ${xmlUrl}") Log.i("RLog", "rss: ${title} , ${xmlUrl}")
groupWithFeedList.last().feeds.add( if (groupWithFeedList.isEmpty()) {
Feed( groupWithFeedList.add(
id = UUID.randomUUID().toString(), GroupWithFeed(
name = title, group = defaultGroup,
url = xmlUrl, feeds = mutableListOf()
groupId = UUID.randomUUID().toString(), )
accountId = accountId,
) )
) }
groupWithFeedList.last().let { groupWithFeed ->
groupWithFeed.feeds.add(
Feed(
id = UUID.randomUUID().toString(),
name = title,
url = xmlUrl,
groupId = groupWithFeed.group.id,
accountId = accountId,
)
)
}
} else { } else {
val title = parser.getAttributeValue(null, "title") val title = parser.getAttributeValue(null, "title")
Log.i("RLog", "title: ${title}") Log.i("RLog", "title: ${title}")