Merge branch 'peko_feature/region' into peko_test/2.4.0

This commit is contained in:
Max
2023-12-08 16:01:30 +08:00
19 changed files with 505 additions and 287 deletions

View File

@@ -20,7 +20,6 @@ import com.netease.nimlib.sdk.msg.MsgService;
import com.orhanobut.logger.Logger;
import com.yizhuan.xchat_android_core.DemoCache;
import com.yizhuan.xchat_android_core.R;
import com.yizhuan.xchat_android_core.auth.bean.AreaInfoBean;
import com.yizhuan.xchat_android_core.auth.entity.AccountInfo;
import com.yizhuan.xchat_android_core.auth.entity.ThirdUserInfo;
import com.yizhuan.xchat_android_core.auth.entity.TicketInfo;
@@ -1001,17 +1000,6 @@ public class AuthModel extends BaseModel implements IAuthModel {
.compose(RxHelper.handleSchedulers());
}
/**
* 区号
*
* @return
*/
public Single<List<AreaInfoBean>> getAreaCodeList() {
return api.getAreaCodeList()
.compose(RxHelper.handleBeanData())
.compose(RxHelper.handleSchedulers());
}
/**
* 发送验证码
*
@@ -1273,14 +1261,6 @@ public class AuthModel extends BaseModel implements IAuthModel {
@POST("phone/auth/bound")
Single<ServiceResult<String>> boundAuthCode(@Field("authCode") String authCode);
/**
* 获取地区码
*
* @return
*/
@GET("areaInfo/list")
Single<ServiceResult<List<AreaInfoBean>>> getAreaCodeList();
/**
* 发送验证码
*

View File

@@ -1,15 +0,0 @@
package com.yizhuan.xchat_android_core.auth.bean
import lombok.Data
@Data
class AreaInfoBean(
val id: Long = 0L,
val name: String? = null,
val abbr: String? = null,
val phoneAreaCode: String? = null,
val seq: Int = 0,
val status: Int = 0,
val createTime: Long = 0L,
val updateTime: Long = 0L
)

View File

@@ -0,0 +1,118 @@
package com.yizhuan.xchat_android_core.region
import android.widget.TextView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.coroutineScope
import com.chuhai.utils.AppUtils
import com.chuhai.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()
}
}

View File

@@ -0,0 +1,48 @@
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
}
}