Commit 6ddd21eb13e468972b88999d8eff23127062f310

Authored by 权海
1 parent 71be496b

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

@@ -10,6 +10,7 @@ import com.doublefeel.app.pigeon.platform.AppleSignInModel @@ -10,6 +10,7 @@ import com.doublefeel.app.pigeon.platform.AppleSignInModel
10 import com.doublefeel.app.pigeon.platform.HResourceType 10 import com.doublefeel.app.pigeon.platform.HResourceType
11 import com.doublefeel.app.pigeon.platform.PlatformHostApi 11 import com.doublefeel.app.pigeon.platform.PlatformHostApi
12 import io.flutter.BuildConfig 12 import io.flutter.BuildConfig
  13 +import java.util.Locale
13 import kotlin.math.max 14 import kotlin.math.max
14 import kotlin.math.min 15 import kotlin.math.min
15 16
@@ -98,6 +99,16 @@ class PlatformHostApiImpl(private val application: android.app.Application) : Pl @@ -98,6 +99,16 @@ class PlatformHostApiImpl(private val application: android.app.Application) : Pl
98 return BuildConfig.DEBUG 99 return BuildConfig.DEBUG
99 } 100 }
100 101
  102 + override fun isChinaRegion(): Boolean {
  103 + val country = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  104 + application.resources.configuration.locales.get(0)?.country
  105 + } else {
  106 + @Suppress("DEPRECATION")
  107 + application.resources.configuration.locale?.country
  108 + }
  109 + return (country ?: Locale.getDefault().country).equals("CN", ignoreCase = true)
  110 + }
  111 +
101 override fun shareText( 112 override fun shareText(
102 text: String, 113 text: String,
103 callback: (Result<Boolean>) -> Unit 114 callback: (Result<Boolean>) -> Unit
@@ -338,6 +338,8 @@ interface PlatformHostApi { @@ -338,6 +338,8 @@ interface PlatformHostApi {
338 * `{systemWebViewUA} {appName}/{versionCode}({versionName})({manufacturer}##{brand}##{model}; {OS}{osVersion}; {height}x{width})(huawei)` 338 * `{systemWebViewUA} {appName}/{versionCode}({versionName})({manufacturer}##{brand}##{model}; {OS}{osVersion}; {height}x{width})(huawei)`
339 */ 339 */
340 fun getFullUserAgent(): String 340 fun getFullUserAgent(): String
  341 + /** 是否是中国大陆地区 */
  342 + fun isChinaRegion(): Boolean
341 /** 申请通知权限 */ 343 /** 申请通知权限 */
342 fun requestNotificationAuth(callback: (Result<Boolean>) -> Unit) 344 fun requestNotificationAuth(callback: (Result<Boolean>) -> Unit)
343 /** 是否是测试环境 */ 345 /** 是否是测试环境 */
@@ -423,6 +425,21 @@ interface PlatformHostApi { @@ -423,6 +425,21 @@ interface PlatformHostApi {
423 } 425 }
424 } 426 }
425 run { 427 run {
  428 + val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.isChinaRegion$separatedMessageChannelSuffix", codec)
  429 + if (api != null) {
  430 + channel.setMessageHandler { _, reply ->
  431 + val wrapped: List<Any?> = try {
  432 + listOf(api.isChinaRegion())
  433 + } catch (exception: Throwable) {
  434 + PlatformApiPigeonUtils.wrapError(exception)
  435 + }
  436 + reply.reply(wrapped)
  437 + }
  438 + } else {
  439 + channel.setMessageHandler(null)
  440 + }
  441 + }
  442 + run {
426 val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.requestNotificationAuth$separatedMessageChannelSuffix", codec) 443 val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.requestNotificationAuth$separatedMessageChannelSuffix", codec)
427 if (api != null) { 444 if (api != null) {
428 channel.setMessageHandler { _, reply -> 445 channel.setMessageHandler { _, reply ->
@@ -368,6 +368,8 @@ protocol PlatformHostApi { @@ -368,6 +368,8 @@ protocol PlatformHostApi {
368 /// 返回完整的 User-Agent 字符串,由 native 侧组装: 368 /// 返回完整的 User-Agent 字符串,由 native 侧组装:
369 /// `{systemWebViewUA} {appName}/{versionCode}({versionName})({manufacturer}##{brand}##{model}; {OS}{osVersion}; {height}x{width})(huawei)` 369 /// `{systemWebViewUA} {appName}/{versionCode}({versionName})({manufacturer}##{brand}##{model}; {OS}{osVersion}; {height}x{width})(huawei)`
370 func getFullUserAgent() throws -> String 370 func getFullUserAgent() throws -> String
  371 + /// 是否是中国大陆地区
  372 + func isChinaRegion() throws -> Bool
371 /// 申请通知权限 373 /// 申请通知权限
372 func requestNotificationAuth(completion: @escaping (Result<Bool, Error>) -> Void) 374 func requestNotificationAuth(completion: @escaping (Result<Bool, Error>) -> Void)
373 /// 是否是测试环境 375 /// 是否是测试环境
@@ -438,6 +440,20 @@ class PlatformHostApiSetup { @@ -438,6 +440,20 @@ class PlatformHostApiSetup {
438 } else { 440 } else {
439 getFullUserAgentChannel.setMessageHandler(nil) 441 getFullUserAgentChannel.setMessageHandler(nil)
440 } 442 }
  443 + /// 是否是中国大陆地区
  444 + let isChinaRegionChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.isChinaRegion\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
  445 + if let api = api {
  446 + isChinaRegionChannel.setMessageHandler { _, reply in
  447 + do {
  448 + let result = try api.isChinaRegion()
  449 + reply(wrapResult(result))
  450 + } catch {
  451 + reply(wrapError(error))
  452 + }
  453 + }
  454 + } else {
  455 + isChinaRegionChannel.setMessageHandler(nil)
  456 + }
441 /// 申请通知权限 457 /// 申请通知权限
442 let requestNotificationAuthChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.requestNotificationAuth\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec) 458 let requestNotificationAuthChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.requestNotificationAuth\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
443 if let api = api { 459 if let api = api {
@@ -54,7 +54,22 @@ final class PlatformHostApiImpl: PlatformHostApi { @@ -54,7 +54,22 @@ final class PlatformHostApiImpl: PlatformHostApi {
54 return false 54 return false
55 #endif 55 #endif
56 } 56 }
57 - 57 + /// 检查appstore 是否是中国大陆区
  58 + func isChinaRegion() throws -> Bool {
  59 + var localeCountryCode: String?
  60 + if let code = SKPaymentQueue.default().storefront?.countryCode{
  61 + localeCountryCode = code
  62 + }
  63 + if localeCountryCode == nil, #available(iOS 16.0, *) {
  64 + localeCountryCode = Locale.current.region?.identifier
  65 + }
  66 + if localeCountryCode == nil,
  67 + let code = (Locale.current as NSLocale).object(forKey: .countryCode) as? String{
  68 + localeCountryCode = code
  69 + }
  70 + return localeCountryCode?.uppercased() == "CN"
  71 + }
  72 +
58 func requestNotificationAuth(completion: @escaping (Result<Bool, any Error>) -> Void) { 73 func requestNotificationAuth(completion: @escaping (Result<Bool, any Error>) -> Void) {
59 Task { 74 Task {
60 let center = UNUserNotificationCenter.current() 75 let center = UNUserNotificationCenter.current()
@@ -23,7 +23,7 @@ class AppEnvironmentConfig { @@ -23,7 +23,7 @@ class AppEnvironmentConfig {
23 _isNativeDebug = await _platformHostApi.isDebugEnvoriment(); 23 _isNativeDebug = await _platformHostApi.isDebugEnvoriment();
24 final stored = await _storage.readRegion(); 24 final stored = await _storage.readRegion();
25 if (stored == null) { 25 if (stored == null) {
26 - final detected = _detectRegionFromLocale(); 26 + final detected = await _detectRegion();
27 await setRegion(detected); 27 await setRegion(detected);
28 } else { 28 } else {
29 region.value = stored; 29 region.value = stored;
@@ -31,6 +31,16 @@ class AppEnvironmentConfig { @@ -31,6 +31,16 @@ class AppEnvironmentConfig {
31 return this; 31 return this;
32 } 32 }
33 33
  34 + Future<AppRegion> _detectRegion() async {
  35 + try {
  36 + return await _platformHostApi.isChinaRegion()
  37 + ? AppRegion.china
  38 + : AppRegion.global;
  39 + } catch (_) {
  40 + return _detectRegionFromLocale();
  41 + }
  42 + }
  43 +
34 AppRegion _detectRegionFromLocale() { 44 AppRegion _detectRegionFromLocale() {
35 final country = WidgetsBinding 45 final country = WidgetsBinding
36 .instance.platformDispatcher.locale.countryCode 46 .instance.platformDispatcher.locale.countryCode
@@ -393,6 +393,35 @@ class PlatformHostApi { @@ -393,6 +393,35 @@ class PlatformHostApi {
393 } 393 }
394 } 394 }
395 395
  396 + /// 是否是中国大陆地区
  397 + Future<bool> isChinaRegion() async {
  398 + final String pigeonVar_channelName = 'dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.isChinaRegion$pigeonVar_messageChannelSuffix';
  399 + final BasicMessageChannel<Object?> pigeonVar_channel = BasicMessageChannel<Object?>(
  400 + pigeonVar_channelName,
  401 + pigeonChannelCodec,
  402 + binaryMessenger: pigeonVar_binaryMessenger,
  403 + );
  404 + final Future<Object?> pigeonVar_sendFuture = pigeonVar_channel.send(null);
  405 + final List<Object?>? pigeonVar_replyList =
  406 + await pigeonVar_sendFuture as List<Object?>?;
  407 + if (pigeonVar_replyList == null) {
  408 + throw _createConnectionError(pigeonVar_channelName);
  409 + } else if (pigeonVar_replyList.length > 1) {
  410 + throw PlatformException(
  411 + code: pigeonVar_replyList[0]! as String,
  412 + message: pigeonVar_replyList[1] as String?,
  413 + details: pigeonVar_replyList[2],
  414 + );
  415 + } else if (pigeonVar_replyList[0] == null) {
  416 + throw PlatformException(
  417 + code: 'null-error',
  418 + message: 'Host platform returned null value for non-null return value.',
  419 + );
  420 + } else {
  421 + return (pigeonVar_replyList[0] as bool?)!;
  422 + }
  423 + }
  424 +
396 /// 申请通知权限 425 /// 申请通知权限
397 Future<bool> requestNotificationAuth() async { 426 Future<bool> requestNotificationAuth() async {
398 final String pigeonVar_channelName = 'dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.requestNotificationAuth$pigeonVar_messageChannelSuffix'; 427 final String pigeonVar_channelName = 'dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.requestNotificationAuth$pigeonVar_messageChannelSuffix';
@@ -115,6 +115,9 @@ abstract class PlatformHostApi { @@ -115,6 +115,9 @@ abstract class PlatformHostApi {
115 /// 返回完整的 User-Agent 字符串,由 native 侧组装: 115 /// 返回完整的 User-Agent 字符串,由 native 侧组装:
116 /// `{systemWebViewUA} {appName}/{versionCode}({versionName})({manufacturer}##{brand}##{model}; {OS}{osVersion}; {height}x{width})(huawei)` 116 /// `{systemWebViewUA} {appName}/{versionCode}({versionName})({manufacturer}##{brand}##{model}; {OS}{osVersion}; {height}x{width})(huawei)`
117 String getFullUserAgent(); 117 String getFullUserAgent();
  118 +
  119 + /// 是否是中国大陆地区
  120 + bool isChinaRegion();
118 121
119 /// 申请通知权限 122 /// 申请通知权限
120 @async 123 @async