PlatformHostApiImpl.swift
14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import Foundation
import AuthenticationServices
import StoreKit
import UIKit
import WebKit
class PriceFormatter{
static func formatPrice(_ price: Decimal, currencyCode: String) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.currencyCode = currencyCode
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
return formatter.string(from: price as NSDecimalNumber) ?? "\(price)"
}
static func currencySymbol(for currencyCode: String) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.currencyCode = currencyCode
// formatter.locale = Locale(identifier: "zh_CN")
return formatter.currencySymbol
}
}
extension WatchAppOtherInfo{
func toParams() -> [String: Any]{
var json: [String: Any] = [:]
json["userId"] = userId
json["nickname"] = nickname
json["markName"] = markName
return json
}
}
/**
* PlatformApi iOS implementation.
*
* 组装完整 User-Agent,格式与 Android 端保持一致:
* `{systemWebViewUA} doublefeel/{versionCode}({versionName})(Apple##Apple##{model}; iOS{osVersion}; {height}x{width})(huawei)`
*/
final class PlatformHostApiImpl: PlatformHostApi {
func isDebugEnvoriment() throws -> Bool {
#if DEBUG || CI_ENV
return true
#else
return false
#endif
}
func nativeHandleUrl(urlString: String) throws -> Bool {
if let url = URL(string: urlString){
UIApplication.shared.open(url)
return true
}
return false
}
func requestUnhandedUrl() throws -> String? {
let unhandedUrl = AppShared.shared.unhandedUrl
AppShared.shared.unhandedUrl = nil
return unhandedUrl
}
func jumpAppSetting() throws -> Bool {
if let url = URL(string: UIApplication.openSettingsURLString){
UIApplication.shared.open(url)
return true
}
return false
}
func requestAppReview(completion: @escaping (Result<Bool, any Error>) -> Void) {
// Task { @MainActor in
// guard let windowScene = UIApplication.shared.connectedScenes
// .compactMap({ $0 as? UIWindowScene })
// .first(where: { $0.activationState == .foregroundActive }) else {
// completion(.success(false))
// return
// }
//
// // App Store controls whether the prompt is actually displayed.
// AppStore.requestReview(in: windowScene)
// completion(.success(true))
// }
let urlString = "itms-apps://itunes.apple.com/app/id\(kAppId)?action=write-review"
guard let url = URL(string: urlString) else{
return
}
if UIApplication.shared.canOpenURL(url){
UIApplication.shared.open(url)
}
}
func shareText(text: String, completion: @escaping (Result<Bool, any Error>) -> Void) {
presentShareSheet(items: [text], completion: completion)
}
func shareFileData(databytes: FlutterStandardTypedData, completion: @escaping (Result<Bool, any Error>) -> Void) {
let fileURL = FileManager.default.temporaryDirectory
.appendingPathComponent("doublefeel-share-\(UUID().uuidString)")
.appendingPathExtension("data")
do {
try databytes.data.write(to: fileURL, options: .atomic)
} catch {
print("[PlatformHostApiImpl.shareFileData] return error: \(error)")
completion(.failure(error))
return
}
presentShareSheet(items: [fileURL], cleanupURL: fileURL, completion: completion)
}
func uploadFile(filePath: String, resourceType: HResourceType, completion: @escaping (Result<String?, any Error>) -> Void) {
let obsResourceType: HuaweiOBSResourceType
switch resourceType {
case .image:
obsResourceType = .image
case .video:
obsResourceType = .video
}
Task {
do {
let fileURL = try await HuaweiOBSUploader.shared.upload(
filePath: filePath,
resourceType: obsResourceType
)
completion(.success(fileURL))
} catch {
DebugLogger.log(desc: "uploadFile error: \(error.localizedDescription)")
completion(.failure(error))
}
}
}
func performCropImage(imageUrl: String, maxKB: Int64?, width: Int64?, height: Int64?, completion: @escaping (Result<String?, any Error>) -> Void) {
}
// 登录
func updateLoginInfo(jsonString: String, baseUrl: String) throws {
if let data = jsonString.data(using: .utf8){
do{
let model = try JSONDecoder().decode(FlutterUserSummary.self, from: data)
if let token = model.accessToken{
// 刷新用户信息
let watchUser = UserInfoModel(userId: model.meUserInfo?.id, token: token, baseUrl: baseUrl)
do{
let data = try JSONEncoder().encode(watchUser)
AppGroupConstants.defaults?.set(data, forKey: AppGroupConstants.Key.myUserInfo)
AppShared.shared.login(userInfo: watchUser)
_ = WatchConnectivityService.shared.sendLoginInfoMessage(userInfo: watchUser)
}catch{}
Task{
await AppShared.shared.reportDeviceInfo()
if await HealthKitService.shared.hasAnyReadableData() {
_ = await AnchoredHealthDataUploader.shared.uploadAll()
}
}
}
}catch{
DebugLogger.log(desc: error.localizedDescription)
}
}
}
// 退出登录
func logout() throws {
// 清除所有数据
AppShared.shared.logout()
_ = WatchConnectivityService.shared.sendLoginInfoMessage(userInfo: nil)
}
// 刷新会员
func refreshVip() throws {
_ = WatchConnectivityService.shared.sendCommandMessage(AppGroupMessageKey.reloadVip)
}
// 刷新watch app 和表盘
func refreshWatchAppAndWidgets() throws {
_ = WatchConnectivityService.shared.sendCommandMessage(AppGroupMessageKey.reloadAll)
}
func performRestore(completion: @escaping (Result<Bool, any Error>) -> Void) {
Task{
let success = await AppShared.shared.payment.restore()
completion(.success(success))
}
}
func requestAppleProductInfo(productId: String, baseUnit: Int64, completion: @escaping (Result<AppleProductInfo?, any Error>) -> Void) {
Task {
do {
let product = try await AppShared.shared.payment.requestProducts(productId)
let isTrialPeriod = await AppShared.shared.payment.isFreeTrail(product: product)
let originPrice = product.price
var displayPrice = product.displayPrice
var price = originPrice
if let introductoryOffer = product.subscription?.introductoryOffer{
price = introductoryOffer.price
displayPrice = introductoryOffer.displayPrice
}else if let offer = product.subscription?.promotionalOffers.first(where: { $0.id == product.id }){
price = offer.price
displayPrice = offer.displayPrice
}
let unitPrice = originPrice/Decimal(baseUnit)
let currencyCode = product.priceFormatStyle.currencyCode
let productInfo = AppleProductInfo(
productId: product.id,
originPriceDescription: product.displayPrice,
priceDescription: displayPrice,
originPrice: NSDecimalNumber(decimal: originPrice * 100).doubleValue,
price: NSDecimalNumber(decimal: price * 100).doubleValue,
unitPrice: PriceFormatter.formatPrice(unitPrice, currencyCode: currencyCode),
currencyCode: PriceFormatter.currencySymbol(for: currencyCode),
isTrialPeriod: isTrialPeriod
)
print("[PlatformHostApiImpl.requestAppleProductInfo] return: \(productInfo)")
completion(.success(productInfo))
} catch StoreKitError.productNotFound {
print("[PlatformHostApiImpl.requestAppleProductInfo] return: nil")
completion(.success(nil))
} catch {
print("[PlatformHostApiImpl.requestAppleProductInfo] error: \(error.localizedDescription)")
completion(.failure(error))
}
}
}
func performApplePayment(productId: String, uuid: String, completion: @escaping (Result<AppleProductPaymentResult?, any Error>) -> Void) {
Task {
let result = await AppShared.shared.payment.purchase(productId, uuidString: uuid)
print("[PlatformHostApiImpl.performApplePayment] return: \(result)")
completion(.success(result))
}
}
private var appleSignInCoordinator: AppleSignInCoordinator?
// private var authorizationController: ASAuthorizationController?
func requestAppleSignIn(completion: @escaping (Result<AppleSignInModel?, any Error>) -> Void) {
DispatchQueue.main.async { [weak self] in
guard let self else {
print("[PlatformHostApiImpl.requestAppleSignIn] error: \(PlatformHostApiError.deallocated.localizedDescription)")
completion(.failure(PlatformHostApiError.deallocated))
return
}
let provider = ASAuthorizationAppleIDProvider()
let request = provider.createRequest()
request.requestedScopes = [.fullName, .email]
guard let presentationAnchor = AppleSignInCoordinator.currentPresentationAnchor() else {
print("[PlatformHostApiImpl.requestAppleSignIn] error: \(PlatformHostApiError.missingPresentationAnchor.localizedDescription)")
completion(.failure(PlatformHostApiError.missingPresentationAnchor))
return
}
let coordinator = AppleSignInCoordinator(presentationAnchor: presentationAnchor) { [weak self] result in
self?.appleSignInCoordinator = nil
switch result {
case .success(let model):
print("[PlatformHostApiImpl.requestAppleSignIn] return: \(String(describing: model))")
case .failure(let error):
print("[PlatformHostApiImpl.requestAppleSignIn] error: \(error.localizedDescription)")
}
completion(result)
}
appleSignInCoordinator = coordinator
let controller = ASAuthorizationController(authorizationRequests: [request])
controller.delegate = coordinator
controller.presentationContextProvider = coordinator
controller.performRequests()
// authorizationController = controller
}
}
func getFullUserAgent() throws -> String {
let userAgent = AppShared.shared.agent.finalUA
print("[PlatformHostApiImpl.getFullUserAgent] return: \(userAgent)")
return userAgent
}
}
//MARK: - 分享
extension PlatformHostApiImpl{
private func presentShareSheet(
items: [Any],
cleanupURL: URL? = nil,
completion: @escaping (Result<Bool, any Error>) -> Void
) {
DispatchQueue.main.async {
guard let viewController = Self.topViewController() else {
if let cleanupURL {
try? FileManager.default.removeItem(at: cleanupURL)
}
let error = NSError(
domain: "PlatformHostApiImpl.Share",
code: 1,
userInfo: [NSLocalizedDescriptionKey: "Unable to find a view controller for sharing."]
)
print("[PlatformHostApiImpl.share] return error: \(error)")
completion(.failure(error))
return
}
let activityController = UIActivityViewController(
activityItems: items,
applicationActivities: nil
)
activityController.popoverPresentationController?.sourceView = viewController.view
activityController.popoverPresentationController?.sourceRect = CGRect(
x: viewController.view.bounds.midX,
y: viewController.view.bounds.midY,
width: 1,
height: 1
)
activityController.completionWithItemsHandler = { _, completed, _, error in
if let cleanupURL {
try? FileManager.default.removeItem(at: cleanupURL)
}
if let error {
print("[PlatformHostApiImpl.share] return error: \(error)")
completion(.failure(error))
} else {
print("[PlatformHostApiImpl.share] return: \(completed)")
completion(.success(completed))
}
}
viewController.present(activityController, animated: true)
}
}
private static func topViewController(
from rootViewController: UIViewController? = UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.first(where: { $0.activationState == .foregroundActive })?
.windows
.first(where: { $0.isKeyWindow })?
.rootViewController
) -> UIViewController? {
if let presented = rootViewController?.presentedViewController {
return topViewController(from: presented)
}
if let navigationController = rootViewController as? UINavigationController {
return topViewController(from: navigationController.visibleViewController)
}
if let tabBarController = rootViewController as? UITabBarController {
return topViewController(from: tabBarController.selectedViewController)
}
return rootViewController
}
}