Skip to content

Commit

Permalink
Refactor all views
Browse files Browse the repository at this point in the history
Break up huge view files into smaller files
  • Loading branch information
milanvarady committed Dec 26, 2024
1 parent 1161418 commit 19e4dd1
Show file tree
Hide file tree
Showing 49 changed files with 1,986 additions and 1,530 deletions.
210 changes: 199 additions & 11 deletions Applite.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

126 changes: 0 additions & 126 deletions Applite/Components/BrewPathSelectorView.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,3 @@ final class AlertManager: ObservableObject {
primaryAction = nil
}
}

struct AlertModifier: ViewModifier {
@ObservedObject var manager: AlertManager

func body(content: Content) -> some View {
content
.alert(manager.title, isPresented: $manager.isPresented) {
Button(manager.primaryButtonTitle) {
manager.primaryAction?()
manager.dismiss()
}

// Add cancel button if we have a primary action
if manager.primaryAction != nil {
Button("Cancel", role: .cancel) {
manager.dismiss()
}
}
} message: {
Text(manager.message)
}
}
}

extension View {
func alertManager(_ manager: AlertManager) -> some View {
modifier(AlertModifier(manager: manager))
}
}
37 changes: 37 additions & 0 deletions Applite/Utilities/Alert Manager/AlertManagerViewModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// AlertManagerViewModifier.swift
// Applite
//
// Created by Milán Várady on 2024.12.27.
//

import SwiftUI

struct AlertModifier: ViewModifier {
@ObservedObject var manager: AlertManager

func body(content: Content) -> some View {
content
.alert(manager.title, isPresented: $manager.isPresented) {
Button(manager.primaryButtonTitle) {
manager.primaryAction?()
manager.dismiss()
}

// Add cancel button if we have a primary action
if manager.primaryAction != nil {
Button("Cancel", role: .cancel) {
manager.dismiss()
}
}
} message: {
Text(manager.message)
}
}
}

extension View {
func alertManager(_ manager: AlertManager) -> some View {
modifier(AlertModifier(manager: manager))
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// BrewPathSelectorView+CustomPathOption.swift
// Applite
//
// Created by Milán Várady on 2024.12.26.
//

import SwiftUI

extension BrewPathSelectorView {
func customPathOption(option: BrewPaths.PathOption) -> some View {
VStack(alignment: .leading, spacing: 5) {
pathOption(option, showPath: false)

HStack {
TextField("Custom brew path", text: $customBrewPathDebounced.text, prompt: Text("/path/to/brew"))
.textFieldStyle(.roundedBorder)
.frame(maxWidth: 300)
.autocorrectionDisabled()

Button("Browse") {
choosingCustomFolder = true
}
.fileImporter(
isPresented: $choosingCustomFolder,
allowedContentTypes: [.unixExecutable]
) { result in
switch result {
case .success(let file):
customBrewPathDebounced.text = file.path
case .failure(let error):
print(error.localizedDescription)
}
}
}
.disabled(brewPathOption != BrewPaths.PathOption.custom.rawValue)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// BrewPathSelectorView+GetPathDescription.swift
// Applite
//
// Created by Milán Várady on 2024.12.26.
//

import SwiftUI

extension BrewPathSelectorView {
func getPathDescription(for option: BrewPaths.PathOption) -> String {
switch option {
case .appPath:
return "\(Bundle.main.appName)'s installation"

case .defaultAppleSilicon:
return "Apple Silicon Mac"

case .defaultIntel:
return "Intel Mac"

case .custom:
return "Custom"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// BrewPathSelectorView+PathOption.swift
// Applite
//
// Created by Milán Várady on 2024.12.26.
//

import SwiftUI

extension BrewPathSelectorView {
func pathOption(_ option: BrewPaths.PathOption, showPath: Bool = true) -> some View {
HStack {
Text("\(getPathDescription(for: option))")

if showPath {
Text("(\(BrewPaths.getBrewExectuablePath(for: option, shellFriendly: false)))")
.truncationMode(.middle)
.lineLimit(1)
.foregroundColor(.gray)
}

if option.rawValue == brewPathOption {
if isSelectedPathValid {
Image(systemName: "checkmark.circle")
.font(.system(size: 16))
.foregroundColor(.green)
} else {
Image(systemName: "xmark.circle")
.font(.system(size: 16))
.foregroundColor(.red)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// BrewPathSelectorView.swift
// Applite
//
// Created by Milán Várady on 2023. 01. 03..
//

import SwiftUI

/// Provides a picker so the user can select the brew executable path they want to use
struct BrewPathSelectorView: View {
@Binding var isSelectedPathValid: Bool

@StateObject var customBrewPathDebounced = DebounceObject()

@AppStorage(Preferences.customUserBrewPath.rawValue) var customUserBrewPath: String = BrewPaths.getBrewExectuablePath(for: .defaultAppleSilicon, shellFriendly: false)
@AppStorage(Preferences.brewPathOption.rawValue) var brewPathOption = BrewPaths.PathOption.defaultAppleSilicon.rawValue

@State var choosingCustomFolder = false

var body: some View {
VStack(alignment: .leading) {
Picker("", selection: $brewPathOption) {
ForEach(BrewPaths.PathOption.allCases) { option in
if option != .custom {
pathOption(option)
.tag(option.rawValue)
} else {
customPathOption(option: option)
.tag(option.rawValue)
}
}
}
.pickerStyle(.radioGroup)
}
.onAppear {
customBrewPathDebounced.text = customUserBrewPath
isSelectedPathValid = BrewPaths.isSelectedBrewPathValid()
}
.onChange(of: brewPathOption) { _ in
isSelectedPathValid = BrewPaths.isSelectedBrewPathValid()
}
.onChange(of: customBrewPathDebounced.debouncedText) { newPath in
customUserBrewPath = newPath

if brewPathOption == BrewPaths.PathOption.custom.rawValue {
isSelectedPathValid = isBrewPathValid(path: newPath)
}
}
}
}

struct BrewPathSelectorView_Previews: PreviewProvider {
static var previews: some View {
BrewPathSelectorView(isSelectedPathValid: .constant(false))
}
}
File renamed without changes.
Loading

0 comments on commit 19e4dd1

Please sign in to comment.