-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace full screen state view by in-row state view while running wat… (
- Loading branch information
Showing
5 changed files
with
154 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
Sources/Extensions/Watch/Home/Mocks/MockWatchHomeViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#if DEBUG | ||
import Foundation | ||
|
||
final class MockWatchHomeViewModel: WatchHomeViewModelProtocol { | ||
@Published var actions: [WatchActionItem] = [] | ||
@Published var state: WatchHomeViewState = .idle | ||
|
||
func runActionId(_ actionId: String, completion: @escaping (Bool) -> Void) {} | ||
|
||
func onAppear() { | ||
actions = [ | ||
.init( | ||
id: "1", | ||
name: "Hello", | ||
iconName: "ab_testing", | ||
backgroundColor: "#34eba8", | ||
iconColor: "#4479b3", | ||
textColor: "#4479b3" | ||
), | ||
] | ||
} | ||
|
||
func onDisappear() {} | ||
} | ||
#endif |
93 changes: 93 additions & 0 deletions
93
Sources/Extensions/Watch/Home/Views/WatchActionButtonView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import Shared | ||
import SwiftUI | ||
|
||
struct WatchActionButtonView<ViewModel>: View where ViewModel: WatchHomeViewModelProtocol { | ||
enum ActionState { | ||
case idle | ||
case loading | ||
case success | ||
case failure | ||
} | ||
|
||
@EnvironmentObject private var homeViewModel: ViewModel | ||
|
||
let action: WatchActionItem | ||
|
||
@State private var state: ActionState = .idle | ||
|
||
var body: some View { | ||
content | ||
.onChange(of: state) { newValue in | ||
// On watchOS 10 this can be replaced by '.sensoryFeedback' modifier | ||
let currentDevice = WKInterfaceDevice.current() | ||
switch newValue { | ||
case .success: | ||
currentDevice.play(.success) | ||
case .failure: | ||
currentDevice.play(.failure) | ||
case .loading: | ||
currentDevice.play(.click) | ||
default: | ||
break | ||
} | ||
} | ||
} | ||
|
||
@ViewBuilder | ||
private var content: some View { | ||
Button { | ||
state = .loading | ||
homeViewModel.runActionId(action.id) { success in | ||
state = success ? .success : .failure | ||
resetState() | ||
} | ||
} label: { | ||
HStack(spacing: Spaces.one) { | ||
iconToDisplay | ||
.animation(.easeInOut, value: state) | ||
Text(action.name) | ||
.foregroundStyle(Color(uiColor: .init(hex: action.textColor))) | ||
} | ||
} | ||
.disabled(state != .idle) | ||
.listRowBackground( | ||
Color(uiColor: .init(hex: action.backgroundColor)) | ||
.clipShape(RoundedRectangle(cornerRadius: 12)) | ||
) | ||
} | ||
|
||
private func resetState() { | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { | ||
state = .idle | ||
} | ||
} | ||
|
||
private var iconToDisplay: some View { | ||
VStack { | ||
switch state { | ||
case .idle: | ||
Image(uiImage: MaterialDesignIcons(named: action.iconName).image( | ||
ofSize: .init(width: 24, height: 24), | ||
color: .init(hex: action.iconColor) | ||
)) | ||
case .loading: | ||
ProgressView() | ||
.progressViewStyle(.circular) | ||
.frame(width: 24, height: 24) | ||
.tint(.black) | ||
.shadow(color: .white, radius: 10) | ||
case .success: | ||
makeActionImage(iconName: MaterialDesignIcons.checkIcon.name) | ||
case .failure: | ||
makeActionImage(iconName: MaterialDesignIcons.closeThickIcon.name) | ||
} | ||
} | ||
} | ||
|
||
private func makeActionImage(iconName: String) -> some View { | ||
Image(uiImage: MaterialDesignIcons(named: iconName).image( | ||
ofSize: .init(width: 24, height: 24), | ||
color: .init(hex: action.iconColor) | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters