From 9cc072c7223a2df1f7919584a329c1e3fd9c0e9e Mon Sep 17 00:00:00 2001 From: ekibun Date: Tue, 17 Mar 2020 20:24:40 +0800 Subject: [PATCH] fix crash when clear history --- .../soko/ekibun/bangumi/model/HistoryModel.kt | 32 +++++++++---------- .../main/fragment/history/HistoryAdapter.kt | 2 +- .../main/fragment/history/HistoryFragment.kt | 2 +- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/soko/ekibun/bangumi/model/HistoryModel.kt b/app/src/main/java/soko/ekibun/bangumi/model/HistoryModel.kt index 68abd58..eb1767c 100644 --- a/app/src/main/java/soko/ekibun/bangumi/model/HistoryModel.kt +++ b/app/src/main/java/soko/ekibun/bangumi/model/HistoryModel.kt @@ -52,27 +52,35 @@ class HistoryModel(context: Context) { private val sp: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(context) } + val historyList by lazy { + JsonUtil.toEntity>(sp.getString(PREF_HISTORY, null) ?: "[]") ?: ArrayList() + } + /** * 添加 * @param data History */ fun addHistory(data: History) { - val newList = getHistoryList().toMutableList() val cacheKey = data.getCacheKey() - newList.removeAll { it.getCacheKey() == cacheKey } - newList.add(0, data) - sp.edit().putString(PREF_HISTORY, JsonUtil.toJson(newList.sortedByDescending { it.timestamp })).apply() + historyList.removeAll { it.getCacheKey() == cacheKey } + historyList.add(0, data) + historyList.sortByDescending { it.timestamp } + save() + } + + private fun save() { + sp.edit().putString(PREF_HISTORY, JsonUtil.toJson(historyList.subList(0, Math.min(historyList.size, 500)))) + .apply() } /** * 删除 - * @param searchKey String + * @param data String * @return Boolean */ fun removeHistory(data: History): Boolean { - val newList = getHistoryList().toMutableList() - val removed = newList.removeAll { it.timestamp == data.timestamp } - sp.edit().putString(PREF_HISTORY, JsonUtil.toJson(newList)).apply() + val removed = historyList.removeAll { it.timestamp == data.timestamp } + save() return removed } @@ -83,14 +91,6 @@ class HistoryModel(context: Context) { sp.edit().putString(PREF_HISTORY, "[]").apply() } - /** - * 获取列表 - * @return List - */ - fun getHistoryList(): List { - return JsonUtil.toEntity>(sp.getString(PREF_HISTORY, null) ?: "[]") ?: ArrayList() - } - companion object { const val PREF_HISTORY = "history" } diff --git a/app/src/main/java/soko/ekibun/bangumi/ui/main/fragment/history/HistoryAdapter.kt b/app/src/main/java/soko/ekibun/bangumi/ui/main/fragment/history/HistoryAdapter.kt index abb9c62..22b5fdd 100644 --- a/app/src/main/java/soko/ekibun/bangumi/ui/main/fragment/history/HistoryAdapter.kt +++ b/app/src/main/java/soko/ekibun/bangumi/ui/main/fragment/history/HistoryAdapter.kt @@ -30,7 +30,7 @@ class HistoryAdapter(data: MutableList? = null) : fun setUpWithRecyclerView(container: StickyHeadContainer, recyclerView: androidx.recyclerview.widget.RecyclerView) { bindToRecyclerView(recyclerView) container.setDataCallback { - container.item_header.text = data[it].header + container.item_header.text = data.getOrNull(it)?.header } recyclerView.addItemDecoration(StickyItemDecoration(container, SECTION_HEADER_VIEW)) } diff --git a/app/src/main/java/soko/ekibun/bangumi/ui/main/fragment/history/HistoryFragment.kt b/app/src/main/java/soko/ekibun/bangumi/ui/main/fragment/history/HistoryFragment.kt index f40d8bc..af37feb 100644 --- a/app/src/main/java/soko/ekibun/bangumi/ui/main/fragment/history/HistoryFragment.kt +++ b/app/src/main/java/soko/ekibun/bangumi/ui/main/fragment/history/HistoryFragment.kt @@ -50,7 +50,7 @@ class HistoryFragment : DrawerFragment(R.layout.content_history) { item_swipe?.isRefreshing = false val history = ArrayList() var dateString = "" - App.get(context ?: return).historyModel.getHistoryList().forEach { + App.get(context ?: return).historyModel.historyList.forEach { if (dateString != it.dateString) history.add(HistoryAdapter.History(it.dateString)) history.add(HistoryAdapter.History(it)) dateString = it.dateString