Closed5
SwiftUIのライブラリを作りたい
ピン留めされたアイテム
作るライブラリ
名前
Quick Toast (仮)
機能
- カスタムアラートを出しやすくする
- 通知(エラー通知、完了通知を出しやすくする)
- 背景が暗くなるマスク
SwiftUIのライブラリを作る際の参考記事
デザイン案
通知
アラート
アクションシート
機能
- 色を指定できるように
- 細かいスタイルの選択
- 簡単にアラートを呼び出す
使用イメージ
// AppDelegate.swift
MainView().enviromentObject(QuickToastManager())
func setQuickToastManager() {
QuickToastManager.theme = .colorful
}
// MainView.swift
struct MainView {
@EnvironmentObject var quickToastManager: QuickToastManager
var body: some View {
ZStack {
ContentView()
QuickToastView()
}
}
}
// QuickToastView.swift
struct QuickToastView {
@EnvironmentObject var manager: QuickToastManager
var body: some View {
ZStack {
if manager.useMask {
Color.black.withOpacity(0.3).edgeIgnoreSafeArea(true)
}
switch manager.currentView {
case .notification:
case .alert:
case .actionSheet:
default:
}
}
}
}
// QuickToastManager.swift
class QuickToastManager: ObservableObject {
enum CurrentView {
case none
case notification
case alert
case actionSheet
}
@Published var currentView: CurrentView = .none
var useMask: Bool {
return currentView == .alert || currentView == .actionSheet
}
}
// ContentView.swift
struct ContentView {
@EnvironmentObject var quickToastManager: QuickToastManager
var body: some View {
VStack {
Button(action: {
quickToastManager.showNotification(
.danger(
title: "Danger",
message: "This is dangerous",
)
)
}) {
Text("Show notification")
}
Button(action: {
quickToastManager.showAlert(
.confirm(
title: "Confirm",
actions: [
.destructive(
text: "Destroy"
action: {
Something.destroy();
}
),
.cancel(
text: "Cancel"
)
]
)
)
}) {
Text("Show Alert")
}
Button(action: {
quickToastManager.showActionSheet(
.common(
title: "Common",
actions: [
.destructive(
text: "Destroy"
action: {
Something.destroy();
}
),
.common(
text: "Common",
action: {
Something.common();
}
)
.cancel(
text: "Cancel"
),
]
)
)
}) {
Text("Show action sheet")
}
}
}
}
enum ColorTheme {
case mono
case highlight
case colorful
}
enum AnimationType {
case fade
case slide
case pop
}
enum ToastType {
case danger
case notice
case reward
case progress
case common
}
protocol QuickToast {
enum Type
enum Layout
var type: Type
var layout: Layout
var theme: ColorTheme
var title: String
var message: String?
var animation: AnimationType
}
// Notification.swift
struct Notification: QuickToast {
var completition: () -> Void
}
// Alert.swift
struct Alert: QuickToast {
var actions: [ActionItem]
}
// ActionSheet.swift
struct ActionSheet: QuickToast {
var actions: [ActionItem]
}
// ActionItem.swift
struct ActionItem {
enum Type {
case cancel
case destructive
case common
}
var text: String
var action: (() -> Void)?
}
このスクラップは2022/05/31にクローズされました