优化:首页弹窗tips多语言适配 #544

This commit is contained in:
pppscn 2024-11-11 20:49:38 +08:00
parent aa8ab646f5
commit c86f20c6de
9 changed files with 37 additions and 157 deletions

View File

@ -1,24 +0,0 @@
package com.idormy.sms.forwarder.core.http.api
import com.idormy.sms.forwarder.core.http.entity.TipInfo
import com.xuexiang.xhttp2.model.ApiResult
import io.reactivex.Observable
import retrofit2.http.GET
/**
* @author xuexiang
* @since 2021/1/9 7:01 PM
*/
@Suppress("unused")
class ApiService {
/**
* 使用的是retrofit的接口定义
*/
interface IGetService {
/**
* 获得小贴士
*/
@get:GET("/pp/SmsForwarder.wiki/raw/master/tips.json")
val tips: Observable<ApiResult<List<TipInfo?>?>>
}
}

View File

@ -1,35 +0,0 @@
package com.idormy.sms.forwarder.core.http.callback
import com.xuexiang.xhttp2.callback.SimpleCallBack
import com.xuexiang.xhttp2.exception.ApiException
import com.xuexiang.xhttp2.model.XHttpRequest
import com.xuexiang.xutil.common.StringUtils
import com.xuexiang.xutil.common.logger.Logger
/**
* 不带错误提示的网络请求回调
*
* @author xuexiang
* @since 2019-11-18 23:02
*/
@Suppress("unused")
abstract class NoTipCallBack<T> : SimpleCallBack<T> {
/**
* 记录一下请求的url,确定出错的请求是哪个请求
*/
private var mUrl: String? = null
constructor()
constructor(req: XHttpRequest) : this(req.url)
constructor(url: String?) {
mUrl = url
}
override fun onError(e: ApiException) {
if (!StringUtils.isEmpty(mUrl)) {
Logger.e("Request Url: $mUrl", e)
} else {
Logger.e(e)
}
}
}

View File

@ -1,37 +0,0 @@
package com.idormy.sms.forwarder.core.http.callback
import com.idormy.sms.forwarder.utils.XToastUtils
import com.xuexiang.xhttp2.callback.SimpleCallBack
import com.xuexiang.xhttp2.exception.ApiException
import com.xuexiang.xhttp2.model.XHttpRequest
import com.xuexiang.xutil.common.StringUtils
import com.xuexiang.xutil.common.logger.Logger
/**
* 带错误toast提示的网络请求回调
*
* @author xuexiang
* @since 2019-11-18 23:02
*/
@Suppress("unused")
abstract class TipCallBack<T> : SimpleCallBack<T> {
/**
* 记录一下请求的url,确定出错的请求是哪个请求
*/
private var mUrl: String? = null
constructor()
constructor(req: XHttpRequest) : this(req.url)
constructor(url: String?) {
mUrl = url
}
override fun onError(e: ApiException) {
XToastUtils.error(e)
if (!StringUtils.isEmpty(mUrl)) {
Logger.e("Request Url: $mUrl", e)
} else {
Logger.e(e)
}
}
}

View File

@ -1,45 +0,0 @@
package com.idormy.sms.forwarder.core.http.callback
import com.idormy.sms.forwarder.core.BaseFragment
import com.idormy.sms.forwarder.utils.XToastUtils
import com.xuexiang.xhttp2.callback.ProgressLoadingCallBack
import com.xuexiang.xhttp2.exception.ApiException
import com.xuexiang.xhttp2.model.XHttpRequest
import com.xuexiang.xhttp2.subsciber.impl.IProgressLoader
import com.xuexiang.xutil.common.StringUtils
import com.xuexiang.xutil.common.logger.Logger
/**
* 带错误toast提示和加载进度条的网络请求回调
*
* @author xuexiang
* @since 2019-11-18 23:16
*/
@Suppress("unused")
abstract class TipProgressLoadingCallBack<T> : ProgressLoadingCallBack<T> {
/**
* 记录一下请求的url,确定出错的请求是哪个请求
*/
private var mUrl: String? = null
constructor(fragment: BaseFragment<*>) : super(fragment.progressLoader)
constructor(iProgressLoader: IProgressLoader?) : super(iProgressLoader)
constructor(req: XHttpRequest, iProgressLoader: IProgressLoader?) : this(
req.url,
iProgressLoader
)
constructor(url: String?, iProgressLoader: IProgressLoader?) : super(iProgressLoader) {
mUrl = url
}
override fun onError(e: ApiException) {
super.onError(e)
XToastUtils.error(e)
if (!StringUtils.isEmpty(mUrl)) {
Logger.e("Request Url: $mUrl", e)
} else {
Logger.e(e)
}
}
}

View File

@ -6,17 +6,19 @@ import android.widget.CompoundButton
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.AppCompatCheckBox
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.reflect.TypeToken
import com.idormy.sms.forwarder.R
import com.idormy.sms.forwarder.core.http.api.ApiService.IGetService
import com.idormy.sms.forwarder.core.http.callback.NoTipCallBack
import com.idormy.sms.forwarder.core.http.entity.TipInfo
import com.idormy.sms.forwarder.utils.AppUtils
import com.idormy.sms.forwarder.utils.SharedPreference
import com.xuexiang.constant.TimeConstants
import com.xuexiang.xaop.annotation.SingleClick
import com.xuexiang.xhttp2.XHttp
import com.xuexiang.xhttp2.cache.model.CacheMode
import com.xuexiang.xhttp2.callback.SimpleCallBack
import com.xuexiang.xhttp2.exception.ApiException
import com.xuexiang.xui.widget.dialog.BaseDialog
import com.xuexiang.xutil.resource.ResUtils.getString
import com.zzhoujay.richtext.RichText
/**
@ -163,18 +165,33 @@ class GuideTipsDialog(context: Context?, tips: List<TipInfo>) :
*/
@JvmStatic
fun showTipsForce(context: Context?) {
val request = XHttp.custom().cacheMode(CacheMode.FIRST_CACHE)
.cacheTime(TimeConstants.DAY.toLong()).cacheKey("getTips")
request.apiCall(request.create(
IGetService::class.java
).tips, object : NoTipCallBack<List<TipInfo>>() {
@Throws(Throwable::class)
override fun onSuccess(response: List<TipInfo>?) {
if (!response.isNullOrEmpty()) {
GuideTipsDialog(context, response).show()
XHttp.get(getString(R.string.url_tips))
.keepJson(true)
.ignoreHttpsCert()
.timeStamp(true) //url自动追加时间戳避免缓存
.execute(object : SimpleCallBack<String>() {
override fun onError(e: ApiException) {
e.printStackTrace()
}
}
})
override fun onSuccess(json: String) {
try {
val gson = Gson()
val jsonObject = gson.fromJson(json, JsonObject::class.java)
if (jsonObject.isJsonObject
&& jsonObject.has("Code") && jsonObject["Code"].asInt == 0
&& jsonObject.has("Data") && jsonObject["Data"].isJsonArray
) {
val dataJsonArray = jsonObject["Data"].asJsonArray
val listType = object : TypeToken<List<TipInfo>>() {}.type
val tips = gson.fromJson<List<TipInfo>>(dataJsonArray, listType)
GuideTipsDialog(context, tips).show()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
})
}
fun setIsIgnoreTips(isIgnore: Boolean): Boolean {
@ -190,4 +207,4 @@ class GuideTipsDialog(context: Context?, tips: List<TipInfo>) :
initViews()
updateTips(tips)
}
}
}

View File

@ -58,6 +58,7 @@
<string name="url_help">https://gitee.com/pp/SmsForwarder/wikis/pages</string>
<string name="url_donation_link">https://gitee.com/pp/SmsForwarder.wiki/raw/master/%E6%89%93%E8%B5%8F%E5%90%8D%E5%8D%95.md</string>
<string name="url_wechat_miniprogram">https://gitee.com/pp/SmsForwarder/raw/main/pic/wechat_miniprogram.jpg</string>
<string name="url_tips">https://gitee.com/pp/SmsForwarder.wiki/raw/master/tips_en.json</string>
<string name="lab_yes">Yes</string>
<string name="lab_no">No</string>

View File

@ -58,6 +58,7 @@
<string name="url_help">https://gitee.com/pp/SmsForwarder/wikis/pages</string>
<string name="url_donation_link">https://gitee.com/pp/SmsForwarder.wiki/raw/master/%E6%89%93%E8%B5%8F%E5%90%8D%E5%8D%95.md</string>
<string name="url_wechat_miniprogram">https://gitee.com/pp/SmsForwarder/raw/main/pic/wechat_miniprogram.jpg</string>
<string name="url_tips">https://gitee.com/pp/SmsForwarder.wiki/raw/master/tips.json</string>
<string name="lab_yes"></string>
<string name="lab_no"></string>

View File

@ -58,6 +58,7 @@
<string name="url_help">https://gitee.com/pp/SmsForwarder/wikis/pages</string>
<string name="url_donation_link">https://gitee.com/pp/SmsForwarder.wiki/raw/master/%E6%89%93%E8%B5%8F%E5%90%8D%E5%8D%95.md</string>
<string name="url_wechat_miniprogram">https://gitee.com/pp/SmsForwarder/raw/main/pic/wechat_miniprogram.jpg</string>
<string name="url_tips">https://gitee.com/pp/SmsForwarder.wiki/raw/master/tips_tc.json</string>
<string name="lab_yes"></string>
<string name="lab_no"></string>

View File

@ -85,6 +85,7 @@
<string name="url_help">https://gitee.com/pp/SmsForwarder/wikis/pages</string>
<string name="url_donation_link">https://gitee.com/pp/SmsForwarder.wiki/raw/master/%E6%89%93%E8%B5%8F%E5%90%8D%E5%8D%95.md</string>
<string name="url_wechat_miniprogram">https://gitee.com/pp/SmsForwarder/raw/main/pic/wechat_miniprogram.jpg</string>
<string name="url_tips">https://gitee.com/pp/SmsForwarder.wiki/raw/master/tips.json</string>
<string name="lab_yes"></string>
<string name="lab_no"></string>