Skip to content

Commit

Permalink
[optimize|build] Optimize code; update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyD666 committed Nov 14, 2024
1 parent 6617411 commit 6e06023
Show file tree
Hide file tree
Showing 27 changed files with 222 additions and 187 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android {
minSdk = 24
targetSdk = 35
versionCode = 24
versionName = "2.1-beta08"
versionName = "2.1-beta09"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down Expand Up @@ -204,6 +204,7 @@ dependencies {
implementation(libs.ffmpeg.kit)

implementation(libs.coil.compose)
implementation(libs.coil.network)
implementation(libs.coil.gif)
implementation(libs.coil.svg)
implementation(libs.coil.video)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/skyd/anivu/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.skyd.anivu.ext.dataStore
import com.skyd.anivu.ext.getOrDefault
import com.skyd.anivu.model.preference.appearance.DarkModePreference
import com.skyd.anivu.model.worker.deletearticle.listenerDeleteArticleFrequency
import com.skyd.anivu.model.worker.rsssync.listenerRssSyncFrequency
import com.skyd.anivu.model.worker.rsssync.listenerRssSyncConfig
import com.skyd.anivu.util.CrashHandler
import dagger.hilt.android.HiltAndroidApp

Expand All @@ -22,7 +22,7 @@ class App : Application() {

CrashHandler.init(this)

listenerRssSyncFrequency(this)
listenerRssSyncConfig(this)
listenerDeleteArticleFrequency(this)
}
}
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/com/skyd/anivu/di/CoilModule.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.skyd.anivu.di

import android.content.Context
import coil.ImageLoader
import coil.util.DebugLogger
import coil3.ImageLoader
import coil3.network.okhttp.OkHttpNetworkFetcherFactory
import coil3.request.crossfade
import coil3.util.DebugLogger
import com.skyd.anivu.util.debug
import dagger.Module
import dagger.Provides
Expand All @@ -21,7 +23,7 @@ object CoilModule {
@ApplicationContext context: Context,
okHttpClient: OkHttpClient,
): ImageLoader = ImageLoader.Builder(context)
.okHttpClient(okHttpClient)
.components { add(OkHttpNetworkFetcherFactory(callFactory = { okHttpClient })) }
.crossfade(400)
.apply { debug { logger(DebugLogger()) } }
.build()
Expand Down
23 changes: 12 additions & 11 deletions app/src/main/java/com/skyd/anivu/ext/ContextExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import android.os.Build.VERSION.SDK_INT
import android.view.Window
import androidx.core.content.ContextCompat
import androidx.core.content.pm.PackageInfoCompat
import coil.ImageLoader
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.decode.SvgDecoder
import coil.decode.VideoFrameDecoder
import coil3.ImageLoader
import coil3.gif.AnimatedImageDecoder
import coil3.gif.GifDecoder
import coil3.network.okhttp.OkHttpNetworkFetcherFactory
import coil3.svg.SvgDecoder
import coil3.video.VideoFrameDecoder
import okhttp3.Interceptor
import okhttp3.OkHttpClient

Expand Down Expand Up @@ -128,20 +129,20 @@ fun Context.imageLoaderBuilder(): ImageLoader.Builder {
return ImageLoader.Builder(this)
.components {
if (SDK_INT >= 28) {
add(ImageDecoderDecoder.Factory())
add(AnimatedImageDecoder.Factory())
} else {
add(GifDecoder.Factory())
}
add(SvgDecoder.Factory())
add(VideoFrameDecoder.Factory())
}
.okHttpClient {
OkHttpClient.Builder()
.addNetworkInterceptor(Interceptor { chain ->
.components {
add(OkHttpNetworkFetcherFactory(callFactory = {
OkHttpClient.Builder().addNetworkInterceptor(Interceptor { chain ->
chain.proceed(chain.request()).newBuilder()
.header("Cache-Control", "max-age=31536000,public")
.build()
})
.build()
}).build()
}))
}
}
154 changes: 74 additions & 80 deletions app/src/main/java/com/skyd/anivu/ext/PointerInputScopeExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,95 +27,89 @@ suspend fun PointerInputScope.detectDoubleFingerTransformGestures(
onHorizontalDragCancel: () -> Unit = { },
onHorizontalDrag: (change: PointerInputChange, dragAmount: Float) -> Unit,
onGesture: (centroid: Offset, pan: Offset, zoom: Float, rotation: Float) -> Unit,
) {
awaitEachGesture {
var rotation = 0f
var zoom = 1f
var pan = Offset.Zero
var singlePan = Offset.Zero
var pastTouchSlop = false
val touchSlop = viewConfiguration.touchSlop
var lockedToPanZoom = false
) = awaitEachGesture {
var rotation = 0f
var zoom = 1f
var pan = Offset.Zero
var singlePan = Offset.Zero
var pastTouchSlop = false
val touchSlop = viewConfiguration.touchSlop
var lockedToPanZoom = false

var horizontalDrag = false
var verticalDrag = false
var transformDrag = false
var horizontalDrag = false
var verticalDrag = false
var transformDrag = false

val firstDown = awaitFirstDown(requireUnconsumed = false)
var canceled: Boolean
val firstDown = awaitFirstDown(requireUnconsumed = false)
var canceled: Boolean

do {
val event = awaitPointerEvent()
canceled = event.changes.fastAny { it.isConsumed }
val count: Int = if (event.changes.size > 2) {
event.changes.takeIf { it.last().id != it.first().id }?.size ?: 1
} else event.changes.size
do {
val event = awaitPointerEvent()
canceled = event.changes.fastAny { it.isConsumed }
if (canceled) continue
val count: Int = if (event.changes.size > 2) {
event.changes.takeIf { it.last().id != it.first().id }?.size ?: 1
} else event.changes.size

if (!canceled) {
val zoomChange = event.calculateZoom()
val panChange = event.calculatePan()
val rotationChange = event.calculateRotation()
if (!pastTouchSlop) {
if (count == 1) {
singlePan += panChange
val singlePanMotion = singlePan.getDistance()
if (singlePanMotion > touchSlop) {
pastTouchSlop = true
if (abs(singlePan.x) > abs(singlePan.y)) {
horizontalDrag = true
onHorizontalDragStart(firstDown.position)
} else {
verticalDrag = true
onVerticalDragStart(firstDown.position)
}
}
} else if (count > 1) {
zoom *= zoomChange
rotation += rotationChange
pan += panChange
val zoomChange = event.calculateZoom()
val panChange = event.calculatePan()
val rotationChange = event.calculateRotation()
if (!pastTouchSlop) {
if (count == 1) {
singlePan += panChange
val singlePanMotion = singlePan.getDistance()
if (singlePanMotion > touchSlop) {
pastTouchSlop = true
if (abs(singlePan.x) > abs(singlePan.y)) {
horizontalDrag = true
onHorizontalDragStart(firstDown.position)
} else {
verticalDrag = true
onVerticalDragStart(firstDown.position)
}
}
} else if (count > 1) {
zoom *= zoomChange
rotation += rotationChange
pan += panChange

val centroidSize = event.calculateCentroidSize(useCurrent = false)
val zoomMotion = abs(1 - zoom) * centroidSize
val rotationMotion = abs(rotation * PI.toFloat() * centroidSize / 180f)
val panMotion = pan.getDistance()
val centroidSize = event.calculateCentroidSize(useCurrent = false)
val zoomMotion = abs(1 - zoom) * centroidSize
val rotationMotion = abs(rotation * PI.toFloat() * centroidSize / 180f)
val panMotion = pan.getDistance()

if (zoomMotion > touchSlop ||
rotationMotion > touchSlop ||
panMotion > touchSlop
) {
transformDrag = true
pastTouchSlop = true
lockedToPanZoom = rotationMotion < touchSlop
}
}
if (zoomMotion > touchSlop || rotationMotion > touchSlop || panMotion > touchSlop) {
transformDrag = true
pastTouchSlop = true
lockedToPanZoom = rotationMotion < touchSlop
}
if (pastTouchSlop) {
if (horizontalDrag) {
onHorizontalDrag(event.changes.first(), panChange.x)
} else if (verticalDrag) {
onVerticalDrag(event.changes.first(), panChange.y)
} else if (transformDrag) {
val centroid = event.calculateCentroid(useCurrent = false)
val effectiveRotation = if (lockedToPanZoom) 0f else rotationChange
if (effectiveRotation != 0f ||
zoomChange != 1f ||
panChange != Offset.Zero
) {
onGesture(centroid, panChange, zoomChange, effectiveRotation)
}
}
event.changes.fastForEach {
if (it.positionChanged()) {
it.consume()
}
}
}
}
if (pastTouchSlop) {
if (horizontalDrag) {
onHorizontalDrag(event.changes.first(), panChange.x)
} else if (verticalDrag) {
onVerticalDrag(event.changes.first(), panChange.y)
} else if (transformDrag) {
val centroid = event.calculateCentroid(useCurrent = false)
val effectiveRotation = if (lockedToPanZoom) 0f else rotationChange
if (effectiveRotation != 0f ||
zoomChange != 1f ||
panChange != Offset.Zero
) {
onGesture(centroid, panChange, zoomChange, effectiveRotation)
}
}
event.changes.fastForEach {
if (it.positionChanged()) {
it.consume()
}
}
} while (!canceled && event.changes.fastAny { it.pressed })
if (horizontalDrag) {
if (canceled) onHorizontalDragCancel() else onHorizontalDragEnd()
} else if (verticalDrag) {
if (canceled) onVerticalDragCancel() else onVerticalDragEnd()
}
} while (!canceled && event.changes.fastAny { it.pressed })
if (horizontalDrag) {
if (canceled) onHorizontalDragCancel() else onHorizontalDragEnd()
} else if (verticalDrag) {
if (canceled) onVerticalDragCancel() else onVerticalDragEnd()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,21 @@ class ArticleRepository @Inject constructor(
val requests = mutableListOf<Deferred<Unit>>()
feedUrls.forEach { feedUrl ->
requests += async {
val articleBeanListAsync = async {
runCatching {
rssHelper.queryRssXml(
feed = feedDao.getFeed(feedUrl),
latestLink = articleDao.queryLatestByFeedUrl(feedUrl)?.link,
)?.also { feedWithArticle ->
feedDao.updateFeed(feedWithArticle.feed)
}?.articles
}.onFailure { e ->
if (e !is CancellationException) {
e.printStackTrace()
(feedUrl + "\n" + e.message).showToast()
}
}.getOrNull()
}
val articleBeanList = articleBeanListAsync.await() ?: return@async
val articleBeanList = runCatching {
rssHelper.queryRssXml(
feed = feedDao.getFeed(feedUrl),
latestLink = articleDao.queryLatestByFeedUrl(feedUrl)?.link,
)?.also { feedWithArticle ->
feedDao.updateFeed(feedWithArticle.feed)
}?.articles
}.onFailure { e ->
if (e !is CancellationException) {
e.printStackTrace()
(feedUrl + "\n" + e.message).showToast()
}
}.getOrNull()

if (articleBeanList.isEmpty()) return@async
if (articleBeanList.isNullOrEmpty()) return@async

articleDao.insertListIfNotExist(articleBeanList.map { articleWithEnclosure ->
if (articleWithEnclosure.article.feedUrl != feedUrl) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.skyd.anivu.model.repository

import coil.request.CachePolicy
import coil.request.ErrorResult
import coil.request.ImageRequest
import coil.request.SuccessResult
import coil3.request.CachePolicy
import coil3.request.ErrorResult
import coil3.request.ImageRequest
import coil3.request.SuccessResult
import com.skyd.anivu.appContext
import com.skyd.anivu.base.BaseRepository
import com.skyd.anivu.config.Const.TEMP_PICTURES_DIR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject
Expand Down Expand Up @@ -63,7 +64,7 @@ class SearchRepository @Inject constructor(
feedUrls: List<String>,
articleIds: List<String>,
): Flow<PagingData<ArticleWithFeed>> {
return searchQuery.flatMapLatest { query ->
return searchQuery.debounce(70).flatMapLatest { query ->
Pager(pagingConfig) {
articleDao.getArticlePagingSource(genSql(
tableName = ARTICLE_TABLE_NAME,
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/com/skyd/anivu/model/worker/rsssync/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private data class RssSyncConfiguration(
val requireBatteryNotLow: Boolean,
)

fun listenerRssSyncFrequency(context: Context) {
fun listenerRssSyncConfig(context: Context) {
coroutineScope.launch {
context.dataStore.data.map {
RssSyncConfiguration(
Expand All @@ -60,7 +60,7 @@ fun listenerRssSyncFrequency(context: Context) {
val requireBatteryNotLow = rssSyncConfiguration.requireBatteryNotLow

if (rssSyncFrequency == RssSyncFrequencyPreference.MANUAL) {
if (workInfo == null || !workInfo.state.isFinished) {
if (workInfo != null && !workInfo.state.isFinished) {
stopRssSyncWorker(context)
}
} else {
Expand Down Expand Up @@ -95,7 +95,7 @@ fun listenerRssSyncFrequency(context: Context) {
}
}

fun updateRssSyncWorker(
private fun updateRssSyncWorker(
context: Context,
rssSyncFrequency: Long,
requireWifi: Boolean,
Expand Down Expand Up @@ -128,7 +128,7 @@ fun updateRssSyncWorker(
)
}

fun startRssSyncWorker(
private fun startRssSyncWorker(
context: Context,
rssSyncFrequency: Long,
requireWifi: Boolean,
Expand All @@ -147,11 +147,11 @@ fun startRssSyncWorker(
)
}

fun stopRssSyncWorker(context: Context) {
private fun stopRssSyncWorker(context: Context) {
WorkManager.getInstance(context).cancelUniqueWork(RssSyncWorker.UNIQUE_WORK_NAME)
}

fun getRssSyncWorkRequest(
private fun getRssSyncWorkRequest(
rssSyncFrequency: Long,
requireWifi: Boolean,
requireCharging: Boolean,
Expand Down
Loading

0 comments on commit 6e06023

Please sign in to comment.