2023-10-26 11:05:42 +02:00
|
|
|
//
|
2024-09-06 16:34:30 +03:00
|
|
|
// Copyright 2023, 2024 New Vector Ltd.
|
2023-10-26 11:05:42 +02:00
|
|
|
//
|
2025-01-06 11:27:37 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
// Please see LICENSE files in the repository root for full details.
|
2023-10-26 11:05:42 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct VoiceMessageButton: View {
|
|
|
|
@ScaledMetric private var buttonSize: CGFloat
|
|
|
|
|
|
|
|
enum State {
|
|
|
|
case loading
|
|
|
|
case playing
|
|
|
|
case paused
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Size {
|
|
|
|
case small
|
|
|
|
case medium
|
|
|
|
}
|
|
|
|
|
|
|
|
let state: State
|
|
|
|
let action: () -> Void
|
|
|
|
|
|
|
|
init(state: State, size: Size, action: @escaping () -> Void) {
|
|
|
|
switch size {
|
|
|
|
case .small:
|
|
|
|
_buttonSize = .init(wrappedValue: 30)
|
|
|
|
case .medium:
|
|
|
|
_buttonSize = .init(wrappedValue: 36)
|
|
|
|
}
|
|
|
|
|
|
|
|
self.state = state
|
|
|
|
self.action = action
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Button(action: action) {
|
|
|
|
buttonLabel
|
|
|
|
.frame(width: buttonSize, height: buttonSize)
|
|
|
|
}
|
|
|
|
.animation(nil, value: state)
|
2023-11-01 10:03:35 +01:00
|
|
|
.buttonStyle(VoiceMessageButtonStyle())
|
2023-10-26 11:05:42 +02:00
|
|
|
.disabled(state == .loading)
|
2023-11-03 16:04:03 +01:00
|
|
|
.accessibilityLabel(accessibilityLabel)
|
2023-10-26 11:05:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
private var buttonLabel: some View {
|
|
|
|
switch state {
|
|
|
|
case .loading:
|
|
|
|
ProgressView()
|
|
|
|
case .playing, .paused:
|
|
|
|
let imageAsset = state == .playing ? Asset.Images.mediaPause : Asset.Images.mediaPlay
|
|
|
|
let offset: CGFloat = state == .playing ? 0 : 2
|
|
|
|
|
|
|
|
Image(asset: imageAsset)
|
|
|
|
.resizable()
|
|
|
|
.scaledToFit()
|
2023-11-14 12:20:34 +01:00
|
|
|
.scaledFrame(width: 12, height: 14)
|
2023-10-26 11:05:42 +02:00
|
|
|
.offset(x: offset)
|
2023-11-03 16:04:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var accessibilityLabel: String {
|
|
|
|
switch state {
|
|
|
|
case .loading:
|
|
|
|
return ""
|
|
|
|
case .playing:
|
|
|
|
return L10n.a11yPause
|
|
|
|
case .paused:
|
|
|
|
return L10n.a11yPlay
|
2023-10-26 11:05:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-01 10:03:35 +01:00
|
|
|
private struct VoiceMessageButtonStyle: ButtonStyle {
|
|
|
|
@Environment(\.isEnabled) var isEnabled: Bool
|
|
|
|
|
|
|
|
func makeBody(configuration: Configuration) -> some View {
|
|
|
|
configuration.label
|
|
|
|
.foregroundColor(isEnabled ? .compound.textSecondary.opacity(configuration.isPressed ? 0.6 : 1) : .compound.iconDisabled)
|
2023-11-28 10:21:42 +01:00
|
|
|
.background(
|
|
|
|
Circle()
|
|
|
|
.foregroundColor(configuration.isPressed ? .compound.bgSubtlePrimary : .compound.bgCanvasDefault)
|
|
|
|
)
|
2023-11-01 10:03:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 11:05:42 +02:00
|
|
|
extension VoiceMessageButton.State {
|
2023-11-03 11:39:22 +01:00
|
|
|
init(_ state: AudioPlayerPlaybackState) {
|
2023-10-26 11:05:42 +02:00
|
|
|
switch state {
|
|
|
|
case .loading:
|
|
|
|
self = .loading
|
|
|
|
case .playing:
|
|
|
|
self = .playing
|
2023-11-03 11:39:22 +01:00
|
|
|
case .stopped, .error, .readyToPlay:
|
2023-10-26 11:05:42 +02:00
|
|
|
self = .paused
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct VoiceMessageButton_Previews: PreviewProvider, TestablePreview {
|
|
|
|
static var previews: some View {
|
2023-11-14 12:38:38 +00:00
|
|
|
VStack(spacing: 8) {
|
|
|
|
HStack(spacing: 8) {
|
2024-12-06 16:58:14 +02:00
|
|
|
VoiceMessageButton(state: .paused, size: .small) { }
|
|
|
|
VoiceMessageButton(state: .paused, size: .medium) { }
|
2023-10-26 11:05:42 +02:00
|
|
|
}
|
2023-11-14 12:38:38 +00:00
|
|
|
HStack(spacing: 8) {
|
2024-12-06 16:58:14 +02:00
|
|
|
VoiceMessageButton(state: .playing, size: .small) { }
|
|
|
|
VoiceMessageButton(state: .playing, size: .medium) { }
|
2023-10-26 11:05:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
.background(Color.gray)
|
|
|
|
}
|
|
|
|
}
|