From bac07abf1f57051f5b2eda03a86740b06ed9b386 Mon Sep 17 00:00:00 2001 From: tarsin Date: Sun, 30 Apr 2023 12:56:14 +0800 Subject: [PATCH] Kill global map --- .../java/com/hippo/ehviewer/EhApplication.kt | 16 ----------- .../hippo/ehviewer/ui/scene/FavoritesScene.kt | 4 +-- .../ehviewer/ui/scene/GalleryListScene.kt | 4 +-- .../widget/GalleryInfoContentHelper.kt | 16 ++++------- .../java/com/hippo/util/ParcelableCompat.kt | 1 + .../java/com/hippo/widget/ContentLayout.kt | 27 ++++--------------- 6 files changed, 15 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/EhApplication.kt b/app/src/main/java/com/hippo/ehviewer/EhApplication.kt index 54910749e..f20294fdf 100644 --- a/app/src/main/java/com/hippo/ehviewer/EhApplication.kt +++ b/app/src/main/java/com/hippo/ehviewer/EhApplication.kt @@ -42,7 +42,6 @@ import com.hippo.ehviewer.ui.EhActivity import com.hippo.ehviewer.ui.keepNoMediaFileStatus import com.hippo.util.ReadableTime import com.hippo.yorozuya.FileUtils -import com.hippo.yorozuya.IntIdGenerator import eu.kanade.tachiyomi.core.preference.AndroidPreferenceStore import eu.kanade.tachiyomi.ui.reader.setting.ReaderPreferences import eu.kanade.tachiyomi.util.lang.launchIO @@ -59,8 +58,6 @@ import javax.net.ssl.TrustManagerFactory import javax.net.ssl.X509TrustManager class EhApplication : Application(), DefaultLifecycleObserver, ImageLoaderFactory { - private val mIdGenerator = IntIdGenerator() - private val mGlobalStuffMap = HashMap() private val mActivityList = ArrayList() val topActivity: EhActivity? get() = if (mActivityList.isNotEmpty()) { @@ -113,7 +110,6 @@ class EhApplication : Application(), DefaultLifecycleObserver, ImageLoaderFactor } } cleanObsoleteCache(this) - mIdGenerator.setNextId(Settings.getInt(KEY_GLOBAL_STUFF_NEXT_ID, 0)) } private suspend fun cleanupDownload() { @@ -147,17 +143,6 @@ class EhApplication : Application(), DefaultLifecycleObserver, ImageLoaderFactor } } - fun putGlobalStuff(o: Any): Int { - val id = mIdGenerator.nextId() - mGlobalStuffMap[id] = o - Settings.putInt(KEY_GLOBAL_STUFF_NEXT_ID, mIdGenerator.nextId()) - return id - } - - fun removeGlobalStuff(id: Int): Any? { - return mGlobalStuffMap.remove(id) - } - fun registerActivity(activity: Activity) { mActivityList.add(activity) } @@ -185,7 +170,6 @@ class EhApplication : Application(), DefaultLifecycleObserver, ImageLoaderFactor } companion object { - private const val KEY_GLOBAL_STUFF_NEXT_ID = "global_stuff_next_id" var locked = true var locked_last_leave_time: Long = 0 diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt index bf1d8f370..1d5a8005c 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt @@ -979,8 +979,8 @@ class FavoritesScene : showSearchBar() } - override fun isDuplicate(d1: GalleryInfo?, d2: GalleryInfo?): Boolean { - return d1?.gid == d2?.gid && d1 != null && d2 != null + override fun isDuplicate(d1: GalleryInfo, d2: GalleryInfo): Boolean { + return d1.gid == d2.gid } override fun onScrollToPosition(position: Int) { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.kt index 0bca16df3..2905b4842 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.kt @@ -1338,8 +1338,8 @@ class GalleryListScene : showActionFab() } - override fun isDuplicate(d1: GalleryInfo?, d2: GalleryInfo?): Boolean { - return d1?.gid == d2?.gid && d1 != null && d2 != null + override fun isDuplicate(d1: GalleryInfo, d2: GalleryInfo): Boolean { + return d1.gid == d2.gid } override fun onScrollToPosition(position: Int) { diff --git a/app/src/main/java/com/hippo/ehviewer/widget/GalleryInfoContentHelper.kt b/app/src/main/java/com/hippo/ehviewer/widget/GalleryInfoContentHelper.kt index f480a0fef..7d47d279f 100644 --- a/app/src/main/java/com/hippo/ehviewer/widget/GalleryInfoContentHelper.kt +++ b/app/src/main/java/com/hippo/ehviewer/widget/GalleryInfoContentHelper.kt @@ -28,7 +28,7 @@ import java.time.ZoneOffset import java.time.format.DateTimeFormatter import java.util.Locale -abstract class GalleryInfoContentHelper : ContentHelper() { +abstract class GalleryInfoContentHelper : ContentHelper() { @JvmField var jumpTo: String? = null private val listener: FavouriteStatusRouter.Listener @@ -50,18 +50,12 @@ abstract class GalleryInfoContentHelper : ContentHelper() { EhApplication.favouriteStatusRouter.removeListener(listener) } - override fun onAddData(data: List) { - for (info in data) { - info?.let { - map[info.gid] = info - } - } + override fun onAddData(data: List) { + data.forEach { map[it.gid] = it } } - override fun onRemoveData(data: List) { - for (info in data) { - info?.let { map.remove(info.gid) } - } + override fun onRemoveData(data: List) { + data.forEach { map.remove(it.gid) } } override fun onClearData() { diff --git a/app/src/main/java/com/hippo/util/ParcelableCompat.kt b/app/src/main/java/com/hippo/util/ParcelableCompat.kt index fe57fecaf..6f9b24cd9 100644 --- a/app/src/main/java/com/hippo/util/ParcelableCompat.kt +++ b/app/src/main/java/com/hippo/util/ParcelableCompat.kt @@ -7,4 +7,5 @@ import androidx.core.content.IntentCompat import androidx.core.os.BundleCompat inline fun Bundle.getParcelableCompat(key: String?): T? = BundleCompat.getParcelable(this, key, T::class.java) +inline fun Bundle.getParcelableArrayListCompat(key: String?): ArrayList? = BundleCompat.getParcelableArrayList(this, key, T::class.java) inline fun Intent.getParcelableExtraCompat(key: String?): T? = IntentCompat.getParcelableExtra(this, key, T::class.java) diff --git a/app/src/main/java/com/hippo/widget/ContentLayout.kt b/app/src/main/java/com/hippo/widget/ContentLayout.kt index 9aacf7230..51455dd64 100644 --- a/app/src/main/java/com/hippo/widget/ContentLayout.kt +++ b/app/src/main/java/com/hippo/widget/ContentLayout.kt @@ -30,7 +30,6 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener import com.hippo.easyrecyclerview.HandlerDrawable import com.hippo.easyrecyclerview.LayoutManagerUtils import com.hippo.easyrecyclerview.LayoutManagerUtils.OnScrollToPositionListener -import com.hippo.ehviewer.EhApplication import com.hippo.ehviewer.R import com.hippo.ehviewer.databinding.WidgetContentLayoutBinding import com.hippo.util.ExceptionUtils @@ -130,7 +129,7 @@ class ContentLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout super.onRestoreInstanceState(mContentHelper.restoreInstanceState(state)) } - abstract class ContentHelper : OnShowViewListener { + abstract class ContentHelper : OnShowViewListener { /** * Generate task id */ @@ -236,7 +235,6 @@ class ContentLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout } } } - private var mSavedDataId = IntIdGenerator.INVALID_ID fun init(contentLayout: ContentLayout) { binding = contentLayout.binding mNextPageScrollSize = LayoutUtils.dp2pix(contentLayout.context, 48f) @@ -733,15 +731,7 @@ class ContentLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout val shownView = mViewTransition.shownViewIndex bundle.putInt(KEY_SHOWN_VIEW, shownView) bundle.putString(KEY_TIP, binding.tip.text.toString()) - - // TODO It's a bad design - val app = EhApplication.application - if (mSavedDataId != IntIdGenerator.INVALID_ID) { - app.removeGlobalStuff(mSavedDataId) - mSavedDataId = IntIdGenerator.INVALID_ID - } - mSavedDataId = app.putGlobalStuff(mData) - bundle.putInt(KEY_DATA, mSavedDataId) + bundle.putParcelableArrayList(KEY_DATA, mData) bundle.putInt(KEY_NEXT_ID, mIdGenerator.nextId()) bundle.putParcelable(KEY_PAGE_DIVIDER, mPageDivider) bundle.putInt(KEY_START_PAGE, mStartPage) @@ -756,16 +746,9 @@ class ContentLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout return if (state is Bundle) { mViewTransition.showView(state.getInt(KEY_SHOWN_VIEW), false) binding.tip.text = state.getString(KEY_TIP) - mSavedDataId = state.getInt(KEY_DATA) - var newData: ArrayList? = null - val app = EhApplication.application - if (mSavedDataId != IntIdGenerator.INVALID_ID) { - newData = app.removeGlobalStuff(mSavedDataId) as ArrayList? - mSavedDataId = IntIdGenerator.INVALID_ID - if (newData != null) { - mData = newData - } - } + @Suppress("DEPRECATION") + val newData = state.getParcelableArrayList(KEY_DATA) + newData?.let { mData = it } mIdGenerator.setNextId(state.getInt(KEY_NEXT_ID)) mPageDivider = state.getParcelableCompat(KEY_PAGE_DIVIDER)!! mStartPage = state.getInt(KEY_START_PAGE)