2026-09-19 17:02:03 +02:00
|
|
|
// MyPass/ViewModels/EntryEditViewModel.swift
|
2026-09-19 16:00:37 +02:00
|
|
|
import Foundation
|
|
|
|
|
import Combine
|
|
|
|
|
import SwiftUI
|
|
|
|
|
import MyPassCore
|
|
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
|
final class EntryEditViewModel: ObservableObject {
|
|
|
|
|
@Published var title: String
|
|
|
|
|
@Published var username: String
|
|
|
|
|
@Published var password: String
|
|
|
|
|
@Published var url: String
|
|
|
|
|
@Published var notes: String
|
|
|
|
|
@Published var customFields: [CustomField]
|
2026-09-19 17:02:03 +02:00
|
|
|
@Published var groupId: UUID
|
2026-09-19 16:00:37 +02:00
|
|
|
@Published var errorMessage: String?
|
|
|
|
|
@Published var isSaving: Bool = false
|
|
|
|
|
|
2026-09-19 17:02:03 +02:00
|
|
|
let session: VaultSession
|
2026-09-19 16:00:37 +02:00
|
|
|
private let existingEntry: Entry?
|
|
|
|
|
|
2026-09-19 17:02:03 +02:00
|
|
|
/// `groupId` is the target group for a new entry, or the entry's current group when
|
|
|
|
|
/// editing (shown read-only in that case -- MyPass doesn't support re-parenting an
|
|
|
|
|
/// existing entry to a different group yet).
|
|
|
|
|
init(session: VaultSession, groupId: UUID, existing: Entry? = nil) {
|
2026-09-19 16:00:37 +02:00
|
|
|
self.session = session
|
|
|
|
|
self.groupId = groupId
|
|
|
|
|
self.existingEntry = existing
|
|
|
|
|
title = existing?.title ?? ""
|
|
|
|
|
username = existing?.username ?? ""
|
|
|
|
|
password = existing?.password.reveal() ?? ""
|
|
|
|
|
url = existing?.url ?? ""
|
|
|
|
|
notes = existing?.notes ?? ""
|
|
|
|
|
customFields = existing?.customFields ?? []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var isEditing: Bool { existingEntry != nil }
|
|
|
|
|
|
2026-09-19 17:02:03 +02:00
|
|
|
var flatGroups: [FlatGroup] {
|
|
|
|
|
guard let root = session.database?.root else { return [] }
|
|
|
|
|
return flattenGroups(in: root)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-19 16:02:23 +02:00
|
|
|
func save(dismiss: @escaping () -> Void) {
|
2026-09-19 16:00:37 +02:00
|
|
|
Task {
|
|
|
|
|
isSaving = true
|
|
|
|
|
errorMessage = nil
|
|
|
|
|
do {
|
|
|
|
|
if var entry = existingEntry {
|
|
|
|
|
entry.title = title
|
|
|
|
|
entry.username = username
|
|
|
|
|
entry.password = ProtectedString(password, isProtected: true)
|
|
|
|
|
entry.url = url
|
|
|
|
|
entry.notes = notes
|
|
|
|
|
entry.customFields = customFields
|
|
|
|
|
try session.updateEntry(entry)
|
|
|
|
|
} else {
|
|
|
|
|
let entry = Entry(
|
|
|
|
|
title: title,
|
|
|
|
|
username: username,
|
|
|
|
|
password: ProtectedString(password, isProtected: true),
|
|
|
|
|
url: url,
|
|
|
|
|
notes: notes,
|
|
|
|
|
customFields: customFields
|
|
|
|
|
)
|
|
|
|
|
try session.addEntry(entry, toGroupId: groupId)
|
|
|
|
|
}
|
|
|
|
|
dismiss()
|
|
|
|
|
} catch {
|
|
|
|
|
errorMessage = error.localizedDescription
|
|
|
|
|
}
|
|
|
|
|
isSaving = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addCustomField() {
|
|
|
|
|
customFields.append(CustomField(key: "", value: ProtectedString("", isProtected: false)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func removeCustomField(at offsets: IndexSet) {
|
|
|
|
|
customFields.remove(atOffsets: offsets)
|
|
|
|
|
}
|
|
|
|
|
}
|