feat: 优化代码

pull/103/head
kunfei 5 years ago
parent 91612ff127
commit 396d747c85
  1. 17
      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.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
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) {
if (this.items.isNotEmpty()) {
this.items.clear()
@ -130,9 +131,19 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) : Rec
if (items != null) {
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)
}
}

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

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

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

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

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

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

Loading…
Cancel
Save