Skip to content

Commit

Permalink
Split reusable views into extensions (#322)
Browse files Browse the repository at this point in the history
Like in #321
  • Loading branch information
keeshux committed Jul 3, 2023
1 parent d7ebcb2 commit bd6340c
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 171 deletions.
4 changes: 0 additions & 4 deletions Passepartout.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
0E71ACF927C12E4800F85C4B /* CreditsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E71ACF827C12E4800F85C4B /* CreditsView.swift */; };
0E71ACFB27C12E5300F85C4B /* VersionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E71ACFA27C12E5300F85C4B /* VersionView.swift */; };
0E71ACFD27C1321A00F85C4B /* ActivityView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E71ACFC27C1321A00F85C4B /* ActivityView.swift */; };
0E7577D72816A3B200081CBE /* DestructiveButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E7577D62816A3B200081CBE /* DestructiveButton.swift */; };
0E7577DF2817E22C00081CBE /* VPNToggle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E7577DE2817E22C00081CBE /* VPNToggle.swift */; };
0E7A8C0A2A1D410500780F4B /* PersistenceManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E7A8C092A1D410400780F4B /* PersistenceManager.swift */; };
0E7A8C0C2A1D4A6100780F4B /* PassepartoutLibrary in Frameworks */ = {isa = PBXBuildFile; productRef = 0E7A8C0B2A1D4A6100780F4B /* PassepartoutLibrary */; };
Expand Down Expand Up @@ -398,7 +397,6 @@
0E71ACF827C12E4800F85C4B /* CreditsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreditsView.swift; sourceTree = "<group>"; };
0E71ACFA27C12E5300F85C4B /* VersionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VersionView.swift; sourceTree = "<group>"; };
0E71ACFC27C1321A00F85C4B /* ActivityView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivityView.swift; sourceTree = "<group>"; };
0E7577D62816A3B200081CBE /* DestructiveButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DestructiveButton.swift; sourceTree = "<group>"; };
0E7577DE2817E22C00081CBE /* VPNToggle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VPNToggle.swift; sourceTree = "<group>"; };
0E7A8C072A1D40BA00780F4B /* Picker+OpenVPN.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Picker+OpenVPN.swift"; sourceTree = "<group>"; };
0E7A8C082A1D40BA00780F4B /* Picker+Network.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Picker+Network.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -613,7 +611,6 @@
0EB4042D27CA136200378B1A /* AddingTextField.swift */,
0EB3412F27C7761A00483410 /* Binding+Extensions.swift */,
0E9ED48027FD9BAE003B2316 /* CopySavingButton.swift */,
0E7577D62816A3B200081CBE /* DestructiveButton.swift */,
0EDE02C127F61C79000FBE3C /* EditableTextList.swift */,
0E3A593B2A50975700B3FE40 /* ErrorHandler.swift */,
0E2C171A27CB5A3A007E8488 /* GenericCreditsView.swift */,
Expand Down Expand Up @@ -1458,7 +1455,6 @@
0E3A593C2A50975700B3FE40 /* ErrorHandler.swift in Sources */,
0E34AC8227F892C40042F2AB /* OnDemandView+SSID.swift in Sources */,
0E3B7FCD27E47B3700C66F13 /* AddHostView+Name.swift in Sources */,
0E7577D72816A3B200081CBE /* DestructiveButton.swift in Sources */,
0EF2212D27E66EB5001D0BD7 /* AddProviderView.swift in Sources */,
0EB90CC129C25BBD00E64628 /* InteractiveConnectionView.swift in Sources */,
0E35C09A280E95BB0071FA35 /* ProviderProfileAvailability.swift in Sources */,
Expand Down
8 changes: 6 additions & 2 deletions Passepartout/App/Reusable/AddingTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,19 @@ struct AddingTextField<Field: View, ActionLabel: View>: View {
}
}
}
}

// MARK: -

private func doAdd() {
private extension AddingTextField {
func doAdd() {
withAnimation {
onAdd?()
isAdding = true
}
}

private func doCommit() {
func doCommit() {
withAnimation {
onCommit?()
isAdding = false
Expand Down
14 changes: 11 additions & 3 deletions Passepartout/App/Reusable/CopySavingButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,28 @@ struct CopySavingButton<T: Equatable, Label: View>: View {
}
}
}
}

// MARK: -

private var canSave: Bool {
private extension CopySavingButton {
var canSave: Bool {
isLoaded && (saveAnyway || copy != original)
}
}

// MARK: -

private func loadFromOriginal(once: Bool) {
private extension CopySavingButton {
func loadFromOriginal(once: Bool) {
guard !once || !isLoaded else {
return
}
copy = original
isLoaded = true
}

private func saveToOriginal() {
func saveToOriginal() {
if copy != original {
original = copy
}
Expand Down
36 changes: 0 additions & 36 deletions Passepartout/App/Reusable/DestructiveButton.swift

This file was deleted.

62 changes: 34 additions & 28 deletions Passepartout/App/Reusable/EditableTextList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ struct EditableTextList<Field: View, ActionLabel: View>: View {

private let addedUUID = UUID()

private var addedText: Binding<String> {
.init {
editedTextStrings[addedUUID] ?? ""
} set: {
editedTextStrings[addedUUID] = $0
}
}

var body: some View {
debugChanges()
return Group {
Expand All @@ -90,8 +82,20 @@ struct EditableTextList<Field: View, ActionLabel: View>: View {
}
}.onChange(of: elements, perform: remapElements)
}
}

// MARK: -

private extension EditableTextList {
var addedText: Binding<String> {
.init {
editedTextStrings[addedUUID] ?? ""
} set: {
editedTextStrings[addedUUID] = $0
}
}

private func existingRow(_ element: IdentifiableString) -> some View {
func existingRow(_ element: IdentifiableString) -> some View {
let editedText = binding(toEditedElement: element)

return textField(.init(isNewElement: false, text: editedText, onEditingChanged: {
Expand All @@ -104,7 +108,7 @@ struct EditableTextList<Field: View, ActionLabel: View>: View {
}))
}

private var newRow: some View {
var newRow: some View {
AddingTextField(
onAdd: {
addedText.wrappedValue = ""
Expand All @@ -120,10 +124,8 @@ struct EditableTextList<Field: View, ActionLabel: View>: View {
}
}

// MARK: View model

extension EditableTextList {
private func remapElements(_ newElements: [String]) {
private extension EditableTextList {
func remapElements(_ newElements: [String]) {
var oldIdentifiableElements = identifiableElements
var newIdentifiableElements: [IdentifiableString] = []

Expand All @@ -148,7 +150,20 @@ extension EditableTextList {
}
}

private func addElement() {
func binding(toEditedElement element: IdentifiableString) -> Binding<String> {
// print(">>> <-> \(element)")
.init {
editedTextStrings[element.id] ?? element.string
} set: {
editedTextStrings[element.id] = $0
}
}
}

// MARK: -

private extension EditableTextList {
func addElement() {
guard allowsDuplicates || !identifiableElements.contains(where: {
$0.string == addedText.wrappedValue
}) else {
Expand All @@ -159,16 +174,7 @@ extension EditableTextList {
commit()
}

private func binding(toEditedElement element: IdentifiableString) -> Binding<String> {
// print(">>> <-> \(element)")
.init {
editedTextStrings[element.id] ?? element.string
} set: {
editedTextStrings[element.id] = $0
}
}

private func replaceElement(at id: UUID, with editedText: Binding<String>) {
func replaceElement(at id: UUID, with editedText: Binding<String>) {
// print(">>> \(identifiableElements[id].string) -> \(editedText.wrappedValue)")
guard let i = identifiableElements.firstIndex(where: {
$0.id == id
Expand All @@ -188,21 +194,21 @@ extension EditableTextList {
commit()
}

private func onDelete(offsets: IndexSet) {
func onDelete(offsets: IndexSet) {
var mapped = mapping(identifiableElements)
mapped.remove(atOffsets: offsets)
identifiableElements = mapped
commit()
}

private func onMove(indexSet: IndexSet, to offset: Int) {
func onMove(indexSet: IndexSet, to offset: Int) {
var mapped = mapping(identifiableElements)
mapped.move(fromOffsets: indexSet, toOffset: offset)
identifiableElements = mapped
commit()
}

private func commit() {
func commit() {
// print(">>> identifiableElements = \(identifiableElements.map { "\($0.string) (\($0.id))" })")
elements = identifiableElements.map(\.string)
}
Expand Down
Loading

0 comments on commit bd6340c

Please sign in to comment.