Merge pull request #368 from 1552980358/master

Modify code style of package `io.legado.app.base` and `io.legado.app.constant`
pull/370/head
kunfei 4 years ago committed by GitHub
commit 6430673f94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      app/src/main/java/io/legado/app/base/BaseActivity.kt
  2. 4
      app/src/main/java/io/legado/app/base/BaseDialogFragment.kt
  3. 8
      app/src/main/java/io/legado/app/base/BaseService.kt
  4. 97
      app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt
  5. 23
      app/src/main/java/io/legado/app/base/adapter/ItemAnimation.kt
  6. 19
      app/src/main/java/io/legado/app/constant/Theme.kt

@ -108,9 +108,7 @@ abstract class BaseActivity(
return true
}
open fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
return super.onCreateOptionsMenu(menu)
}
open fun onCompatCreateOptionsMenu(menu: Menu) = super.onCreateOptionsMenu(menu)
final override fun onOptionsItemSelected(item: MenuItem?): Boolean {
item?.let {
@ -122,9 +120,7 @@ abstract class BaseActivity(
return item != null && onCompatOptionsItemSelected(item)
}
open fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
return super.onOptionsItemSelected(item)
}
open fun onCompatOptionsItemSelected(item: MenuItem) = super.onOptionsItemSelected(item)
private fun initTheme() {
when (theme) {

@ -51,9 +51,7 @@ abstract class BaseDialogFragment : DialogFragment(), CoroutineScope {
scope: CoroutineScope = this,
context: CoroutineContext = Dispatchers.IO,
block: suspend CoroutineScope.() -> T
): Coroutine<T> {
return Coroutine.async(scope, context) { block() }
}
) = Coroutine.async(scope, context) { block() }
open fun observeLiveBus() {
}

@ -16,13 +16,9 @@ abstract class BaseService : Service(), CoroutineScope by MainScope() {
scope: CoroutineScope = this,
context: CoroutineContext = Dispatchers.IO,
block: suspend CoroutineScope.() -> T
): Coroutine<T> {
return Coroutine.async(scope, context) { block() }
}
) = Coroutine.async(scope, context) { block() }
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onBind(intent: Intent?) = null
override fun onDestroy() {
super.onDestroy()

@ -10,7 +10,6 @@ import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.util.*
/**
* Created by Invincible on 2017/11/24.
*
@ -43,7 +42,8 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) :
private var itemClickListener: ((holder: ItemViewHolder, item: ITEM) -> Unit)? = null
private var itemLongClickListener: ((holder: ItemViewHolder, item: ITEM) -> Boolean)? = null
private var itemAnimation: ItemAnimation? = null
// 这个用Kotlin的setter就行了, 不需要手动开一个函数进行设置
var itemAnimation: ItemAnimation? = null
fun setOnItemClickListener(listener: (holder: ItemViewHolder, item: ITEM) -> Unit) {
itemClickListener = listener
@ -71,11 +71,10 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) :
}
}
fun addItemViewDelegates(vararg delegates: Pair<Int, ItemViewDelegate<ITEM>>) {
fun addItemViewDelegates(vararg delegates: Pair<Int, ItemViewDelegate<ITEM>>) =
delegates.forEach {
addItemViewDelegate(it.first, it.second)
}
}
fun addHeaderView(header: View) {
synchronized(lock) {
@ -90,7 +89,7 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) :
}
}
fun addFooterView(footer: View) {
fun addFooterView(footer: View) =
synchronized(lock) {
if (footerItems == null) {
footerItems = SparseArray()
@ -101,9 +100,9 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) :
notifyItemInserted(index)
}
}
}
fun removeHeaderView(header: View) {
fun removeHeaderView(header: View) =
synchronized(lock) {
headerItems?.let {
val index = it.indexOfValue(header)
@ -113,9 +112,8 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) :
}
}
}
}
fun removeFooterView(footer: View) {
fun removeFooterView(footer: View) =
synchronized(lock) {
footerItems?.let {
val index = it.indexOfValue(footer)
@ -125,7 +123,6 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) :
}
}
}
}
fun setItems(items: List<ITEM>?) {
synchronized(lock) {
@ -228,7 +225,7 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) :
}
}
fun updateItem(item: ITEM) {
fun updateItem(item: ITEM) =
synchronized(lock) {
val index = this.items.indexOf(item)
if (index >= 0) {
@ -236,18 +233,16 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) :
notifyItemChanged(index)
}
}
}
fun updateItem(position: Int, payload: Any) {
fun updateItem(position: Int, payload: Any) =
synchronized(lock) {
val size = getActualItemCount()
if (position in 0 until size) {
notifyItemChanged(position + getHeaderCount(), payload)
}
}
}
fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) {
fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) =
synchronized(lock) {
val size = getActualItemCount()
if (fromPosition in 0 until size && toPosition in 0 until size) {
@ -258,74 +253,52 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) :
)
}
}
}
fun clearItems() {
fun clearItems() =
synchronized(lock) {
this.items.clear()
notifyDataSetChanged()
}
}
fun isEmpty(): Boolean {
return items.isEmpty()
}
fun isEmpty() = items.isEmpty()
fun isNotEmpty(): Boolean {
return items.isNotEmpty()
}
fun isNotEmpty() = items.isNotEmpty()
/**
* 除去header和footer
*/
fun getActualItemCount(): Int {
return items.size
}
fun getActualItemCount() = items.size
fun getHeaderCount(): Int {
return headerItems?.size() ?: 0
}
fun getFooterCount(): Int {
return footerItems?.size() ?: 0
}
fun getHeaderCount() = headerItems?.size() ?: 0
fun getFooterCount() = footerItems?.size() ?: 0
fun getItem(position: Int): ITEM? = items.getOrNull(position)
fun getItemByLayoutPosition(position: Int): ITEM? {
val pos = position - getHeaderCount()
return items.getOrNull(pos)
}
fun getItemByLayoutPosition(position: Int) = items.getOrNull(position - getHeaderCount())
fun getItems(): List<ITEM> = items
protected open fun getItemViewType(item: ITEM, position: Int): Int {
return 0
}
protected open fun getItemViewType(item: ITEM, position: Int) = 0
/**
* grid 模式下使用
*/
protected open fun getSpanSize(item: ITEM, viewType: Int, position: Int): Int {
return 1
}
protected open fun getSpanSize(item: ITEM, viewType: Int, position: Int) = 1
final override fun getItemCount(): Int {
return getActualItemCount() + getHeaderCount() + getFooterCount()
}
final override fun getItemCount() = getActualItemCount() + getHeaderCount() + getFooterCount()
final override fun getItemViewType(position: Int): Int {
return when {
final override fun getItemViewType(position: Int) = when {
isHeader(position) -> TYPE_HEADER_VIEW + position
isFooter(position) -> TYPE_FOOTER_VIEW + position - getActualItemCount() - getHeaderCount()
else -> getItem(getActualPosition(position))?.let {
getItemViewType(it, getActualPosition(position))
} ?: 0
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
return when {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = when {
viewType < TYPE_HEADER_VIEW + getHeaderCount() -> {
ItemViewHolder(headerItems!!.get(viewType))
}
@ -365,11 +338,8 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) :
holder
}
}
}
final override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
}
final override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {}
final override fun onBindViewHolder(
holder: ItemViewHolder,
@ -407,21 +377,11 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) :
}
}
fun setItemAnimation(item: ItemAnimation) {
itemAnimation = item
}
private fun isHeader(position: Int) = position < getHeaderCount()
private fun isHeader(position: Int): Boolean {
return position < getHeaderCount()
}
private fun isFooter(position: Int) = position >= getActualItemCount() + getHeaderCount()
private fun isFooter(position: Int): Boolean {
return position >= getActualItemCount() + getHeaderCount()
}
private fun getActualPosition(position: Int): Int {
return position - getHeaderCount()
}
private fun getActualPosition(position: Int) = position - getHeaderCount()
private fun addAnimation(holder: ItemViewHolder) {
itemAnimation?.let {
@ -434,7 +394,6 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) :
}
}
protected open fun startAnimation(holder: ItemViewHolder, item: ItemAnimation) {
item.itemAnimation?.let {
for (anim in it.getAnimators(holder.itemView)) {

@ -16,22 +16,19 @@ class ItemAnimation private constructor() {
var itemAnimDuration: Long = 300L
var itemAnimStartPosition: Int = -1
fun interpolator(interpolator: Interpolator): ItemAnimation {
fun interpolator(interpolator: Interpolator) = apply {
itemAnimInterpolator = interpolator
return this
}
fun duration(duration: Long): ItemAnimation {
fun duration(duration: Long) = apply {
itemAnimDuration = duration
return this
}
fun startPostion(startPos: Int): ItemAnimation {
fun startPosition(startPos: Int) = apply {
itemAnimStartPosition = startPos
return this
}
fun animation(animationType: Int = NONE, animation: BaseAnimation? = null): ItemAnimation {
fun animation(animationType: Int = NONE, animation: BaseAnimation? = null) = apply {
if (animation != null) {
itemAnimation = animation
} else {
@ -43,17 +40,14 @@ class ItemAnimation private constructor() {
RIGHT_SLIDE_IN -> itemAnimation = SlideInRightAnimation()
}
}
return this
}
fun enabled(enabled: Boolean): ItemAnimation {
fun enabled(enabled: Boolean) = apply {
itemAnimEnabled = enabled
return this
}
fun firstOnly(firstOnly: Boolean): ItemAnimation {
fun firstOnly(firstOnly: Boolean) = apply {
itemAnimFirstOnly = firstOnly
return this
}
companion object {
@ -79,8 +73,7 @@ class ItemAnimation private constructor() {
*/
const val RIGHT_SLIDE_IN: Int = 0x00000005
fun create(): ItemAnimation {
return ItemAnimation()
}
fun create() = ItemAnimation()
}
}

@ -7,18 +7,13 @@ enum class Theme {
Dark, Light, Auto, Transparent;
companion object {
fun getTheme(): Theme {
return if (AppConfig.isNightTheme) {
Dark
} else Light
}
fun getTheme() =
if (AppConfig.isNightTheme) Dark
else Light
fun getTheme(backgroundColor: Int) =
if (ColorUtils.isColorLight(backgroundColor)) Light
else Dark
fun getTheme(backgroundColor: Int): Theme {
return if (ColorUtils.isColorLight(backgroundColor)) {
Light
} else {
Dark
}
}
}
}
Loading…
Cancel
Save