Skip to content

Commit

Permalink
Merge pull request #282 from RedrockMobile/GuoXR/bugfix2
Browse files Browse the repository at this point in the history
Guo xr/bugfix2
  • Loading branch information
985892345 authored Aug 29, 2022
2 parents 557468c + bd5e1a3 commit 867e394
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,5 @@ abstract class BaseActivity : OperationActivity() {
* 这样写会在 intent 中寻找名字叫 key 的参数
* ```
*/
fun <T : Any> Intent.helper() = IntentHelper<T> { intent }
inline fun <reified T : Any> Intent?.helper() = IntentHelper(T::class.java) { intent }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import androidx.core.os.bundleOf
import com.mredrock.cyxbs.lib.utils.utils.GenericityUtils
import java.io.Serializable
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
Expand All @@ -13,19 +12,19 @@ import kotlin.reflect.KProperty
* 这里面所有的类型参考了官方的设计:[bundleOf]
*/
class IntentHelper<T: Any>(
val intent: () -> Intent
private val clazz: Class<T>,
private val intent: () -> Intent
) : ReadWriteProperty<Any, T> {

private var mValue: T? = null
private val mType = GenericityUtils.getGenericClassFromSuperClass<T, Any>(javaClass)

@Suppress("UNCHECKED_CAST")
override fun getValue(thisRef: Any, property: KProperty<*>): T {
if (mValue != null) return mValue!!
val name = property.name
mValue = intent.invoke().run {
// 因为 intent 的 getExtra() 方法被废弃,所以只能一个一个判断
when (mType) {
when (clazz) {
String::class -> getStringExtra(name)

Int::class -> getIntExtra(name, Int.MIN_VALUE)
Expand All @@ -48,7 +47,7 @@ class IntentHelper<T: Any>(
ShortArray::class -> getShortArrayExtra(name)

Array::class -> {
val componentType = mType::class.java.componentType!!
val componentType = clazz.componentType!!
when {
Parcelable::class.java.isAssignableFrom(componentType) -> {
getParcelableArrayExtra(name)
Expand All @@ -73,10 +72,10 @@ class IntentHelper<T: Any>(

else -> {
when {
CharSequence::class.java.isAssignableFrom(mType) -> getCharExtra(name, ' ')
Parcelable::class.java.isAssignableFrom(mType) -> getParcelableExtra(name)
Serializable::class.java.isAssignableFrom(mType) -> getSerializableExtra(name)
else -> error("未实现该类型 ${mType::class.java.name} for key \"$name\"")
CharSequence::class.java.isAssignableFrom(clazz) -> getCharExtra(name, ' ')
Parcelable::class.java.isAssignableFrom(clazz) -> getParcelableExtra(name)
Serializable::class.java.isAssignableFrom(clazz) -> getSerializableExtra(name)
else -> error("未实现该类型 ${clazz.name} for key \"$name\"")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mredrock.cyxbs.lib.utils.utils

import java.lang.RuntimeException
import java.lang.reflect.ParameterizedType

/**
Expand Down Expand Up @@ -49,6 +48,6 @@ object GenericityUtils {
}
}
}
throw RuntimeException("你父类的泛型为: $genericSuperclass, 其中不存在 ${E::class.java.simpleName}")
throw IllegalStateException("你父类的泛型为: $genericSuperclass, 其中不存在 ${E::class.java.simpleName}")
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package com.mredrock.cyxbs.discover.map.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.mredrock.cyxbs.common.utils.extensions.pressToZoomOut
import com.mredrock.cyxbs.discover.map.R

class DetailTagRvAdapter(val context: Context, private val mList: MutableList<String>) : RecyclerView.Adapter<DetailTagRvAdapter.ViewHolder>() {

class DetailTagRvAdapter : ListAdapter<String, DetailTagRvAdapter.ViewHolder>(
object : DiffUtil.ItemCallback<String>() {
override fun areItemsTheSame(oldItem: String, newItem: String): Boolean {
return oldItem == newItem
}

override fun areContentsTheSame(oldItem: String, newItem: String): Boolean {
return true
}

override fun getChangePayload(oldItem: String, newItem: String): Any {
return "" // 不返回 null 可以避免 ViewHolder 互换,减少性能消耗
}
}
) {

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val tag: TextView = view.findViewById<TextView>(R.id.map_tv_recycle_item_detail_tag).apply { pressToZoomOut() }
Expand All @@ -22,18 +36,7 @@ class DetailTagRvAdapter(val context: Context, private val mList: MutableList<St
return ViewHolder(view)
}

override fun getItemCount(): Int {
return mList.size
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.tag.text = mList[position]
holder.tag.text = getItem(position)
}


fun setList(list: List<String>) {
mList.clear()
mList.addAll(list)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class PlaceDetailBottomSheetFragment : BaseFragment() {
val flexBoxManager = FlexboxLayoutManager(context)
flexBoxManager.flexWrap = FlexWrap.WRAP
mRvDetailAboutList.layoutManager = flexBoxManager
val tagAdapter = context?.let { DetailTagRvAdapter(it, mutableListOf()) }
val tagAdapter = DetailTagRvAdapter()
mRvDetailAboutList.adapter = tagAdapter

val linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
Expand Down Expand Up @@ -103,10 +103,15 @@ class PlaceDetailBottomSheetFragment : BaseFragment() {
mIndicatorDetail.setupWithViewPager(mBannerDetailImage)
}
}
if (tagAdapter != null && t.tags != null) {
tagAdapter.setList(t.tags!!)
tagAdapter.notifyDataSetChanged()
if (t.tags != null) {
mBinding.mapTvDetailAboutText.visible()
mBinding.mapRvDetailAboutList.visible()
tagAdapter.submitList(t.tags ?: emptyList())
} else {
mBinding.mapTvDetailAboutText.gone()
mBinding.mapRvDetailAboutList.gone()
}

if (attributeAdapter != null) {
if (t.placeAttribute != null) {
attributeAdapter.setList(t.placeAttribute!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.mredrock.cyxbs.lib.utils.extensions.*
import com.mredrock.cyxbs.lib.utils.network.mapOrThrowApiException
import com.mredrock.cyxbs.lib.utils.service.impl
import com.mredrock.cyxbs.sport.model.network.SportDetailApiService
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -85,6 +87,8 @@ class SportRepository : InitialService {
if (stuNum != null && sSportData == null) {
SportDetailApiService.INSTANCE
.getSportDetailData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.mapOrThrowApiException()
.interceptExceptionByResult {
emitter.onSuccess(Result.failure(throwable))
Expand Down

0 comments on commit 867e394

Please sign in to comment.