From f137dc9abcc9192d4500c67c9c6c84391a1f55c9 Mon Sep 17 00:00:00 2001 From: Ash Date: Fri, 6 May 2022 23:07:18 +0800 Subject: [PATCH 1/2] Switch to Rome --- app/build.gradle | 5 +-- app/proguard-rules.pro | 5 ++- app/src/main/java/me/ash/reader/App.kt | 4 -- .../ash/reader/data/module/RetrofitModule.kt | 6 --- .../data/repository/AbstractRssRepository.kt | 2 - .../data/repository/LocalRssRepository.kt | 5 +-- .../ash/reader/data/repository/RssHelper.kt | 44 +++++++++++-------- .../reader/data/source/FeverApiDataSource.kt | 3 -- .../data/source/GoogleReaderApiDataSource.kt | 3 -- .../data/source/RssNetworkDataSource.kt | 30 ------------- .../me/ash/reader/ui/component/WebView.kt | 4 +- .../reader/ui/page/home/flow/ArticleItem.kt | 2 +- .../me/ash/reader/ui/page/home/read/Header.kt | 12 ++--- .../reader/ui/page/home/read/ReadViewModel.kt | 6 +-- build.gradle | 2 +- 15 files changed, 46 insertions(+), 87 deletions(-) delete mode 100644 app/src/main/java/me/ash/reader/data/source/RssNetworkDataSource.kt diff --git a/app/build.gradle b/app/build.gradle index d93c138d2..9c2e9c382 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -85,9 +85,8 @@ dependencies { // https://github.com/dankito/Readability4J implementation "net.dankito.readability4j:readability4j:$readability4j" - // https://github.com/muhrifqii/ParseRSS/releases - implementation "com.github.muhrifqii.ParseRSS:parserss:$parseRSS" - implementation "com.github.muhrifqii.ParseRSS:retrofit:$parseRSS" + // https://mvnrepository.com/artifact/com.rometools/rome + implementation "com.rometools:rome:$rome" // https://coil-kt.github.io/coil/changelog/ implementation("io.coil-kt:coil-compose:$coil") diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index c6cfd0957..6fff2b024 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -29,4 +29,7 @@ # ksoap2 XmlPullParser confusion -dontwarn org.xmlpull.v1.XmlPullParser -dontwarn org.xmlpull.v1.XmlSerializer --keep class org.xmlpull.v1.* {*;} \ No newline at end of file +-keep class org.xmlpull.v1.* {*;} + +# Rome +-keep class com.rometools.** { *; } \ No newline at end of file diff --git a/app/src/main/java/me/ash/reader/App.kt b/app/src/main/java/me/ash/reader/App.kt index 7e5a52253..ef073f2c4 100644 --- a/app/src/main/java/me/ash/reader/App.kt +++ b/app/src/main/java/me/ash/reader/App.kt @@ -14,7 +14,6 @@ import me.ash.reader.data.repository.* import me.ash.reader.data.source.AppNetworkDataSource import me.ash.reader.data.source.OpmlLocalDataSource import me.ash.reader.data.source.ReaderDatabase -import me.ash.reader.data.source.RssNetworkDataSource import me.ash.reader.ui.ext.* import javax.inject.Inject @@ -35,9 +34,6 @@ class App : Application(), Configuration.Provider { @Inject lateinit var opmlLocalDataSource: OpmlLocalDataSource - @Inject - lateinit var rssNetworkDataSource: RssNetworkDataSource - @Inject lateinit var rssHelper: RssHelper diff --git a/app/src/main/java/me/ash/reader/data/module/RetrofitModule.kt b/app/src/main/java/me/ash/reader/data/module/RetrofitModule.kt index 7ae99d9ff..2423b02fd 100644 --- a/app/src/main/java/me/ash/reader/data/module/RetrofitModule.kt +++ b/app/src/main/java/me/ash/reader/data/module/RetrofitModule.kt @@ -7,7 +7,6 @@ import dagger.hilt.components.SingletonComponent import me.ash.reader.data.source.AppNetworkDataSource import me.ash.reader.data.source.FeverApiDataSource import me.ash.reader.data.source.GoogleReaderApiDataSource -import me.ash.reader.data.source.RssNetworkDataSource import javax.inject.Singleton @Module @@ -19,11 +18,6 @@ object RetrofitModule { fun provideAppNetworkDataSource(): AppNetworkDataSource = AppNetworkDataSource.getInstance() - @Provides - @Singleton - fun provideRssNetworkDataSource(): RssNetworkDataSource = - RssNetworkDataSource.getInstance() - @Provides @Singleton fun provideFeverApiDataSource(): FeverApiDataSource = diff --git a/app/src/main/java/me/ash/reader/data/repository/AbstractRssRepository.kt b/app/src/main/java/me/ash/reader/data/repository/AbstractRssRepository.kt index f91bb8c17..71eb82d2a 100644 --- a/app/src/main/java/me/ash/reader/data/repository/AbstractRssRepository.kt +++ b/app/src/main/java/me/ash/reader/data/repository/AbstractRssRepository.kt @@ -15,7 +15,6 @@ import me.ash.reader.data.dao.ArticleDao import me.ash.reader.data.dao.FeedDao import me.ash.reader.data.dao.GroupDao import me.ash.reader.data.entity.* -import me.ash.reader.data.source.RssNetworkDataSource import me.ash.reader.ui.ext.currentAccountId import java.util.* import java.util.concurrent.TimeUnit @@ -26,7 +25,6 @@ abstract class AbstractRssRepository constructor( private val articleDao: ArticleDao, private val groupDao: GroupDao, private val feedDao: FeedDao, - private val rssNetworkDataSource: RssNetworkDataSource, private val workManager: WorkManager, private val dispatcherIO: CoroutineDispatcher, ) { diff --git a/app/src/main/java/me/ash/reader/data/repository/LocalRssRepository.kt b/app/src/main/java/me/ash/reader/data/repository/LocalRssRepository.kt index d0e2aeed2..d101a190e 100644 --- a/app/src/main/java/me/ash/reader/data/repository/LocalRssRepository.kt +++ b/app/src/main/java/me/ash/reader/data/repository/LocalRssRepository.kt @@ -28,7 +28,6 @@ import me.ash.reader.data.entity.Group import me.ash.reader.data.module.DispatcherDefault import me.ash.reader.data.module.DispatcherIO import me.ash.reader.data.repository.SyncWorker.Companion.setIsSyncing -import me.ash.reader.data.source.RssNetworkDataSource import me.ash.reader.ui.ext.currentAccountId import me.ash.reader.ui.ext.spacerDollar import me.ash.reader.ui.page.common.ExtraName @@ -42,7 +41,6 @@ class LocalRssRepository @Inject constructor( private val articleDao: ArticleDao, private val feedDao: FeedDao, private val rssHelper: RssHelper, - private val rssNetworkDataSource: RssNetworkDataSource, private val accountDao: AccountDao, private val groupDao: GroupDao, @DispatcherDefault @@ -52,8 +50,7 @@ class LocalRssRepository @Inject constructor( workManager: WorkManager, ) : AbstractRssRepository( context, accountDao, articleDao, groupDao, - feedDao, rssNetworkDataSource, workManager, - dispatcherIO + feedDao, workManager, dispatcherIO ) { private val notificationManager: NotificationManagerCompat = NotificationManagerCompat.from(context).apply { diff --git a/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt b/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt index 9fdfae911..b611959c4 100644 --- a/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt +++ b/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt @@ -3,6 +3,9 @@ package me.ash.reader.data.repository import android.content.Context import android.text.Html import android.util.Log +import com.rometools.rome.feed.synd.SyndFeed +import com.rometools.rome.io.SyndFeedInput +import com.rometools.rome.io.XmlReader import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.withContext @@ -11,13 +14,13 @@ import me.ash.reader.data.entity.Article import me.ash.reader.data.entity.Feed import me.ash.reader.data.entity.FeedWithArticle import me.ash.reader.data.module.DispatcherIO -import me.ash.reader.data.source.RssNetworkDataSource import me.ash.reader.ui.ext.currentAccountId import me.ash.reader.ui.ext.spacerDollar import net.dankito.readability4j.Readability4J import net.dankito.readability4j.extended.Readability4JExtended import okhttp3.OkHttpClient import okhttp3.Request +import java.net.URL import java.text.ParsePosition import java.text.SimpleDateFormat import java.util.* @@ -26,7 +29,6 @@ import javax.inject.Inject class RssHelper @Inject constructor( @ApplicationContext private val context: Context, - private val rssNetworkDataSource: RssNetworkDataSource, @DispatcherIO private val dispatcherIO: CoroutineDispatcher, ) { @@ -34,7 +36,7 @@ class RssHelper @Inject constructor( suspend fun searchFeed(feedLink: String): FeedWithArticle { return withContext(dispatcherIO) { val accountId = context.currentAccountId - val parseRss = rssNetworkDataSource.parseRss(feedLink) + val parseRss: SyndFeed = SyndFeedInput().build(XmlReader(URL(feedLink))) val feed = Feed( id = accountId.spacerDollar(UUID.randomUUID().toString()), name = parseRss.title!!, @@ -83,28 +85,34 @@ class RssHelper @Inject constructor( return withContext(dispatcherIO) { val a = mutableListOf
() val accountId = context.currentAccountId - val parseRss = rssNetworkDataSource.parseRss(feed.url) - parseRss.items.forEach { + val parseRss: SyndFeed = SyndFeedInput().build(XmlReader(URL(feed.url))) + parseRss.entries.forEach { if (latestLink != null && latestLink == it.link) return@withContext a - Log.i("RLog", "request rss:\n${feed.name},${feed.url}\n${it.title}\n${it.link}\n") + val desc = it.description?.value + val content = it.contents?.mapNotNull { it.value }?.joinToString("\n") + Log.i( + "RLog", + "request rss:\n" + + "name: ${feed.name}\n" + + "url: ${feed.url}\n" + + "title: ${it.title}\n" + + "desc: ${it.description?.value}\n" + + "content: ${content}\n" + ) a.add( Article( id = accountId.spacerDollar(UUID.randomUUID().toString()), accountId = accountId, feedId = feed.id, - date = (it.publishDate ?: it.lastUpdated).toString().let { - try { - Date(it) - } catch (e: IllegalArgumentException) { - parseDate(it) ?: Date() - } - }, + date = it.publishedDate ?: it.updatedDate ?: Date(), title = Html.fromHtml(it.title.toString()).toString(), - author = it.author?.name, - rawDescription = it.description ?: it.summary ?: "", - shortDescription = - (Readability4JExtended("", it.description ?: it.summary ?: "") - .parse().textContent ?: "").take(100).trim(), + author = it.author, + rawDescription = (desc ?: content) ?: "", + shortDescription = (Readability4JExtended("", desc ?: content ?: "") + .parse().textContent ?: "") + .take(100) + .trim(), + fullContent = content, link = it.link ?: "", ) ) diff --git a/app/src/main/java/me/ash/reader/data/source/FeverApiDataSource.kt b/app/src/main/java/me/ash/reader/data/source/FeverApiDataSource.kt index 9a5b58720..616ae7cd5 100644 --- a/app/src/main/java/me/ash/reader/data/source/FeverApiDataSource.kt +++ b/app/src/main/java/me/ash/reader/data/source/FeverApiDataSource.kt @@ -1,9 +1,7 @@ package me.ash.reader.data.source -import com.github.muhrifqii.parserss.ParseRSS import okhttp3.RequestBody import okhttp3.RequestBody.Companion.toRequestBody -import org.xmlpull.v1.XmlPullParserFactory import retrofit2.Call import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory @@ -48,7 +46,6 @@ interface FeverApiDataSource { fun getInstance(): FeverApiDataSource { return instance ?: synchronized(this) { - ParseRSS.init(XmlPullParserFactory.newInstance()) instance ?: Retrofit.Builder() .baseUrl("http://10.0.2.2/api/") .addConverterFactory(GsonConverterFactory.create()) diff --git a/app/src/main/java/me/ash/reader/data/source/GoogleReaderApiDataSource.kt b/app/src/main/java/me/ash/reader/data/source/GoogleReaderApiDataSource.kt index e86932ed7..929532259 100644 --- a/app/src/main/java/me/ash/reader/data/source/GoogleReaderApiDataSource.kt +++ b/app/src/main/java/me/ash/reader/data/source/GoogleReaderApiDataSource.kt @@ -1,7 +1,5 @@ package me.ash.reader.data.source -import com.github.muhrifqii.parserss.ParseRSS -import org.xmlpull.v1.XmlPullParserFactory import retrofit2.Call import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory @@ -33,7 +31,6 @@ interface GoogleReaderApiDataSource { fun getInstance(): GoogleReaderApiDataSource { return instance ?: synchronized(this) { - ParseRSS.init(XmlPullParserFactory.newInstance()) instance ?: Retrofit.Builder() .baseUrl("http://10.0.2.2/api/greader.php/") .addConverterFactory(GsonConverterFactory.create()) diff --git a/app/src/main/java/me/ash/reader/data/source/RssNetworkDataSource.kt b/app/src/main/java/me/ash/reader/data/source/RssNetworkDataSource.kt deleted file mode 100644 index 94189d854..000000000 --- a/app/src/main/java/me/ash/reader/data/source/RssNetworkDataSource.kt +++ /dev/null @@ -1,30 +0,0 @@ -package me.ash.reader.data.source - -import com.github.muhrifqii.parserss.ParseRSS -import com.github.muhrifqii.parserss.RSSFeedObject -import com.github.muhrifqii.parserss.retrofit.ParseRSSConverterFactory -import org.xmlpull.v1.XmlPullParserFactory -import retrofit2.Retrofit -import retrofit2.http.GET -import retrofit2.http.Url - -interface RssNetworkDataSource { - @GET - suspend fun parseRss(@Url url: String): RSSFeedObject - - companion object { - private var instance: RssNetworkDataSource? = null - - fun getInstance(): RssNetworkDataSource { - return instance ?: synchronized(this) { - ParseRSS.init(XmlPullParserFactory.newInstance()) - instance ?: Retrofit.Builder() - .baseUrl("https://api.feeddd.org/feeds/") - .addConverterFactory(ParseRSSConverterFactory.create()) - .build().create(RssNetworkDataSource::class.java).also { - instance = it - } - } - } - } -} \ No newline at end of file diff --git a/app/src/main/java/me/ash/reader/ui/component/WebView.kt b/app/src/main/java/me/ash/reader/ui/component/WebView.kt index f19e9b7ae..dc4a8acda 100644 --- a/app/src/main/java/me/ash/reader/ui/component/WebView.kt +++ b/app/src/main/java/me/ash/reader/ui/component/WebView.kt @@ -127,6 +127,7 @@ fun WebView( update = { it.apply { Log.i("RLog", "CustomWebView: ${content}") + settings.javaScriptEnabled = true loadDataWithBaseURL( null, getStyle(color) + content, @@ -158,10 +159,9 @@ fun getStyle(argb: Int): String = """ padding: 0 24px; } -img, video { +img, video, iframe { margin: 0 -24px 20px; width: calc(100% + 48px); - height: auto; border-top: 1px solid ${argbToCssColor(argb)}08; border-bottom: 1px solid ${argbToCssColor(argb)}08; } diff --git a/app/src/main/java/me/ash/reader/ui/page/home/flow/ArticleItem.kt b/app/src/main/java/me/ash/reader/ui/page/home/flow/ArticleItem.kt index ed74bf948..497e25115 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/flow/ArticleItem.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/flow/ArticleItem.kt @@ -121,7 +121,7 @@ fun ArticleItem( overflow = TextOverflow.Ellipsis, ) // Description - if (articleListDesc.value) { + if (articleListDesc.value && articleWithFeed.article.shortDescription.isNotBlank()) { Text( text = articleWithFeed.article.shortDescription, color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f), diff --git a/app/src/main/java/me/ash/reader/ui/page/home/read/Header.kt b/app/src/main/java/me/ash/reader/ui/page/home/read/Header.kt index 7838f8b63..44cccb25c 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/read/Header.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/read/Header.kt @@ -46,11 +46,13 @@ fun Header( ) Spacer(modifier = Modifier.height(4.dp)) articleWithFeed.article.author?.let { - Text( - text = articleWithFeed.article.author, - color = MaterialTheme.colorScheme.outline.copy(alpha = 0.7f), - style = MaterialTheme.typography.labelMedium, - ) + if (it.isNotEmpty()) { + Text( + text = it, + color = MaterialTheme.colorScheme.outline.copy(alpha = 0.7f), + style = MaterialTheme.typography.labelMedium, + ) + } } Text( text = articleWithFeed.feed.name, diff --git a/app/src/main/java/me/ash/reader/ui/page/home/read/ReadViewModel.kt b/app/src/main/java/me/ash/reader/ui/page/home/read/ReadViewModel.kt index 674fc2b88..5b92fe71d 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/read/ReadViewModel.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/read/ReadViewModel.kt @@ -54,10 +54,8 @@ class ReadViewModel @Inject constructor( private fun renderDescriptionContent() { _viewState.update { it.copy( - content = rssHelper.parseDescriptionContent( - link = it.articleWithFeed?.article?.link ?: "", - content = it.articleWithFeed?.article?.rawDescription ?: "", - ) + content = it.articleWithFeed?.article?.fullContent + ?: it.articleWithFeed?.article?.rawDescription ?: "", ) } } diff --git a/build.gradle b/build.gradle index e4148b354..02bfb71e0 100644 --- a/build.gradle +++ b/build.gradle @@ -12,7 +12,7 @@ buildscript { profileinstaller = '1.2.0-alpha02' retrofit2 = '2.9.0' coil = '2.0.0-rc03' - parseRSS = '0.6.0' + rome = '1.18.0' readability4j = '1.0.8' opmlParser = '2.2.0' androidSVG = '1.4' From 3c4ec48e154542809ff49a263abf9c970a00241f Mon Sep 17 00:00:00 2001 From: Ash Date: Sat, 7 May 2022 00:24:50 +0800 Subject: [PATCH 2/2] Fix some styles --- .../me/ash/reader/data/repository/RssHelper.kt | 6 ++++-- .../java/me/ash/reader/ui/component/WebView.kt | 15 +++++++-------- .../me/ash/reader/ui/page/home/feeds/FeedsPage.kt | 2 +- .../me/ash/reader/ui/page/home/flow/FlowPage.kt | 1 + app/src/main/res/values-zh-rCN/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt b/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt index b611959c4..078bd05e1 100644 --- a/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt +++ b/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt @@ -89,14 +89,16 @@ class RssHelper @Inject constructor( parseRss.entries.forEach { if (latestLink != null && latestLink == it.link) return@withContext a val desc = it.description?.value - val content = it.contents?.mapNotNull { it.value }?.joinToString("\n") + val content = it.contents + .takeIf { it.isNotEmpty() } + ?.let { it.joinToString("\n") { it.value } } Log.i( "RLog", "request rss:\n" + "name: ${feed.name}\n" + "url: ${feed.url}\n" + "title: ${it.title}\n" + - "desc: ${it.description?.value}\n" + + "desc: ${desc}\n" + "content: ${content}\n" ) a.add( diff --git a/app/src/main/java/me/ash/reader/ui/component/WebView.kt b/app/src/main/java/me/ash/reader/ui/component/WebView.kt index dc4a8acda..ea2634aeb 100644 --- a/app/src/main/java/me/ash/reader/ui/component/WebView.kt +++ b/app/src/main/java/me/ash/reader/ui/component/WebView.kt @@ -6,10 +6,7 @@ import android.net.http.SslError import android.util.Log import android.webkit.* import androidx.compose.material3.MaterialTheme -import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember +import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.platform.LocalContext @@ -122,7 +119,7 @@ fun WebView( // } AndroidView( - modifier = modifier, + modifier = modifier,//.padding(horizontal = if (content.contains("class=\"page\"")) 0.dp else 24.dp), factory = { webView }, update = { it.apply { @@ -139,10 +136,12 @@ fun WebView( ) } +@Stable fun argbToCssColor(argb: Int): String = String.format("#%06X", 0xFFFFFF and argb) +@Stable fun getStyle(argb: Int): String = """ - + """ \ No newline at end of file diff --git a/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsPage.kt b/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsPage.kt index 4ceb989ad..4b223273a 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsPage.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsPage.kt @@ -190,7 +190,7 @@ fun FeedsPage( item { Banner( title = filterState.filter.getName(), - desc = feedsViewState.importantCount, + desc = feedsViewState.importantCount.ifEmpty { stringResource(R.string.loading) }, icon = filterState.filter.iconOutline, action = { Icon( diff --git a/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowPage.kt b/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowPage.kt index af9094c5a..c69b0dd50 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowPage.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowPage.kt @@ -273,6 +273,7 @@ fun FlowPage( if (pagingItems.loadState.source.refresh is LoadState.NotLoading && pagingItems.itemCount != 0) { Spacer(modifier = Modifier.height(64.dp)) } + Spacer(modifier = Modifier.windowInsetsBottomHeight(WindowInsets.navigationBars)) } } } diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index e84b160fe..960235055 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -8,6 +8,7 @@ %1$d 项已加星标 分组 正在同步… + 加载中… 收缩 展开 确认 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0c4318477..ec332f40c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -8,6 +8,7 @@ %1$d Starred Items Feeds Syncing… + Loading… Expand Less Expand More Confirm