Skip to content

Commit

Permalink
PrefUI // Add a button for importing KeyKey user data.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen committed Nov 1, 2023
1 parent b0c5ba0 commit f26025f
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CocoaExtension
import Shared
import SwiftExtension
import SwiftUI
import UniformTypeIdentifiers

@available(macOS 13, *)
public struct VwrSettingsPaneDictionary: View {
Expand Down Expand Up @@ -45,6 +46,8 @@ public struct VwrSettingsPaneDictionary: View {

// MARK: - Main View

@State var keykeyImportButtonDisabled = false

private var fdrUserDataDefault: String { LMMgr.dataFolderPath(isDefaultFolder: true) }

private static let dlgOpenPath = NSOpenPanel()
Expand Down Expand Up @@ -229,6 +232,36 @@ public struct VwrSettingsPaneDictionary: View {
)
.settingsDescription()
}
} footer: {
HStack {
Spacer()
Button {
Self.dlgOpenFile.title = NSLocalizedString(
"i18n:settings.importFromKimoTxt.buttonText", comment: ""
) + ":"
Self.dlgOpenFile.showsResizeIndicator = true
Self.dlgOpenFile.showsHiddenFiles = true
Self.dlgOpenFile.canChooseFiles = true
Self.dlgOpenFile.allowsMultipleSelection = false
Self.dlgOpenFile.canChooseDirectories = false
Self.dlgOpenFile.allowedContentTypes = [.init(filenameExtension: "txt")].compactMap { $0 }

if let window = CtlSettingsUI.shared?.window {
Self.dlgOpenFile.beginSheetModal(for: window) { result in
if result == NSApplication.ModalResponse.OK {
keykeyImportButtonDisabled = true
defer { keykeyImportButtonDisabled = false }
guard let url = Self.dlgOpenFile.url else { return }
guard var rawString = try? String(contentsOf: url) else { return }
let count = LMMgr.importYahooKeyKeyUserDictionary(text: &rawString)
window.callAlert(title: String(format: "i18n:settings.importFromKimoTxt.finishedCount:%@".localized, count.description))
}
}
}
} label: {
Text(verbatim: "i18n:settings.importFromKimoTxt.buttonText".localized + " (TXT)…")
}.disabled(keykeyImportButtonDisabled)
}
}
}.formStyled().frame(minWidth: CtlSettingsUI.formWidth, maxWidth: ceil(CtlSettingsUI.formWidth * 1.2))
}
Expand Down

0 comments on commit f26025f

Please sign in to comment.