diff --git a/Bark.xcodeproj/project.pbxproj b/Bark.xcodeproj/project.pbxproj index cdbb704..4769e2a 100644 --- a/Bark.xcodeproj/project.pbxproj +++ b/Bark.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ 06172FDA27F6DAEF002333A4 /* ServerListTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06172FD927F6DAEF002333A4 /* ServerListTableViewCell.swift */; }; 06172FDC27F6DB06002333A4 /* ServerListTableViewCellViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06172FDB27F6DB06002333A4 /* ServerListTableViewCellViewModel.swift */; }; 061894C529962EB900E001C2 /* GradientButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 061894C429962EB900E001C2 /* GradientButton.swift */; }; + 061894C729A75BEA00E001C2 /* Algorithm.swift in Sources */ = {isa = PBXBuildFile; fileRef = 061894C629A75BEA00E001C2 /* Algorithm.swift */; }; 0627DABB298B6EA2002F3F69 /* DropBoxView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0627DABA298B6EA2002F3F69 /* DropBoxView.swift */; }; 0627DABD2990D615002F3F69 /* BorderTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0627DABC2990D615002F3F69 /* BorderTextField.swift */; }; 062B98C3251B2762004562E7 /* BKButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 062B98C2251B2762004562E7 /* BKButton.swift */; }; @@ -192,6 +193,7 @@ 06172FD927F6DAEF002333A4 /* ServerListTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerListTableViewCell.swift; sourceTree = ""; }; 06172FDB27F6DB06002333A4 /* ServerListTableViewCellViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerListTableViewCellViewModel.swift; sourceTree = ""; }; 061894C429962EB900E001C2 /* GradientButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GradientButton.swift; sourceTree = ""; }; + 061894C629A75BEA00E001C2 /* Algorithm.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Algorithm.swift; sourceTree = ""; }; 0627DABA298B6EA2002F3F69 /* DropBoxView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DropBoxView.swift; sourceTree = ""; }; 0627DABC2990D615002F3F69 /* BorderTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BorderTextField.swift; sourceTree = ""; }; 062B98C2251B2762004562E7 /* BKButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BKButton.swift; sourceTree = ""; }; @@ -430,6 +432,7 @@ 06B1158E247BB1FB006D91FB /* Message.swift */, 064CABA5256BE9510018155C /* PreviewModel.swift */, 06BD4DA92901352E003364DB /* Object+Dictionary.swift */, + 061894C629A75BEA00E001C2 /* Algorithm.swift */, ); path = Model; sourceTree = ""; @@ -989,6 +992,7 @@ 06C5953124811392006B98F3 /* ArchiveSettingCell.swift in Sources */, 068EC15A27ED99E700D5D11E /* ServerListViewModel.swift in Sources */, 06172FDC27F6DB06002333A4 /* ServerListTableViewCellViewModel.swift in Sources */, + 061894C729A75BEA00E001C2 /* Algorithm.swift in Sources */, 065BE4502563D939002A8CA4 /* SoundCellViewModel.swift in Sources */, 06B1158F247BB1FB006D91FB /* Message.swift in Sources */, 06172FDA27F6DAEF002333A4 /* ServerListTableViewCell.swift in Sources */, diff --git a/Bark/en.lproj/Localizable.strings b/Bark/en.lproj/Localizable.strings index ad4521b..7fdbb77 100644 --- a/Bark/en.lproj/Localizable.strings +++ b/Bark/en.lproj/Localizable.strings @@ -124,8 +124,8 @@ import = "Import"; exportOrImport = "Export and import messages"; items = "messages"; -enterKey = "Please enter %s-bit Key"; -enterIv = "Please enter %s-bit Iv"; +enterKey = "Please enter %d-bit Key"; +enterIv = "Please enter 16-bit Iv"; encryptionSettings = "Encryption Settings"; algorithm = "Algorithm"; mode = "Mode"; diff --git a/Bark/zh-Hans.lproj/Localizable.strings b/Bark/zh-Hans.lproj/Localizable.strings index 78f537b..4f2049a 100644 --- a/Bark/zh-Hans.lproj/Localizable.strings +++ b/Bark/zh-Hans.lproj/Localizable.strings @@ -127,7 +127,7 @@ import = "导入"; exportOrImport = "导出或导入消息列表"; items = "条消息"; -enterKey = "请输入%s位Key"; +enterKey = "请输入%d位Key"; enterIv = "请输入16位Iv"; encryptionSettings = "加密设置"; algorithm = "算法"; diff --git a/Controller/CryptoSettingController.swift b/Controller/CryptoSettingController.swift index 8843c17..8a10801 100644 --- a/Controller/CryptoSettingController.swift +++ b/Controller/CryptoSettingController.swift @@ -7,6 +7,7 @@ // import UIKit +import RxSwift class CryptoSettingController: BaseViewController { let algorithmFeild = DropBoxView(values: ["AES128", "AES192", "AES256"]) @@ -180,4 +181,32 @@ class CryptoSettingController: BaseViewController { super.viewDidLoad() self.view.backgroundColor = BKColor.white } + + override func bindViewModel() { + + let output = viewModel.transform(input: CryptoSettingModel.Input( + algorithmChanged: self.algorithmFeild + .rx + .currentValueChanged + .compactMap{$0} + .asDriver(onErrorDriveWith: .empty()) + )) + + output.algorithmList + .map{$0.map {$0.rawValue}} + .drive(self.algorithmFeild.rx.values) + .disposed(by: rx.disposeBag) + + output.modeList + .drive(self.modeFeild.rx.values) + .disposed(by: rx.disposeBag) + + output.paddingList + .drive(self.paddingField.rx.values) + .disposed(by: rx.disposeBag) + + output.keyLenght.drive(onNext: {[weak self] keyLenght in + self?.keyTextField.placeholder = String(format: NSLocalizedString("enterKey"), keyLenght) + }).disposed(by: rx.disposeBag) + } } diff --git a/Controller/CryptoSettingModel.swift b/Controller/CryptoSettingModel.swift index 485cead..843494c 100644 --- a/Controller/CryptoSettingModel.swift +++ b/Controller/CryptoSettingModel.swift @@ -8,13 +8,38 @@ import CryptoSwift import Foundation -class CryptoSettingModel: ViewModel, ViewModelType { - struct Input {} +import RxCocoa +import RxSwift - struct Output {} +class CryptoSettingModel: ViewModel, ViewModelType { + struct Input { + let algorithmChanged: Driver + } + + struct Output { + let algorithmList: Driver<[Algorithm]> + let modeList: Driver<[String]> + let paddingList: Driver<[String]> + let keyLenght: Driver + } func transform(input: Input) -> Output { - return Output() + let modeList = input + .algorithmChanged + .compactMap { Algorithm(rawValue: $0) } + .map { $0.modes } + + let keyLenght = input + .algorithmChanged + .compactMap { Algorithm(rawValue: $0) } + .map { $0.keyLenght } + + return Output( + algorithmList: Driver.just([Algorithm.aes128, Algorithm.aes192, Algorithm.aes256]), + modeList: Driver.merge(Driver.just(["CBC", "ECB", "GCM"]), modeList), + paddingList: Driver.just(["pkcs7"]), + keyLenght: Driver.merge(Driver.just(16), keyLenght) + ) } override init() { diff --git a/Model/Algorithm.swift b/Model/Algorithm.swift new file mode 100644 index 0000000..3205221 --- /dev/null +++ b/Model/Algorithm.swift @@ -0,0 +1,40 @@ +// +// Algorithm.swift +// Bark +// +// Created by huangfeng on 2023/2/23. +// Copyright © 2023 Fin. All rights reserved. +// + +import Foundation + +enum Algorithm: String { + case aes128 = "AES128" + case aes192 = "AES192" + case aes256 = "AES256" + + var modes: [String] { + switch self { + case .aes128, .aes192, .aes256: + return ["CBC", "ECB", "GCM"] + } + } + + var paddings: [String] { + switch self { + case .aes128, .aes192, .aes256: + return ["pkcs7"] + } + } + + var keyLenght: Int { + switch self { + case .aes128: + return 16 + case .aes192: + return 24 + case .aes256: + return 32 + } + } +} diff --git a/View/DropBoxView.swift b/View/DropBoxView.swift index 17f363c..a259d49 100644 --- a/View/DropBoxView.swift +++ b/View/DropBoxView.swift @@ -8,6 +8,8 @@ import DropDown import UIKit +import RxCocoa +import RxSwift class DropBoxView: UIView { @@ -42,12 +44,20 @@ class DropBoxView: UIView { } } - let values: [String] + var values: [String] { + didSet { + self.currentValue = values.first + } + } + var currentValue: String? { didSet { self.valueLabel.text = currentValue + self.currentValueChanged?(self.currentValue) } } + + var currentValueChanged: ((String?) -> Void)? init(values: [String]) { self.values = values @@ -107,3 +117,21 @@ class DropBoxView: UIView { dropDown.show() } } + +extension Reactive where Base: DropBoxView { + + var currentValueChanged: ControlEvent { + let source = Observable.create { [weak control = self.base] observer -> Disposable in + MainScheduler.ensureExecutingOnScheduler() + guard let control = control else { + observer.onCompleted() + return Disposables.create() + } + control.currentValueChanged = { value in + observer.onNext(value) + } + return Disposables.create() + } + return ControlEvent(events: source) + } +}