feat: 优化代码

pull/103/head
kunfei 5 years ago
parent 91612ff127
commit 396d747c85
  1. 15
      app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt
  2. 3
      app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt
  3. 3
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  4. 3
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  5. 3
      app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt
  6. 3
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt
  7. 3
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt

@ -5,6 +5,7 @@ import android.util.SparseArray
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import java.util.* import java.util.*
@ -122,7 +123,7 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) : Rec
} }
} }
fun setItems(items: List<ITEM>?, notify: Boolean = true) { fun setItems(items: List<ITEM>?) {
synchronized(lock) { synchronized(lock) {
if (this.items.isNotEmpty()) { if (this.items.isNotEmpty()) {
this.items.clear() this.items.clear()
@ -130,10 +131,20 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) : Rec
if (items != null) { if (items != null) {
this.items.addAll(items) this.items.addAll(items)
} }
if (notify) {
notifyDataSetChanged() notifyDataSetChanged()
} }
} }
fun setItems(items: List<ITEM>?, diffResult: DiffUtil.DiffResult) {
synchronized(lock) {
if (this.items.isNotEmpty()) {
this.items.clear()
}
if (items != null) {
this.items.addAll(items)
}
diffResult.dispatchUpdatesTo(this)
}
} }
fun setItem(position: Int, item: ITEM) { fun setItem(position: Int, item: ITEM) {

@ -82,8 +82,7 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
App.db.bookGroupDao().liveDataAll().observe(viewLifecycleOwner, Observer { App.db.bookGroupDao().liveDataAll().observe(viewLifecycleOwner, Observer {
val diffResult = val diffResult =
DiffUtil.calculateDiff(GroupDiffCallBack(ArrayList(adapter.getItems()), it)) DiffUtil.calculateDiff(GroupDiffCallBack(ArrayList(adapter.getItems()), it))
adapter.setItems(it, false) adapter.setItems(it, diffResult)
diffResult.dispatchUpdatesTo(adapter)
}) })
val itemTouchCallback = ItemTouchCallback() val itemTouchCallback = ItemTouchCallback()
itemTouchCallback.onItemTouchCallbackListener = adapter itemTouchCallback.onItemTouchCallbackListener = adapter

@ -285,8 +285,7 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
} }
refreshTime = System.currentTimeMillis() refreshTime = System.currentTimeMillis()
val diffResult = DiffUtil.calculateDiff(DiffCallBack(ArrayList(adapter.getItems()), items)) val diffResult = DiffUtil.calculateDiff(DiffCallBack(ArrayList(adapter.getItems()), items))
adapter.setItems(items, false) adapter.setItems(items, diffResult)
diffResult.dispatchUpdatesTo(adapter)
} }
/** /**

@ -136,8 +136,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
bookSourceLiveDate?.observe(this, Observer { bookSourceLiveDate?.observe(this, Observer {
val diffResult = DiffUtil val diffResult = DiffUtil
.calculateDiff(DiffCallBack(ArrayList(adapter.getItems()), it)) .calculateDiff(DiffCallBack(ArrayList(adapter.getItems()), it))
adapter.setItems(it, false) adapter.setItems(it, diffResult)
diffResult.dispatchUpdatesTo(adapter)
upCountView() upCountView()
}) })
} }

@ -106,8 +106,7 @@ class BooksFragment : BaseFragment(R.layout.fragment_books),
bookshelfLiveData?.observe(this, Observer { bookshelfLiveData?.observe(this, Observer {
val diffResult = DiffUtil val diffResult = DiffUtil
.calculateDiff(BooksDiffCallBack(ArrayList(booksAdapter.getItems()), it)) .calculateDiff(BooksDiffCallBack(ArrayList(booksAdapter.getItems()), it))
booksAdapter.setItems(it, false) booksAdapter.setItems(it, diffResult)
diffResult.dispatchUpdatesTo(booksAdapter)
}) })
} }

@ -134,8 +134,7 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
} }
val diffResult = val diffResult =
DiffUtil.calculateDiff(DiffCallBack(ArrayList(adapter.getItems()), it)) DiffUtil.calculateDiff(DiffCallBack(ArrayList(adapter.getItems()), it))
adapter.setItems(it, false) adapter.setItems(it, diffResult)
diffResult.dispatchUpdatesTo(adapter)
dataInit = true dataInit = true
upCountView() upCountView()
}) })

@ -186,8 +186,7 @@ class RssSourceActivity : VMBaseActivity<RssSourceViewModel>(R.layout.activity_r
sourceLiveData?.observe(this, Observer { sourceLiveData?.observe(this, Observer {
val diffResult = DiffUtil val diffResult = DiffUtil
.calculateDiff(DiffCallBack(adapter.getItems(), it)) .calculateDiff(DiffCallBack(adapter.getItems(), it))
adapter.setItems(it, false) adapter.setItems(it, diffResult)
diffResult.dispatchUpdatesTo(adapter)
upCountView() upCountView()
}) })
} }

Loading…
Cancel
Save