Issue #2: Moved local SwiftUI state to a view model.
This commit is contained in:
parent
3ec25bc247
commit
6b253a6843
4 changed files with 42 additions and 17 deletions
16
dns/DnsApp.swift
Normal file
16
dns/DnsApp.swift
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
@main
|
||||||
|
struct DnsApp: App {
|
||||||
|
|
||||||
|
@StateObject
|
||||||
|
var viewModel = ViewModel()
|
||||||
|
|
||||||
|
var body: some Scene {
|
||||||
|
WindowGroup {
|
||||||
|
HomeView()
|
||||||
|
.environmentObject(viewModel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -35,8 +35,11 @@ enum ServiceStatus {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct HomeView: View {
|
struct HomeView: View {
|
||||||
|
|
||||||
|
@EnvironmentObject
|
||||||
|
private var viewModel: ViewModel
|
||||||
|
|
||||||
@State private var isEnabled = false
|
@State private var isEnabled = false
|
||||||
@State private var selectedBlocklist: BlocklistOption = .secure
|
|
||||||
@State private var serviceStatus: ServiceStatus = .operational
|
@State private var serviceStatus: ServiceStatus = .operational
|
||||||
|
|
||||||
private let falsePositiveURL = URL(string: "https://www.sr2.uk/contact")!
|
private let falsePositiveURL = URL(string: "https://www.sr2.uk/contact")!
|
||||||
|
|
@ -74,13 +77,13 @@ struct HomeView: View {
|
||||||
ForEach(BlocklistOption.allCases) { option in
|
ForEach(BlocklistOption.allCases) { option in
|
||||||
BlocklistRow(
|
BlocklistRow(
|
||||||
option: option,
|
option: option,
|
||||||
isSelected: selectedBlocklist == option
|
isSelected: viewModel.blocklist == option
|
||||||
)
|
)
|
||||||
.contentShape(Rectangle())
|
.contentShape(Rectangle())
|
||||||
.onTapGesture {
|
.onTapGesture {
|
||||||
guard option.enabled else { return }
|
guard option.enabled else { return }
|
||||||
withAnimation(.spring(duration: 0.3)) {
|
withAnimation(.spring(duration: 0.3)) {
|
||||||
selectedBlocklist = option
|
viewModel.blocklist = option
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.opacity(option.enabled ? 1 : 0.6)
|
.opacity(option.enabled ? 1 : 0.6)
|
||||||
|
|
@ -105,7 +108,7 @@ struct HomeView: View {
|
||||||
HStack {
|
HStack {
|
||||||
Label("Server", systemImage: "server.rack")
|
Label("Server", systemImage: "server.rack")
|
||||||
Spacer()
|
Spacer()
|
||||||
Text(selectedBlocklist.server)
|
Text(viewModel.blocklist.server)
|
||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
.font(.system(.body, design: .monospaced))
|
.font(.system(.body, design: .monospaced))
|
||||||
}
|
}
|
||||||
|
|
@ -113,7 +116,7 @@ struct HomeView: View {
|
||||||
HStack {
|
HStack {
|
||||||
Label("IPv4", systemImage: "globe")
|
Label("IPv4", systemImage: "globe")
|
||||||
Spacer()
|
Spacer()
|
||||||
Text(selectedBlocklist.ipv4)
|
Text(viewModel.blocklist.ipv4)
|
||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
.font(.system(.body, design: .monospaced))
|
.font(.system(.body, design: .monospaced))
|
||||||
}
|
}
|
||||||
|
|
@ -121,7 +124,7 @@ struct HomeView: View {
|
||||||
HStack {
|
HStack {
|
||||||
Label("IPv6", systemImage: "globe")
|
Label("IPv6", systemImage: "globe")
|
||||||
Spacer()
|
Spacer()
|
||||||
Text(selectedBlocklist.ipv6)
|
Text(viewModel.blocklist.ipv6)
|
||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
.font(.system(.body, design: .monospaced))
|
.font(.system(.body, design: .monospaced))
|
||||||
}
|
}
|
||||||
|
|
@ -217,4 +220,5 @@ struct HomeView: View {
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
HomeView()
|
HomeView()
|
||||||
|
.environmentObject(ViewModel())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
16
dns/ViewModel.swift
Normal file
16
dns/ViewModel.swift
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
//
|
||||||
|
// ViewModel.swift
|
||||||
|
// dns
|
||||||
|
//
|
||||||
|
// Created by Benjamin Erhart on 14.04.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import Combine
|
||||||
|
|
||||||
|
class ViewModel: NSObject, ObservableObject {
|
||||||
|
|
||||||
|
@Published
|
||||||
|
var blocklist: BlocklistOption = .secure
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
|
|
||||||
import SwiftUI
|
|
||||||
|
|
||||||
@main
|
|
||||||
struct dnsApp: App {
|
|
||||||
var body: some Scene {
|
|
||||||
WindowGroup {
|
|
||||||
HomeView()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue