Commit 6ddd21eb13e468972b88999d8eff23127062f310

Authored by 权海
1 parent 71be496b

feat(ui):增加是否是中国地区的判断

... ... @@ -10,6 +10,7 @@ import com.doublefeel.app.pigeon.platform.AppleSignInModel
import com.doublefeel.app.pigeon.platform.HResourceType
import com.doublefeel.app.pigeon.platform.PlatformHostApi
import io.flutter.BuildConfig
import java.util.Locale
import kotlin.math.max
import kotlin.math.min
... ... @@ -98,6 +99,16 @@ class PlatformHostApiImpl(private val application: android.app.Application) : Pl
return BuildConfig.DEBUG
}
override fun isChinaRegion(): Boolean {
val country = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
application.resources.configuration.locales.get(0)?.country
} else {
@Suppress("DEPRECATION")
application.resources.configuration.locale?.country
}
return (country ?: Locale.getDefault().country).equals("CN", ignoreCase = true)
}
override fun shareText(
text: String,
callback: (Result<Boolean>) -> Unit
... ...
... ... @@ -338,6 +338,8 @@ interface PlatformHostApi {
* `{systemWebViewUA} {appName}/{versionCode}({versionName})({manufacturer}##{brand}##{model}; {OS}{osVersion}; {height}x{width})(huawei)`
*/
fun getFullUserAgent(): String
/** 是否是中国大陆地区 */
fun isChinaRegion(): Boolean
/** 申请通知权限 */
fun requestNotificationAuth(callback: (Result<Boolean>) -> Unit)
/** 是否是测试环境 */
... ... @@ -423,6 +425,21 @@ interface PlatformHostApi {
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.isChinaRegion$separatedMessageChannelSuffix", codec)
if (api != null) {
channel.setMessageHandler { _, reply ->
val wrapped: List<Any?> = try {
listOf(api.isChinaRegion())
} catch (exception: Throwable) {
PlatformApiPigeonUtils.wrapError(exception)
}
reply.reply(wrapped)
}
} else {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.requestNotificationAuth$separatedMessageChannelSuffix", codec)
if (api != null) {
channel.setMessageHandler { _, reply ->
... ...
... ... @@ -368,6 +368,8 @@ protocol PlatformHostApi {
/// 返回完整的 User-Agent 字符串,由 native 侧组装:
/// `{systemWebViewUA} {appName}/{versionCode}({versionName})({manufacturer}##{brand}##{model}; {OS}{osVersion}; {height}x{width})(huawei)`
func getFullUserAgent() throws -> String
/// 是否是中国大陆地区
func isChinaRegion() throws -> Bool
/// 申请通知权限
func requestNotificationAuth(completion: @escaping (Result<Bool, Error>) -> Void)
/// 是否是测试环境
... ... @@ -438,6 +440,20 @@ class PlatformHostApiSetup {
} else {
getFullUserAgentChannel.setMessageHandler(nil)
}
/// 是否是中国大陆地区
let isChinaRegionChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.isChinaRegion\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
isChinaRegionChannel.setMessageHandler { _, reply in
do {
let result = try api.isChinaRegion()
reply(wrapResult(result))
} catch {
reply(wrapError(error))
}
}
} else {
isChinaRegionChannel.setMessageHandler(nil)
}
/// 申请通知权限
let requestNotificationAuthChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.requestNotificationAuth\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
... ...
... ... @@ -54,7 +54,22 @@ final class PlatformHostApiImpl: PlatformHostApi {
return false
#endif
}
/// 检查appstore 是否是中国大陆区
func isChinaRegion() throws -> Bool {
var localeCountryCode: String?
if let code = SKPaymentQueue.default().storefront?.countryCode{
localeCountryCode = code
}
if localeCountryCode == nil, #available(iOS 16.0, *) {
localeCountryCode = Locale.current.region?.identifier
}
if localeCountryCode == nil,
let code = (Locale.current as NSLocale).object(forKey: .countryCode) as? String{
localeCountryCode = code
}
return localeCountryCode?.uppercased() == "CN"
}
func requestNotificationAuth(completion: @escaping (Result<Bool, any Error>) -> Void) {
Task {
let center = UNUserNotificationCenter.current()
... ...
... ... @@ -23,7 +23,7 @@ class AppEnvironmentConfig {
_isNativeDebug = await _platformHostApi.isDebugEnvoriment();
final stored = await _storage.readRegion();
if (stored == null) {
final detected = _detectRegionFromLocale();
final detected = await _detectRegion();
await setRegion(detected);
} else {
region.value = stored;
... ... @@ -31,6 +31,16 @@ class AppEnvironmentConfig {
return this;
}
Future<AppRegion> _detectRegion() async {
try {
return await _platformHostApi.isChinaRegion()
? AppRegion.china
: AppRegion.global;
} catch (_) {
return _detectRegionFromLocale();
}
}
AppRegion _detectRegionFromLocale() {
final country = WidgetsBinding
.instance.platformDispatcher.locale.countryCode
... ...
... ... @@ -393,6 +393,35 @@ class PlatformHostApi {
}
}
/// 是否是中国大陆地区
Future<bool> isChinaRegion() async {
final String pigeonVar_channelName = 'dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.isChinaRegion$pigeonVar_messageChannelSuffix';
final BasicMessageChannel<Object?> pigeonVar_channel = BasicMessageChannel<Object?>(
pigeonVar_channelName,
pigeonChannelCodec,
binaryMessenger: pigeonVar_binaryMessenger,
);
final Future<Object?> pigeonVar_sendFuture = pigeonVar_channel.send(null);
final List<Object?>? pigeonVar_replyList =
await pigeonVar_sendFuture as List<Object?>?;
if (pigeonVar_replyList == null) {
throw _createConnectionError(pigeonVar_channelName);
} else if (pigeonVar_replyList.length > 1) {
throw PlatformException(
code: pigeonVar_replyList[0]! as String,
message: pigeonVar_replyList[1] as String?,
details: pigeonVar_replyList[2],
);
} else if (pigeonVar_replyList[0] == null) {
throw PlatformException(
code: 'null-error',
message: 'Host platform returned null value for non-null return value.',
);
} else {
return (pigeonVar_replyList[0] as bool?)!;
}
}
/// 申请通知权限
Future<bool> requestNotificationAuth() async {
final String pigeonVar_channelName = 'dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.requestNotificationAuth$pigeonVar_messageChannelSuffix';
... ...
... ... @@ -115,6 +115,9 @@ abstract class PlatformHostApi {
/// 返回完整的 User-Agent 字符串,由 native 侧组装:
/// `{systemWebViewUA} {appName}/{versionCode}({versionName})({manufacturer}##{brand}##{model}; {OS}{osVersion}; {height}x{width})(huawei)`
String getFullUserAgent();
/// 是否是中国大陆地区
bool isChinaRegion();
/// 申请通知权限
@async
... ...