1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2026-03-28 06:50:08 +01:00

UserDefaults-backed storage for Favorites

This commit is contained in:
2024-08-25 14:34:34 +02:00
parent 5c9c51f580
commit d60c26c9b2
6 changed files with 79 additions and 8 deletions

View File

@@ -89,6 +89,9 @@ class App {
/** List of detected (installed) applications that PHP Monitor can work with. */
var detectedApplications: [Application] = []
/** Favorites storage, which keeps track of favorited domains. */
var favorites = Favorites.shared
/** The warning manager, responsible for keeping track of warnings. */
var warnings = WarningManager.shared

View File

@@ -13,7 +13,11 @@ class ValetProxy: ValetListable {
var tld: String
var target: String
var secured: Bool = false
var favorited: Bool = false
var favoriteSignature: String {
"proxy:domain:\(domain).\(tld)|target:\(target)"
}
init(domain: String, target: String, secure: Bool, tld: String) {
self.domain = domain
@@ -29,6 +33,8 @@ class ValetProxy: ValetListable {
secure: false,
tld: configuration.tld
)
self.favorited = Favorites.shared.contains(domain: self.domain)
self.determineSecured()
}
@@ -72,6 +78,11 @@ class ValetProxy: ValetListable {
self.secured = FileSystem.fileExists("~/.config/valet/Certificates/\(self.domain).\(self.tld).key")
}
func toggleFavorite() {
self.favorited.toggle()
Favorites.shared.toggle(domain: self.favoriteSignature)
}
func toggleSecure() async throws {
try await ValetInteractor.shared.toggleSecure(proxy: self)
}

View File

@@ -62,6 +62,9 @@ class ValetSite: ValetListable {
}
var favorited: Bool = false
var favoriteSignature: String {
"site:domain:\(name).\(tld)|path:\(absolutePath)"
}
init(
name: String,
@@ -77,6 +80,7 @@ class ValetSite: ValetListable {
self.secured = false
if makeDeterminations {
self.favorited = Favorites.shared.contains(domain: favoriteSignature)
determineSecured()
determineIsolated()
determineComposerPhpVersion()
@@ -317,6 +321,11 @@ class ValetSite: ValetListable {
try await ValetInteractor.shared.toggleSecure(site: self)
}
func toggleFavorite() {
self.favorited.toggle()
Favorites.shared.toggle(domain: self.favoriteSignature)
}
func isolate(version: String) async throws {
try await ValetInteractor.shared.isolate(site: self, version: version)
}

View File

@@ -0,0 +1,38 @@
//
// Favorites.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 25/08/2024.
// Copyright © 2024 Nico Verbruggen. All rights reserved.
//
import Foundation
class Favorites {
static var shared: Favorites = Favorites()
var items: [String]
init() {
if let items = UserDefaults.standard.array(forKey: "user_favorites") as? [String] {
self.items = items
} else {
self.items = []
}
}
public func contains(domain: String) -> Bool {
return self.items.contains(domain)
}
public func toggle(domain: String) {
if let index = items.firstIndex(of: domain) {
items.remove(at: index)
} else {
items.append(domain)
}
UserDefaults.standard.setValue(items, forKey: "user_favorites")
UserDefaults.standard.synchronize()
}
}

View File

@@ -106,10 +106,10 @@ extension DomainListVC {
waitAndExecute {
do {
if let site = domain as? ValetSite {
site.favorited.toggle()
site.toggleFavorite()
}
if let proxy = domain as? ValetProxy {
proxy.favorited.toggle()
proxy.toggleFavorite()
}
// Reload the entire table as the sorting may be affected