Skip to content

Commit

Permalink
Merge pull request #326 from ensan-hcl/refactoring/use_parameterized_…
Browse files Browse the repository at this point in the history
…any_p

[Refactoring] `any P<T>`がiOS 16で利用可能になるので、導入する
  • Loading branch information
ensan-hcl authored Oct 8, 2023
2 parents 0e90609 + cad557c commit f4d8b84
Show file tree
Hide file tree
Showing 32 changed files with 277 additions and 281 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fileprivate extension CustardInterface {
}
}

@MainActor func flickKeyModels<Extension: ApplicationSpecificKeyboardViewExtension>(extension _: Extension.Type) -> [KeyPosition: (model: any FlickKeyModelProtocol, width: Int, height: Int)] {
@MainActor func flickKeyModels<Extension: ApplicationSpecificKeyboardViewExtension>(extension _: Extension.Type) -> [KeyPosition: (model: any FlickKeyModelProtocol<Extension>, width: Int, height: Int)] {
self.keys.reduce(into: [:]) {dictionary, value in
switch value.key {
case let .gridFit(data):
Expand All @@ -71,7 +71,7 @@ fileprivate extension CustardInterface {
}
}

@MainActor func qwertyKeyModels<Extension: ApplicationSpecificKeyboardViewExtension>(extension _: Extension.Type) -> [KeyPosition: (model: any QwertyKeyModelProtocol, sizeType: QwertyKeySizeType)] {
@MainActor func qwertyKeyModels<Extension: ApplicationSpecificKeyboardViewExtension>(extension _: Extension.Type) -> [KeyPosition: (model: any QwertyKeyModelProtocol<Extension>, sizeType: QwertyKeySizeType)] {
self.keys.reduce(into: [:]) {dictionary, value in
switch value.key {
case let .gridFit(data):
Expand Down Expand Up @@ -126,28 +126,28 @@ fileprivate extension CustardKeyDesign.ColorType {
}

extension CustardInterfaceKey {
@MainActor public func flickKeyModel<Extension: ApplicationSpecificKeyboardViewExtension>(extension _: Extension.Type) -> any FlickKeyModelProtocol {
@MainActor public func flickKeyModel<Extension: ApplicationSpecificKeyboardViewExtension>(extension _: Extension.Type) -> any FlickKeyModelProtocol<Extension> {
switch self {
case let .system(value):
switch value {
case .changeKeyboard:
return FlickChangeKeyboardModel<Extension>.shared
return FlickChangeKeyboardModel.shared
case .enter:
return FlickEnterKeyModel<Extension>()
return FlickEnterKeyModel()
case .upperLower:
return FlickAaKeyModel<Extension>()
return FlickAaKeyModel()
case .nextCandidate:
return FlickNextCandidateKeyModel<Extension>.shared
return FlickNextCandidateKeyModel.shared
case .flickKogaki:
return FlickKogakiKeyModel<Extension>.shared
return FlickKogakiKeyModel.shared
case .flickKutoten:
return FlickKanaSymbolsKeyModel<Extension>.shared
return FlickKanaSymbolsKeyModel.shared
case .flickHiraTab:
return FlickTabKeyModel<Extension>.hiraTabKeyModel()
return FlickTabKeyModel.hiraTabKeyModel()
case .flickAbcTab:
return FlickTabKeyModel<Extension>.abcTabKeyModel()
return FlickTabKeyModel.abcTabKeyModel()
case .flickStar123Tab:
return FlickTabKeyModel<Extension>.numberTabKeyModel()
return FlickTabKeyModel.numberTabKeyModel()
}
case let .custom(value):
let flickKeyModels: [FlickDirection: FlickedKeyModel] = value.variations.reduce(into: [:]) {dictionary, variation in
Expand All @@ -162,24 +162,23 @@ extension CustardInterfaceKey {
break
}
}
let model = FlickKeyModel<Extension>(
return FlickKeyModel(
labelType: value.design.label.keyLabelType,
pressActions: value.press_actions.map {$0.actionType},
longPressActions: value.longpress_actions.longpressActionType,
flickKeys: flickKeyModels,
needSuggestView: value.longpress_actions == .none && !value.variations.isEmpty,
keycolorType: value.design.color.flickKeyColorType
)
return model
}
}

private func convertToQwertyKeyModel<Extension: ApplicationSpecificKeyboardViewExtension>(customKey: KeyFlickSetting.SettingData, extension _: Extension.Type) -> any QwertyKeyModelProtocol {
private func convertToQwertyKeyModel<Extension: ApplicationSpecificKeyboardViewExtension>(customKey: KeyFlickSetting.SettingData, extension _: Extension.Type) -> any QwertyKeyModelProtocol<Extension> {
let variations = VariationsModel([customKey.flick[.left], customKey.flick[.top], customKey.flick[.right], customKey.flick[.bottom]].compactMap {$0}.map {(label: $0.labelType, actions: $0.pressActions)})
return QwertyKeyModel<Extension>(labelType: customKey.labelType, pressActions: customKey.actions, longPressActions: customKey.longpressActions, variationsModel: variations, keyColorType: .normal, needSuggestView: false, for: (1, 1))
return QwertyKeyModel(labelType: customKey.labelType, pressActions: customKey.actions, longPressActions: customKey.longpressActions, variationsModel: variations, keyColorType: .normal, needSuggestView: false, for: (1, 1))
}

@MainActor func qwertyKeyModel<Extension: ApplicationSpecificKeyboardViewExtension>(layout: CustardInterfaceLayout, extension: Extension.Type) -> any QwertyKeyModelProtocol {
@MainActor func qwertyKeyModel<Extension: ApplicationSpecificKeyboardViewExtension>(layout: CustardInterfaceLayout, extension: Extension.Type) -> any QwertyKeyModelProtocol<Extension> {
switch self {
case let .system(value):
switch value {
Expand All @@ -192,11 +191,11 @@ extension CustardInterfaceKey {
}
return changeKeyboardKey
case .enter:
return QwertyEnterKeyModel<Extension>(keySizeType: .enter)
return QwertyEnterKeyModel(keySizeType: .enter)
case .upperLower:
return QwertyAaKeyModel<Extension>()
return QwertyAaKeyModel()
case .nextCandidate:
return QwertyNextCandidateKeyModel<Extension>()
return QwertyNextCandidateKeyModel()
case .flickKogaki:
return convertToQwertyKeyModel(customKey: Extension.SettingProvider.koganaFlickCustomKey.compiled(), extension: Extension.self)
case .flickKutoten:
Expand All @@ -218,7 +217,7 @@ extension CustardInterfaceKey {
}
}

let model = QwertyKeyModel<Extension>(
return QwertyKeyModel(
labelType: value.design.label.keyLabelType,
pressActions: value.press_actions.map {$0.actionType},
longPressActions: value.longpress_actions.longpressActionType,
Expand All @@ -227,35 +226,34 @@ extension CustardInterfaceKey {
needSuggestView: value.longpress_actions == .none,
for: (1, 1)
)
return model
}
}

func simpleKeyModel<Extension: ApplicationSpecificKeyboardViewExtension>(extension _: Extension.Type) -> any SimpleKeyModelProtocol {
func simpleKeyModel<Extension: ApplicationSpecificKeyboardViewExtension>(extension _: Extension.Type) -> any SimpleKeyModelProtocol<Extension> {
switch self {
case let .system(value):
switch value {
case .changeKeyboard:
return SimpleChangeKeyboardKeyModel<Extension>()
return SimpleChangeKeyboardKeyModel()
case .enter:
return SimpleEnterKeyModel<Extension>()
return SimpleEnterKeyModel()
case .upperLower:
return SimpleKeyModel<Extension>(keyLabelType: .text("a/A"), unpressedKeyColorType: .special, pressActions: [.changeCharacterType])
return SimpleKeyModel(keyLabelType: .text("a/A"), unpressedKeyColorType: .special, pressActions: [.changeCharacterType])
case .nextCandidate:
return SimpleNextCandidateKeyModel<Extension>()
return SimpleNextCandidateKeyModel()
case .flickKogaki:
return SimpleKeyModel<Extension>(keyLabelType: .text("小゙゚"), unpressedKeyColorType: .special, pressActions: [.changeCharacterType])
return SimpleKeyModel(keyLabelType: .text("小゙゚"), unpressedKeyColorType: .special, pressActions: [.changeCharacterType])
case .flickKutoten:
return SimpleKeyModel<Extension>(keyLabelType: .text(""), unpressedKeyColorType: .normal, pressActions: [.input("")])
return SimpleKeyModel(keyLabelType: .text(""), unpressedKeyColorType: .normal, pressActions: [.input("")])
case .flickHiraTab:
return SimpleKeyModel<Extension>(keyLabelType: .text("あいう"), unpressedKeyColorType: .special, pressActions: [.moveTab(.system(.user_japanese))])
return SimpleKeyModel(keyLabelType: .text("あいう"), unpressedKeyColorType: .special, pressActions: [.moveTab(.system(.user_japanese))])
case .flickAbcTab:
return SimpleKeyModel<Extension>(keyLabelType: .text("abc"), unpressedKeyColorType: .special, pressActions: [.moveTab(.system(.user_english))])
return SimpleKeyModel(keyLabelType: .text("abc"), unpressedKeyColorType: .special, pressActions: [.moveTab(.system(.user_english))])
case .flickStar123Tab:
return SimpleKeyModel<Extension>(keyLabelType: .text("☆123"), unpressedKeyColorType: .special, pressActions: [.moveTab(.system(.flick_numbersymbols))])
return SimpleKeyModel(keyLabelType: .text("☆123"), unpressedKeyColorType: .special, pressActions: [.moveTab(.system(.flick_numbersymbols))])
}
case let .custom(value):
return SimpleKeyModel<Extension>(
return SimpleKeyModel(
keyLabelType: value.design.label.keyLabelType,
unpressedKeyColorType: value.design.color.simpleKeyColorType,
pressActions: value.press_actions.map {$0.actionType},
Expand Down Expand Up @@ -337,15 +335,15 @@ struct CustomKeyboardView<Extension: ApplicationSpecificKeyboardViewExtension>:
public struct CustardFlickKeysView<Extension: ApplicationSpecificKeyboardViewExtension, Content: View>: View {
@State private var suggestState = FlickSuggestState()

public init(models: [KeyPosition: (model: any FlickKeyModelProtocol, width: Int, height: Int)], tabDesign: TabDependentDesign, layout: CustardInterfaceLayoutGridValue, @ViewBuilder generator: @escaping (FlickKeyView<Extension>, Int, Int) -> (Content)) {
public init(models: [KeyPosition: (model: any FlickKeyModelProtocol<Extension>, width: Int, height: Int)], tabDesign: TabDependentDesign, layout: CustardInterfaceLayoutGridValue, @ViewBuilder generator: @escaping (FlickKeyView<Extension>, Int, Int) -> (Content)) {
self.models = models
self.tabDesign = tabDesign
self.layout = layout
self.contentGenerator = generator
}

private let contentGenerator: (FlickKeyView<Extension>, Int, Int) -> (Content)
private let models: [KeyPosition: (model: any FlickKeyModelProtocol, width: Int, height: Int)]
private let models: [KeyPosition: (model: any FlickKeyModelProtocol<Extension>, width: Int, height: Int)]
private let tabDesign: TabDependentDesign
private let layout: CustardInterfaceLayoutGridValue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ enum SimpleUnpressedKeyColorType: UInt8 {
}
}

protocol SimpleKeyModelProtocol {
protocol SimpleKeyModelProtocol<Extension> {
associatedtype Extension: ApplicationSpecificKeyboardViewExtension

var unpressedKeyColorType: SimpleUnpressedKeyColorType {get}
@MainActor func pressActions(variableStates: VariableStates) -> [ActionType]
@MainActor func longPressActions(variableStates: VariableStates) -> LongpressActionType
@MainActor func feedback(variableStates: VariableStates)
@MainActor func label<Extension: ApplicationSpecificKeyboardViewExtension>(width: CGFloat, states: VariableStates, theme: Extension.Theme) -> KeyLabel<Extension>
@MainActor func label(width: CGFloat, states: VariableStates) -> KeyLabel<Extension>
@MainActor func backGroundColorWhenPressed(theme: Extension.Theme) -> Color
/// `pressActions`とは別に、押された際に発火する操作
/// - note: タブ固有の事情で実行しなければならないような処理に利用すること
Expand Down Expand Up @@ -82,7 +82,7 @@ struct SimpleKeyModel<Extension: ApplicationSpecificKeyboardViewExtension>: Simp
private let pressActions: [ActionType]
let longPressActions: LongpressActionType

func label<E: ApplicationSpecificKeyboardViewExtension>(width: CGFloat, states: VariableStates, theme: ThemeData<some ApplicationSpecificTheme>) -> KeyLabel<E> {
func label(width: CGFloat, states: VariableStates) -> KeyLabel<Extension> {
KeyLabel(self.keyLabelType, width: width)
}

Expand Down Expand Up @@ -115,7 +115,7 @@ struct SimpleEnterKeyModel<Extension: ApplicationSpecificKeyboardViewExtension>:
}

let unpressedKeyColorType: SimpleUnpressedKeyColorType = .enter
func label<E: ApplicationSpecificKeyboardViewExtension>(width: CGFloat, states: VariableStates, theme: ThemeData<some ApplicationSpecificTheme>) -> KeyLabel<E> {
func label(width: CGFloat, states: VariableStates) -> KeyLabel<Extension> {
let text = Design.language.getEnterKeyText(states.enterKeyState)
return KeyLabel(.text(text), width: width)
}
Expand Down Expand Up @@ -148,7 +148,7 @@ struct SimpleNextCandidateKeyModel<Extension: ApplicationSpecificKeyboardViewExt
}
}

func label<E: ApplicationSpecificKeyboardViewExtension>(width: CGFloat, states: VariableStates, theme: ThemeData<some ApplicationSpecificTheme>) -> KeyLabel<E> {
func label(width: CGFloat, states: VariableStates) -> KeyLabel<Extension> {
if states.resultModel.results.isEmpty {
KeyLabel(.text("空白"), width: width)
} else {
Expand Down Expand Up @@ -182,7 +182,7 @@ struct SimpleChangeKeyboardKeyModel<Extension: ApplicationSpecificKeyboardViewEx
.none
}

func label<E: ApplicationSpecificKeyboardViewExtension>(width: CGFloat, states: VariableStates, theme: ThemeData<some ApplicationSpecificTheme>) -> KeyLabel<E> {
func label(width: CGFloat, states: VariableStates) -> KeyLabel<Extension> {
if SemiStaticStates.shared.needsInputModeSwitchKey {
return KeyLabel(.changeKeyboard, width: width)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import SwiftUIUtils

@MainActor
struct SimpleKeyView<Extension: ApplicationSpecificKeyboardViewExtension>: View {
private let model: any SimpleKeyModelProtocol
private let model: any SimpleKeyModelProtocol<Extension>
@EnvironmentObject private var variableStates: VariableStates
@Environment(Extension.Theme.self) private var theme
@Environment(\.userActionManager) private var action

private let keyViewWidth: CGFloat
private let keyViewHeight: CGFloat

init(model: any SimpleKeyModelProtocol, tabDesign: TabDependentDesign) {
init(model: any SimpleKeyModelProtocol<Extension>, tabDesign: TabDependentDesign) {
self.model = model
self.keyViewWidth = tabDesign.keyViewWidth
self.keyViewHeight = tabDesign.keyViewHeight
}

init(model: any SimpleKeyModelProtocol, width: CGFloat, height: CGFloat) {
init(model: any SimpleKeyModelProtocol<Extension>, width: CGFloat, height: CGFloat) {
self.model = model
self.keyViewWidth = width
self.keyViewHeight = height
Expand All @@ -36,7 +36,7 @@ struct SimpleKeyView<Extension: ApplicationSpecificKeyboardViewExtension>: View
@State private var pressStartDate = Date()

private func label(width: CGFloat) -> some View {
model.label(width: keyViewWidth, states: variableStates, theme: theme) as KeyLabel<Extension>
model.label(width: keyViewWidth, states: variableStates)
}

var body: some View {
Expand Down
Loading

0 comments on commit f4d8b84

Please sign in to comment.