Skip to content

Commit

Permalink
fix crash when clear history
Browse files Browse the repository at this point in the history
  • Loading branch information
ekibun committed Mar 17, 2020
1 parent 8655cae commit 9cc072c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
32 changes: 16 additions & 16 deletions app/src/main/java/soko/ekibun/bangumi/model/HistoryModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,35 @@ class HistoryModel(context: Context) {

private val sp: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(context) }

val historyList by lazy {
JsonUtil.toEntity<ArrayList<History>>(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
}

Expand All @@ -83,14 +91,6 @@ class HistoryModel(context: Context) {
sp.edit().putString(PREF_HISTORY, "[]").apply()
}

/**
* 获取列表
* @return List<String>
*/
fun getHistoryList(): List<History> {
return JsonUtil.toEntity<List<History>>(sp.getString(PREF_HISTORY, null) ?: "[]") ?: ArrayList()
}

companion object {
const val PREF_HISTORY = "history"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class HistoryAdapter(data: MutableList<History>? = 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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class HistoryFragment : DrawerFragment(R.layout.content_history) {
item_swipe?.isRefreshing = false
val history = ArrayList<HistoryAdapter.History>()
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
Expand Down

0 comments on commit 9cc072c

Please sign in to comment.