Skip to content

Commit

Permalink
refactor: library views and modifiers (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba authored Nov 23, 2024
1 parent df20395 commit d1068db
Show file tree
Hide file tree
Showing 36 changed files with 300 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ extension View {
}
.voPrimaryButton(width: 60)
Button {} label: {
VOButtonLabel("Lorem Ipsum")
VOButtonLabel("Lorem Ipsum", isLoading: true)
}
.voSecondaryButton(colorScheme: colorScheme, width: 200)
Button {} label: {
Expand Down
File renamed without changes.
68 changes: 68 additions & 0 deletions Sources/Library/Modifiers/ErrorSheet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2024 Anass Bouassaba.
//
// Use of this software is governed by the Business Source License
// included in the file LICENSE in the root of this repository.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the GNU Affero General Public License v3.0 only, included in the file
// AGPL-3.0-only in the root of this repository.

import SwiftUI

struct VOErrorSheet: ViewModifier {
@Environment(\.colorScheme) private var colorScheme
private let isPresented: Binding<Bool>
private let message: String?

init(isPresented: Binding<Bool>, message: String?) {
self.isPresented = isPresented
self.message = message
}

func body(content: Content) -> some View {
content
.sheet(isPresented: isPresented) {
VStack(spacing: VOMetrics.spacing) {
VOErrorMessage(message)
Button {
isPresented.wrappedValue = false
} label: {
VOButtonLabel("Dismiss")
}
.voSecondaryButton(colorScheme: colorScheme)
}
.padding()
.presentationDetents([.fraction(0.25)])
}
}
}

extension View {
func voErrorSheet(isPresented: Binding<Bool>, message: String?) -> some View {
modifier(VOErrorSheet(isPresented: isPresented, message: message))
}
}

#Preview {
@Previewable @State var showError = false
@Previewable @State var showLongError = false

VStack(spacing: VOMetrics.spacing) {
Button("Show Error") {
showError = true
}
Button("Show Long Error") {
showLongError = true
}
}
.voErrorSheet(
isPresented: $showError,
message: "Lorem ipsum dolor sit amet."
)
.voErrorSheet(
isPresented: $showLongError,
// swiftlint:disable:next line_length
message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
68 changes: 68 additions & 0 deletions Sources/Library/Modifiers/WarningSheet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2024 Anass Bouassaba.
//
// Use of this software is governed by the Business Source License
// included in the file LICENSE in the root of this repository.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the GNU Affero General Public License v3.0 only, included in the file
// AGPL-3.0-only in the root of this repository.

import SwiftUI

struct VOWarningSheet: ViewModifier {
@Environment(\.colorScheme) private var colorScheme
private let isPresented: Binding<Bool>
private let message: String?

init(isPresented: Binding<Bool>, message: String?) {
self.isPresented = isPresented
self.message = message
}

func body(content: Content) -> some View {
content
.sheet(isPresented: isPresented) {
VStack(spacing: VOMetrics.spacing) {
VOWarningMessage(message)
Button {
isPresented.wrappedValue = false
} label: {
VOButtonLabel("Dismiss")
}
.voSecondaryButton(colorScheme: colorScheme)
}
.padding()
.presentationDetents([.fraction(0.25)])
}
}
}

extension View {
func voWarningSheet(isPresented: Binding<Bool>, message: String?) -> some View {
modifier(VOWarningSheet(isPresented: isPresented, message: message))
}
}

#Preview {
@Previewable @State var showWarning = false
@Previewable @State var showLongWarning = false

VStack(spacing: VOMetrics.spacing) {
Button("Show Warning") {
showWarning = true
}
Button("Show Long Warning") {
showLongWarning = true
}
}
.voWarningSheet(
isPresented: $showWarning,
message: "Lorem ipsum dolor sit amet."
)
.voWarningSheet(
isPresented: $showLongWarning,
// swiftlint:disable:next line_length
message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct VOAvatar: View {
}

#Preview {
VStack(spacing: VOMetrics.spacing) {
VStack(spacing: VOMetrics.spacing2Xl) {
VOAvatar(name: "Bruce Wayne", size: 100)
VOAvatar(name: "你好世界!!!", size: 100)
VOAvatar(name: "مرحبا بالجميع", size: 100)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import SwiftUI

struct ColorBadge: View {
struct VOColorBadge: View {
var text: String
var color: Color
var style: Style
Expand Down Expand Up @@ -52,11 +52,11 @@ struct ColorBadge: View {

#Preview {
VStack {
ColorBadge("Red", color: .red400, style: .fill)
ColorBadge("Purple", color: .purple400, style: .fill)
ColorBadge("Green", color: .green400, style: .fill)
ColorBadge("Red", color: .red400, style: .outline)
ColorBadge("Purple", color: .purple400, style: .outline)
ColorBadge("Green", color: .green400, style: .outline)
VOColorBadge("Red", color: .red400, style: .fill)
VOColorBadge("Purple", color: .purple400, style: .fill)
VOColorBadge("Green", color: .green400, style: .fill)
VOColorBadge("Red", color: .red400, style: .outline)
VOColorBadge("Purple", color: .purple400, style: .outline)
VOColorBadge("Green", color: .green400, style: .outline)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,14 @@

import SwiftUI

struct VOSheetErrorIcon: View {
struct VOErrorIcon: View {
var body: some View {
Image(systemName: "xmark.circle.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30, height: 30)
Image(systemName: "xmark.circle")
.font(.title)
.foregroundStyle(Color.red500)
}
}

struct SheetWarningIcon: View {
var body: some View {
Image(systemName: "exclamationmark.triangle.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30, height: 30)
.foregroundStyle(Color.yellow300)
}
}

#Preview {
VStack {
VOSheetErrorIcon()
SheetWarningIcon()
}
VOErrorIcon()
}
43 changes: 43 additions & 0 deletions Sources/Library/Views/ErrorMessage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2024 Anass Bouassaba.
//
// Use of this software is governed by the Business Source License
// included in the file LICENSE in the root of this repository.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the GNU Affero General Public License v3.0 only, included in the file
// AGPL-3.0-only in the root of this repository.

import SwiftUI

struct VOErrorMessage: View {
let message: String?

init() {
message = nil
}

init(_ message: String?) {
self.message = message
}

var body: some View {
VStack(spacing: VOMetrics.spacingXs) {
VOErrorIcon()
if let message {
Text(message)
.foregroundStyle(.secondary)
.lineLimit(1)
}
}
}
}

#Preview {
VStack(spacing: VOMetrics.spacing2Xl) {
VOErrorMessage("Lorem ipsum dolor sit amet.")
// swiftlint:disable:next line_length
VOErrorMessage("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
}
.padding()
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
import SwiftUI
import VoltaserveCore

struct PermissionBadge: View {
struct VOPermissionBadge: View {
var permission: VOPermission.Value

init(_ permission: VOPermission.Value) {
self.permission = permission
}

var body: some View {
ColorBadge(text(), color: Constants.background, style: .fill)
VOColorBadge(text(), color: Constants.background, style: .fill)
}

func text() -> String {
Expand All @@ -42,8 +42,8 @@ struct PermissionBadge: View {

#Preview {
VStack {
PermissionBadge(.viewer)
PermissionBadge(.editor)
PermissionBadge(.owner)
VOPermissionBadge(.viewer)
VOPermissionBadge(.editor)
VOPermissionBadge(.owner)
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions Sources/Library/Views/WarningIcon.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2024 Anass Bouassaba.
//
// Use of this software is governed by the Business Source License
// included in the file LICENSE in the root of this repository.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the GNU Affero General Public License v3.0 only, included in the file
// AGPL-3.0-only in the root of this repository.

import SwiftUI

struct VOWarningIcon: View {
var body: some View {
Image(systemName: "exclamationmark.triangle")
.font(.title)
.foregroundStyle(Color.yellow400)
}
}

#Preview {
VOWarningIcon()
}
43 changes: 43 additions & 0 deletions Sources/Library/Views/WarningMessage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2024 Anass Bouassaba.
//
// Use of this software is governed by the Business Source License
// included in the file LICENSE in the root of this repository.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the GNU Affero General Public License v3.0 only, included in the file
// AGPL-3.0-only in the root of this repository.

import SwiftUI

struct VOWarningMessage: View {
let message: String?

init() {
message = nil
}

init(_ message: String?) {
self.message = message
}

var body: some View {
VStack(spacing: VOMetrics.spacingXs) {
VOWarningIcon()
if let message {
Text(message)
.foregroundStyle(.secondary)
.lineLimit(1)
}
}
}
}

#Preview {
VStack(spacing: VOMetrics.spacing2Xl) {
VOWarningMessage("Lorem ipsum dolor sit amet.")
// swiftlint:disable:next line_length
VOWarningMessage("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
}
.padding()
}
12 changes: 10 additions & 2 deletions Sources/Screens/Account/AccountSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,16 @@ struct AccountSettings: View {
}
.navigationBarTitleDisplayMode(.inline)
.navigationTitle("Settings")
.voErrorAlert(isPresented: $showError, title: errorTitle, message: errorMessage)
.voErrorAlert(
isPresented: $showError,
title: errorTitle,
message: errorMessage
)
.voErrorAlert(
isPresented: $accountStore.showError,
title: accountStore.errorTitle,
message: accountStore.errorMessage
)
.onAppear {
accountStore.tokenStore = tokenStore
if tokenStore.token != nil {
Expand All @@ -128,7 +137,6 @@ struct AccountSettings: View {
onAppearOrChange()
}
}
.sync($accountStore.showError, with: $showError)
}

private func onAppearOrChange() {
Expand Down
Loading

0 comments on commit d1068db

Please sign in to comment.