Skip to content

Commit

Permalink
Modals and alerts + custom modal
Browse files Browse the repository at this point in the history
  • Loading branch information
eLud committed May 30, 2024
1 parent a554680 commit 798478e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Projects/First contact/First contact/First_contactApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ struct First_contactApp: App {
}
}
.environment(data)
.sheet(isPresented: $data.showGlobalAlert, content: {
Text("Global Alert")
})
}
}
}
6 changes: 3 additions & 3 deletions Projects/First contact/First contact/Models/AppData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import Foundation
@Observable
class AppData {

var showDivider: Bool
var showGlobalAlert: Bool
var applications: [Application]

var installedApp: Set<Application>

var todayPath: [Application]

init(applications: [Application]) {
self.applications = applications
installedApp = []
showDivider = true
showGlobalAlert = false
todayPath = []
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,52 @@ struct ApplicationListView: View {

@Environment(AppData.self) var data: AppData

@State private var selectedApp: Application?
@State private var showAccount = false
@State private var showAlert = false
@State private var showConfirmationDialog = false
@State private var showHomeMadeModal = false

var body: some View {
List(data.applications) { app in
NavigationLink(value: app) {
AppCellView(app: app)
}
}
.toolbar {
ToolbarItem {
Button("Account") {
// showAccount.toggle()
// selectedApp = data.applications.first
showHomeMadeModal.toggle()
}
}
}
.sheet(item: $selectedApp) { app in
AppStoreView(app: app)
}
.sheet(isPresented: $showAccount, content: {
Text("Test")
.presentationDragIndicator(.visible)
.presentationCornerRadius(50)
.presentationDetents([.fraction(0.5)])
})
.alert("Alerte importante", isPresented: $showAlert) {
Button("Delete", role: .destructive) {}
Button("Cancel", role: .cancel) {}
}
.confirmationDialog("Confirmation", isPresented: $showConfirmationDialog) {
Button("Logout", role: .destructive) { }
Button("Cancel", role: .cancel) {}
}
.smallModal(isPresented: $showHomeMadeModal, height: 200)

}
}

#Preview {
ApplicationListView()
NavigationStack {
ApplicationListView()
}
.environment(AppData(applications: Application.demoApps))
}
25 changes: 25 additions & 0 deletions Projects/First contact/First contact/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ extension View {
func backgroungWithRadius(color: Color, radius: CGFloat) -> some View {
modifier(BackgroundWithRadius(color: color, radius: radius))
}

func smallModal(isPresented: Binding<Bool>, height: Double) -> some View {
modifier(SmallModal(isPresented: isPresented, height: height))
}
}

struct BackgroundWithRadius: ViewModifier {
Expand All @@ -46,6 +50,27 @@ struct BackgroundWithRadius: ViewModifier {
}
}

struct SmallModal: ViewModifier {

@Binding var isPresented: Bool
let height: Double

func body(content: Content) -> some View {
ZStack(alignment: .bottom) {
content
.scaleEffect(isPresented ? 0.95 : 1)
if isPresented {
Rectangle()
.ignoresSafeArea(edges: .bottom)
.frame(height: height)
.transition(.move(edge: .bottom))
.zIndex(100)
}
}
.animation(.spring, value: isPresented)
}
}

#Preview {
ContentView()
}

0 comments on commit 798478e

Please sign in to comment.