diff --git a/core/src/main/java/com/yizhuan/xchat_android_core/region/RegionHelper.kt b/core/src/main/java/com/yizhuan/xchat_android_core/region/RegionHelper.kt deleted file mode 100644 index d09aad90a..000000000 --- a/core/src/main/java/com/yizhuan/xchat_android_core/region/RegionHelper.kt +++ /dev/null @@ -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 { - return withContext(Dispatchers.IO) { - val list = ArrayList() - 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 { - return getRegionListFromAssets("hot_region.json") - } - - /** - * 获取全部地区 - */ - suspend fun getAllRegionList(): List { - return getRegionListFromAssets("region.json") - } - - /** - * 从资源文件中获取地区列表 - */ - private fun getRegionListFromAssets(fileName: String): MutableList { - //获取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>() {}.type) - } catch (e: Exception) { - e.printStackTrace() - } - return mutableListOf() - } -} \ No newline at end of file diff --git a/core/src/main/java/com/yizhuan/xchat_android_core/region/bean/RegionBean.kt b/core/src/main/java/com/yizhuan/xchat_android_core/region/bean/RegionBean.kt deleted file mode 100644 index 0294278ee..000000000 --- a/core/src/main/java/com/yizhuan/xchat_android_core/region/bean/RegionBean.kt +++ /dev/null @@ -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 - } -} \ No newline at end of file