From 0528a7db41b3f5684e0dd79482fc1b75c7a115bb Mon Sep 17 00:00:00 2001 From: Jeremy Rand Date: Wed, 3 Nov 2021 22:55:00 -0400 Subject: [PATCH] Move the destination data model stuff out of the UI. --- ListenerGS.xcodeproj/project.pbxproj | 2 +- .../DestinationsView.swift | 15 ++++++------- ListenerGS/GSDestinations.swift | 21 ++++++++++++++++++- ListenerGS/Info.plist | 2 +- 4 files changed, 28 insertions(+), 12 deletions(-) rename DestinationsView.swift => ListenerGS/DestinationsView.swift (85%) diff --git a/ListenerGS.xcodeproj/project.pbxproj b/ListenerGS.xcodeproj/project.pbxproj index 1cca1e0..6740fb3 100644 --- a/ListenerGS.xcodeproj/project.pbxproj +++ b/ListenerGS.xcodeproj/project.pbxproj @@ -110,7 +110,6 @@ 9D5155E626A1EF7B0075EBC7 = { isa = PBXGroup; children = ( - 9DD67CEF2728F5B700243FC6 /* DestinationsView.swift */, 9D51562126A1F0DF0075EBC7 /* LICENSE */, 9D51562A26A1F1B40075EBC7 /* README.md */, 9D5155F126A1EF7B0075EBC7 /* ListenerGS */, @@ -137,6 +136,7 @@ 9D0DC15826F2E47A007EB92D /* ListenerGS.entitlements */, 9D5155F226A1EF7B0075EBC7 /* ListenerGSApp.swift */, 9D6F27082728EF410089585E /* MainView.swift */, + 9DD67CEF2728F5B700243FC6 /* DestinationsView.swift */, 9DCCDACB271FB87100F311DF /* GSDestinations.swift */, 9D5155F426A1EF7B0075EBC7 /* ContentView.swift */, 9D6ED239271E6BD600D773CD /* SpeechForwarder.swift */, diff --git a/DestinationsView.swift b/ListenerGS/DestinationsView.swift similarity index 85% rename from DestinationsView.swift rename to ListenerGS/DestinationsView.swift index 9e73a76..5738d49 100644 --- a/DestinationsView.swift +++ b/ListenerGS/DestinationsView.swift @@ -7,20 +7,17 @@ import SwiftUI -struct Destination: Identifiable, Hashable { - let ipAddress : String - let id = UUID() -} struct DestinationsView: View { - @State private var destinations:[Destination] = [] @State private var editMode = EditMode.inactive @State private var showPopover = false @State private var newDestination = "" + @StateObject private var destinations = GSDestinations() + var body: some View { List { - ForEach(destinations) { destination in + ForEach(destinations.dests) { destination in NavigationLink(destination: Text(destination.ipAddress)) { Text(destination.ipAddress) } @@ -70,11 +67,11 @@ struct DestinationsView: View { } private func onDelete(offsets: IndexSet) { - destinations.remove(atOffsets: offsets) + destinations.onDelete(offsets: offsets) } private func onMove(source: IndexSet, destination: Int) { - destinations.move(fromOffsets: source, toOffset: destination) + destinations.onMove(source: source, destination: destination) } func showAdd() { @@ -82,7 +79,7 @@ struct DestinationsView: View { } func onAdd() { - destinations.append(Destination(ipAddress: self.newDestination)) + destinations.onAdd(ipAddress: self.newDestination) newDestination = "" showPopover = false; } diff --git a/ListenerGS/GSDestinations.swift b/ListenerGS/GSDestinations.swift index 8ceaa9c..221f63e 100644 --- a/ListenerGS/GSDestinations.swift +++ b/ListenerGS/GSDestinations.swift @@ -7,5 +7,24 @@ import Foundation -class GSDestinations : ObservableObject { + +struct Destination: Identifiable, Hashable { + let ipAddress : String + let id = UUID() +} + +class GSDestinations : ObservableObject { + @Published var dests:[Destination] = [] + + public func onDelete(offsets: IndexSet) { + dests.remove(atOffsets: offsets) + } + + public func onMove(source: IndexSet, destination: Int) { + dests.move(fromOffsets: source, toOffset: destination) + } + + public func onAdd(ipAddress: String) { + dests.append(Destination(ipAddress: ipAddress)) + } } diff --git a/ListenerGS/Info.plist b/ListenerGS/Info.plist index b4e1640..3c1113f 100644 --- a/ListenerGS/Info.plist +++ b/ListenerGS/Info.plist @@ -19,7 +19,7 @@ CFBundleShortVersionString 1.0 CFBundleVersion - 239 + 242 LSApplicationCategoryType public.app-category.utilities LSRequiresIPhoneOS