feat:补充上一个节点的提交
This commit is contained in:
@@ -1,118 +0,0 @@
|
|||||||
package com.yizhuan.xchat_android_core.region
|
|
||||||
|
|
||||||
import android.widget.TextView
|
|
||||||
import androidx.lifecycle.Lifecycle
|
|
||||||
import androidx.lifecycle.coroutineScope
|
|
||||||
import com.example.lib_utils.AppUtils
|
|
||||||
import com.example.lib_utils.TelephonyUtils
|
|
||||||
import com.google.gson.Gson
|
|
||||||
import com.google.gson.reflect.TypeToken
|
|
||||||
import com.yizhuan.xchat_android_core.region.bean.RegionBean
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import java.io.InputStream
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by Max on 2023/12/7 18:23
|
|
||||||
* Desc:地区
|
|
||||||
**/
|
|
||||||
class RegionHelper {
|
|
||||||
|
|
||||||
fun loadRecommendRegion(lifecycle: Lifecycle, textView: TextView){
|
|
||||||
lifecycle.coroutineScope.launch {
|
|
||||||
RegionHelper().getCurrentOrDefRegion().let {
|
|
||||||
textView.text = it.fullCode
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getDefaultRegion(): RegionBean {
|
|
||||||
return RegionBean(name = "Taiwan", abbr = "TW", mcc = "466", code = "886")
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取当前地区或默认
|
|
||||||
*/
|
|
||||||
private suspend fun getCurrentOrDefRegion(): RegionBean {
|
|
||||||
return withContext(Dispatchers.IO) {
|
|
||||||
var operator = TelephonyUtils.getNetWorkOperator()
|
|
||||||
if (operator.isNullOrEmpty()) {
|
|
||||||
operator = TelephonyUtils.getSimOperator()
|
|
||||||
}
|
|
||||||
val mcc = operator?.take(3)
|
|
||||||
val region = getAllRegionList().firstOrNull {
|
|
||||||
it.mcc == mcc
|
|
||||||
}
|
|
||||||
region ?: getDefaultRegion()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取地区选择器列表
|
|
||||||
*/
|
|
||||||
suspend fun getRegionSelectorList(groupItemType: Int): List<RegionBean> {
|
|
||||||
return withContext(Dispatchers.IO) {
|
|
||||||
val list = ArrayList<RegionBean>()
|
|
||||||
list.addAll(getHotRegionList())
|
|
||||||
var lastGroup: Char? = null
|
|
||||||
list.addAll(getAllRegionList()
|
|
||||||
.map {
|
|
||||||
val firstName = it.name?.firstOrNull()?.uppercaseChar() ?: '#'
|
|
||||||
if (firstName in 'A'..'Z') {
|
|
||||||
it.groupName = firstName
|
|
||||||
it.sortedBy = "$firstName${it.name?.uppercase()}"
|
|
||||||
} else {
|
|
||||||
it.groupName = '#'
|
|
||||||
it.sortedBy = "${'Z' + 1}${it.name?.uppercase()}"
|
|
||||||
}
|
|
||||||
it
|
|
||||||
}
|
|
||||||
.sortedBy {
|
|
||||||
it.sortedBy
|
|
||||||
}.map {
|
|
||||||
val groupName = it.groupName ?: '#'
|
|
||||||
if (groupName != lastGroup) {
|
|
||||||
it.itemType = groupItemType
|
|
||||||
}
|
|
||||||
lastGroup = groupName
|
|
||||||
it
|
|
||||||
}
|
|
||||||
)
|
|
||||||
list
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取热门地区
|
|
||||||
*/
|
|
||||||
suspend fun getHotRegionList(): List<RegionBean> {
|
|
||||||
return getRegionListFromAssets("hot_region.json")
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取全部地区
|
|
||||||
*/
|
|
||||||
suspend fun getAllRegionList(): List<RegionBean> {
|
|
||||||
return getRegionListFromAssets("region.json")
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 从资源文件中获取地区列表
|
|
||||||
*/
|
|
||||||
private fun getRegionListFromAssets(fileName: String): MutableList<RegionBean> {
|
|
||||||
//获取IO流
|
|
||||||
try {
|
|
||||||
val inputStream: InputStream =
|
|
||||||
AppUtils.getApp().applicationContext.assets.open(fileName)
|
|
||||||
val json: String
|
|
||||||
inputStream.use {
|
|
||||||
json = it.bufferedReader().readText()
|
|
||||||
}
|
|
||||||
return Gson().fromJson(json, object : TypeToken<MutableList<RegionBean>>() {}.type)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
e.printStackTrace()
|
|
||||||
}
|
|
||||||
return mutableListOf()
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,48 +0,0 @@
|
|||||||
package com.yizhuan.xchat_android_core.region.bean
|
|
||||||
|
|
||||||
import com.chad.library.adapter.base.entity.MultiItemEntity
|
|
||||||
import com.netease.nim.uikit.business.ait.selector.model.ItemType
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by Max on 2023/12/7 18:48
|
|
||||||
* Desc:地区
|
|
||||||
**/
|
|
||||||
data class RegionBean(
|
|
||||||
// 名称(英文)
|
|
||||||
val name: String?,
|
|
||||||
// 简称
|
|
||||||
val abbr: String?,
|
|
||||||
// 区号
|
|
||||||
val code: String?,
|
|
||||||
// MCC
|
|
||||||
val mcc: String?
|
|
||||||
) : MultiItemEntity {
|
|
||||||
|
|
||||||
val fullCode: String?
|
|
||||||
get() {
|
|
||||||
if (code == null) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
return if (code.startsWith("+")) {
|
|
||||||
code
|
|
||||||
} else {
|
|
||||||
"+$code"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 本地分组用到
|
|
||||||
var groupName: Char? = null
|
|
||||||
|
|
||||||
// 本地排序用到
|
|
||||||
var sortedBy: String? = null
|
|
||||||
|
|
||||||
private var itemType: Int = 0
|
|
||||||
|
|
||||||
fun setItemType(itemType: Int) {
|
|
||||||
this.itemType = itemType
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getItemType(): Int {
|
|
||||||
return itemType
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user