Impl platform lock on ios

This commit is contained in:
Ming Ming 2023-08-29 21:37:13 +08:00
parent 6f0a2514b9
commit d338d42a75
9 changed files with 197 additions and 5 deletions

View file

@ -4,7 +4,7 @@
# This file should be version controlled.
version:
revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
revision: f72efea43c3013323d1b95cff571f3c1caa37583
channel: stable
project_type: plugin
@ -13,11 +13,14 @@ project_type: plugin
migration:
platforms:
- platform: root
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
create_revision: f72efea43c3013323d1b95cff571f3c1caa37583
base_revision: f72efea43c3013323d1b95cff571f3c1caa37583
- platform: android
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
create_revision: f72efea43c3013323d1b95cff571f3c1caa37583
base_revision: f72efea43c3013323d1b95cff571f3c1caa37583
- platform: ios
create_revision: f72efea43c3013323d1b95cff571f3c1caa37583
base_revision: f72efea43c3013323d1b95cff571f3c1caa37583
# User provided section

38
np_platform_lock/ios/.gitignore vendored Normal file
View file

@ -0,0 +1,38 @@
.idea/
.vagrant/
.sconsign.dblite
.svn/
.DS_Store
*.swp
profile
DerivedData/
build/
GeneratedPluginRegistrant.h
GeneratedPluginRegistrant.m
.generated/
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
!default.pbxuser
!default.mode1v3
!default.mode2v3
!default.perspectivev3
xcuserdata
*.moved-aside
*.pyc
*sync/
Icon?
.tags*
/Flutter/Generated.xcconfig
/Flutter/ephemeral/
/Flutter/flutter_export_environment.sh

View file

View file

@ -0,0 +1,5 @@
import Foundation
struct K {
static let libId = "com.nkming.nc_photos.np_platform_lock"
}

View file

@ -0,0 +1,73 @@
import Flutter
import Foundation
class LockChannelHandler {
func onMethodCall(call: FlutterMethodCall, result: FlutterResult) {
let args = call.arguments as? Dictionary<String, Any>
do {
switch call.method {
case "tryLock":
try tryLock(
lockId: (args?["lockId"] as? Int).unwrap(),
result: result
)
case "unlock":
try unlock(
lockId: (args?["lockId"] as? Int).unwrap(),
result: result
)
default:
result(FlutterMethodNotImplemented)
}
} catch let error as AppError {
result(
FlutterError(
code: "systemException",
message: error.message,
details: "\(error.stackTrace)"
)
)
} catch {
result(
FlutterError(
code: "systemException",
message: "\(error)",
details: nil
)
)
}
}
private func tryLock(lockId: Int, result: FlutterResult) {
if LockChannelHandler.locks[lockId] != true {
LockChannelHandler.locks[lockId] = true
lockedIds += [lockId]
result(true)
} else {
result(false)
}
}
private func unlock(lockId: Int, result: FlutterResult) {
if LockChannelHandler.locks[lockId] == true {
LockChannelHandler.locks[lockId] = false
lockedIds.removeAll(where: { $0 == lockId })
result(nil)
} else {
result(
FlutterError(
code: "notLockedException",
message: "Cannot unlock without first locking",
details: nil
)
)
}
}
private var lockedIds: [Int] = []
static let methodChannel = "\(K.libId)/lock"
private static var locks: [Int:Bool] = [:]
}

View file

@ -0,0 +1,13 @@
import Flutter
import UIKit
public class NpPlatformLockPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let lockHandler = LockChannelHandler()
let lockChannel = FlutterMethodChannel(
name: LockChannelHandler.methodChannel,
binaryMessenger: registrar.messenger()
)
lockChannel.setMethodCallHandler(lockHandler.onMethodCall)
}
}

View file

@ -0,0 +1,35 @@
import Foundation
class AppError: Error {
init(_ message: String? = nil, stackTrace: [String] = Thread.callStackSymbols) {
self.message_ = message
self.stackTrace = stackTrace.joined(separator: "\n")
}
var description: String {
return "\(message_ ?? "") (throw: \(String(describing: self))\nStack trace:\n\(stackTrace)"
}
var message: String {
return message_ == nil ? String(describing: self) : message_!
}
let stackTrace: String
private let message_: String?
}
class NilError: AppError {
}
extension Optional {
func unwrap(
_ errorBuilder: (() -> Error)? = nil,
file: String = #fileID,
line: Int = #line
) throws -> Wrapped {
guard let value = self else {
throw errorBuilder?() ?? NilError("\(type(of: self)) is nil in \(file):\(line)")
}
return value
}
}

View file

@ -0,0 +1,23 @@
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint np_platform_lock.podspec` to validate before publishing.
#
Pod::Spec.new do |s|
s.name = 'np_platform_lock'
s.version = '0.0.1'
s.summary = 'A new Flutter plugin project.'
s.description = <<-DESC
A new Flutter plugin project.
DESC
s.homepage = 'http://example.com'
s.license = { :file => '../LICENSE' }
s.author = { 'Your Company' => 'email@example.com' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
s.platform = :ios, '9.0'
# Flutter.framework does not contain a i386 slice.
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }
s.swift_version = '5.0'
end

View file

@ -25,3 +25,5 @@ flutter:
android:
package: com.nkming.nc_photos.np_platform_lock
pluginClass: NpPlatformLockPlugin
ios:
pluginClass: NpPlatformLockPlugin