Files
SwiftUI-LifeGame/Shared/View/Screen/MainGame/ActionMenu.swift
T
2020-08-07 18:26:59 +09:00

67 lines
1.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// MenuView.swift
// LifeGameApp
//
// Created by Yusuke Hosonuma on 2020/08/02.
//
import SwiftUI
struct ActionMenu<Label>: View where Label: View {
@ObservedObject var viewModel: MainGameViewModel
let label: () -> Label
// MARK: Private
@State private var isPresentedClearAlert = false
init(viewModel: MainGameViewModel, @ViewBuilder label: @escaping () -> Label) {
self.viewModel = viewModel
self.label = label
}
// MARK: View
var body: some View {
Menu(content: content, label: label)
}
@ViewBuilder
private func content() -> some View {
// Note:
// バグか仕様なのか分からないが、実際のボタン表示は逆順になる。(Xcode 12 beta3
//
// ...
// -----
// Random
//
Button(action: { isPresentedClearAlert.toggle() }) {
HStack {
Text("Clear")
Image(systemName: "xmark.circle")
}
}
.foregroundColor(.red) // TODO: not working in beta3
.alert(isPresented: $isPresentedClearAlert, content: clearAlert)
Divider()
Button(action: { viewModel.tapRandomButton() }) {
HStack {
Text("Random")
Image(systemName: "square.grid.2x2.fill")
}
}
}
// MARK: Actions
private func clearAlert() -> Alert {
Alert(
title: Text("Do you want to clear?"),
primaryButton: .cancel(),
secondaryButton: .destructive(Text("Clear"), action: viewModel.tapClear))
}
}