Skip to content

Commit

Permalink
Adds swipe action support.
Browse files Browse the repository at this point in the history
  • Loading branch information
mntone committed Jan 4, 2024
1 parent 8e03e53 commit cca15e8
Show file tree
Hide file tree
Showing 14 changed files with 276 additions and 29 deletions.
116 changes: 107 additions & 9 deletions src/App/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
}
},
"0" : {
"comment" : "Effective/None",
"comment" : "Effective/Invalid",
"localizations" : {
"ar" : {
"stringUnit" : {
Expand Down Expand Up @@ -4337,6 +4337,23 @@
}
}
},
"Favorite" : {
"comment" : "SwipeActions/Favorite",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Favorite"
}
},
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "お気に入り"
}
}
}
},
"Favorited" : {
"localizations" : {
"en" : {
Expand Down Expand Up @@ -5411,6 +5428,23 @@
}
}
},
"Invalid" : {
"comment" : "Effective/Invalid (Accessibility)",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "None"
}
},
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "効果なし"
}
}
}
},
"keyword.element.dragon" : {
"extractionState" : "manual",
"localizations" : {
Expand Down Expand Up @@ -5496,6 +5530,22 @@
}
}
},
"Left Swipe" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Left Swipe"
}
},
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "左にスワイプ"
}
}
}
},
"Leviathan" : {
"comment" : "text.type[leviathan]",
"extractionState" : "manual",
Expand Down Expand Up @@ -5709,18 +5759,18 @@
}
}
},
"Merge parts with the same status" : {
"Merge Parts with Same Physiology*" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Merge parts with the same status"
"value" : "Merge Parts with Same Physiology*"
}
},
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "同じステータスを持つ部位を結合する"
"value" : "同じ肉質を持つ部位を結合する※"
}
}
}
Expand Down Expand Up @@ -5758,6 +5808,38 @@
}
}
},
"Monster Detail" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Monster Detail"
}
},
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "モンスター詳細"
}
}
}
},
"Monster List" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Monster List"
}
},
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "モンスターリスト"
}
}
}
},
"Name" : {
"comment" : "Sort/Name",
"localizations" : {
Expand Down Expand Up @@ -5842,7 +5924,7 @@
}
},
"None" : {
"comment" : "Effective/None (Accessibility)",
"comment" : "SwipeActions/None",
"localizations" : {
"en" : {
"stringUnit" : {
Expand All @@ -5853,7 +5935,7 @@
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "効果なし"
"value" : "なし"
}
}
}
Expand Down Expand Up @@ -7194,6 +7276,22 @@
}
}
},
"Show Element Attack" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Show Element Attack"
}
},
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "属性攻撃を表示する"
}
}
}
},
"Show Monster’s Title" : {
"localizations" : {
"en" : {
Expand Down Expand Up @@ -8006,18 +8104,18 @@
}
}
},
"The settings will take effect when you restart the app." : {
"The asterisk (*) settings will take effect when you restart the app." : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "The settings will take effect when you restart the app."
"value" : "The asterisk (*) settings will take effect when you restart the app."
}
},
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "設定を反映するには、アプリを再起動する必要があります"
"value" : "※印の設定を反映するには、アプリを再起動する必要があります"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/App/MAApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct MAApp: SwiftUI.App {
WindowGroup {
ContentView(viewModel: viewModel)
.environmentObject(viewModel)
.environment(\.settings, viewModel.app.settings)
}
#if os(watchOS)
.environment(\.watchMetrics, WatchUtil.getMetrics())
Expand Down
10 changes: 5 additions & 5 deletions src/App/ViewModels/Enums/Effective.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ enum Effective: Int8 {
case high = 20
case middle = 10
case low = 5
case none = 0
case invalid = 0

var label: String {
switch self {
Expand All @@ -14,8 +14,8 @@ enum Effective: Int8 {
String(localized: "++", comment: "Effective/Middle")
case .low:
String(localized: "+", comment: "Effective/Low")
case .none:
String(localized: "0", comment: "Effective/None")
case .invalid:
String(localized: "0", comment: "Effective/Invalid")
}
}

Expand All @@ -27,8 +27,8 @@ enum Effective: Int8 {
String(localized: "Middle", comment: "Effective/Middle (Accessibility)")
case .low:
String(localized: "Low", comment: "Effective/Low (Accessibility)")
case .none:
String(localized: "None", comment: "Effective/None (Accessibility)")
case .invalid:
String(localized: "Invalid", comment: "Effective/Invalid (Accessibility)")
}
}
}
2 changes: 1 addition & 1 deletion src/App/ViewModels/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct HomeItemViewModel: Identifiable, Hashable {
}

final class HomeViewModel: ObservableObject {
private let app: App
let app: App

@Published
private(set) var state: StarSwingsState<[HomeItemViewModel]> = .ready
Expand Down
15 changes: 15 additions & 0 deletions src/App/ViewModels/SettingsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ final class SettingsViewModel: ObservableObject {
}
#endif

#if !os(macOS)
@Published
var trailingSwipeAction: SwipeAction {
didSet {
settings.trailingSwipeAction = trailingSwipeAction
}
}
#endif

#if !os(watchOS)
@Published
var includesFavoriteGroupInSearchResult: Bool {
Expand Down Expand Up @@ -58,6 +67,9 @@ final class SettingsViewModel: ObservableObject {
self.rootViewModel = rootViewModel
self.settings = app.settings
self.storage = storage
#if !os(macOS)
self.trailingSwipeAction = app.settings.trailingSwipeAction
#endif
#if !os(watchOS)
self.includesFavoriteGroupInSearchResult = app.settings.includesFavoriteGroupInSearchResult
#endif
Expand All @@ -68,6 +80,9 @@ final class SettingsViewModel: ObservableObject {
self.mergeParts = app.settings.mergeParts

let scheduler = DispatchQueue.main
#if !os(macOS)
settings.$trailingSwipeAction.dropFirst().receive(on: scheduler).assign(to: &$trailingSwipeAction)
#endif
#if !os(watchOS)
settings.$includesFavoriteGroupInSearchResult.dropFirst().receive(on: scheduler).assign(to: &$includesFavoriteGroupInSearchResult)
#endif
Expand Down
23 changes: 23 additions & 0 deletions src/App/ViewModels/Utils/SwipeAction+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import MonsterAnalyzerCore

extension SwipeAction {
var label: String {
switch self {
case .none:
String(localized: "None", comment: "SwipeActions/None")
case .favorite:
String(localized: "Favorite", comment: "SwipeActions/Favorite")
}
}
}

// MARK: - Identifiable

extension SwipeAction: Identifiable {
public var id: String {
@inline(__always)
get {
rawValue
}
}
}
6 changes: 3 additions & 3 deletions src/App/ViewModels/WeaknessViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct WeaknessItemViewModel: Identifiable {
self.effective = .middle
}
} else if value <= 0 {
self.effective = .none
self.effective = .invalid
} else {
self.effective = .low
}
Expand All @@ -37,7 +37,7 @@ struct WeaknessItemViewModel: Identifiable {
switch effective {
case .high, .middle:
return .accentColor
case .low, .none:
case .low, .invalid:
return .secondary
}
}
Expand All @@ -46,7 +46,7 @@ struct WeaknessItemViewModel: Identifiable {
switch effective {
case .high, .middle:
return .bold
case .low, .none:
case .low, .invalid:
return .semibold
}
}
Expand Down
45 changes: 45 additions & 0 deletions src/App/Views/FavoriteSwipeButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import SwiftUI

#if !os(macOS)

@available(macOS, unavailable)
struct FavoriteSwipeButton: View {
@Binding
private(set) var favorite: Bool

var body: some View {
let text: LocalizedStringKey, image: String
if favorite {
text = "Remove Favorite"
image = "star.slash.fill"
} else {
text = "Add to Favorites"
image = "star.fill"
}

return Button(text, systemImage: image) {
favorite.toggle()
}
.tint(.yellow)
}
}

#Preview("Favorite") {
List {
Text(verbatim: "Sample Context Menu")
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
FavoriteSwipeButton(favorite: .constant(false))
}
}
}

#Preview("Unfavorite") {
List {
Text(verbatim: "Sample Context Menu")
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
FavoriteSwipeButton(favorite: .constant(true))
}
}
}

#endif
Loading

0 comments on commit cca15e8

Please sign in to comment.