diff --git a/Sources/FeedbackKit/Feedback.swift b/Sources/FeedbackKit/Feedback.swift index 5674730..49e578f 100644 --- a/Sources/FeedbackKit/Feedback.swift +++ b/Sources/FeedbackKit/Feedback.swift @@ -1,6 +1,5 @@ struct Feedback: Identifiable, Hashable { var id: String { title } - let type: FeedbackType let title: String let description: String diff --git a/Sources/FeedbackKit/FeedbackStore.swift b/Sources/FeedbackKit/FeedbackStore.swift index f1e9b07..ae9b06e 100644 --- a/Sources/FeedbackKit/FeedbackStore.swift +++ b/Sources/FeedbackKit/FeedbackStore.swift @@ -1,3 +1,41 @@ -class FeedbackStore { - +import AppState +import Cache +import Foundation + +extension Application { + fileprivate var storedFeedback: FileState<[UUID: Feedback]> { + fileState(initial: [:], filename: "storedFeedback") + } +} + +class FeedbackStore: ObservableObject { + enum StoreError: Error { + case missingValue + } + + @FileState(\.storedFeedback) private var storedFeedback: [UUID: Feedback] + + func `set`(feedback: Feedback) { + storedFeedback[feedback.id] = feedback + } + + func `get`(id: UUID) -> Feedback? { + storedFeedback[id] + } + + func remove(id: UUID) { + storedFeedback[id] = nil + } + + func update(id: UUID, _ block: (Feedback) -> Feedback) throws { + guard let storedValue = get(id: id) else { + throw StoreError.missingValue + } + + set(feedback: block(storedValue)) + } + + func all() -> [Feedback] { + Array(storedFeedback.values) + } } diff --git a/Sources/FeedbackKit/FeedbackType.swift b/Sources/FeedbackKit/FeedbackType.swift index 1c6419b..056832d 100644 --- a/Sources/FeedbackKit/FeedbackType.swift +++ b/Sources/FeedbackKit/FeedbackType.swift @@ -1,4 +1,4 @@ -enum FeedbackType: String { +enum FeedbackType: String, Codable { case comment case bug case feature diff --git a/Sources/FeedbackKit/FeedbackViewModel.swift b/Sources/FeedbackKit/FeedbackViewModel.swift index feca1aa..cde2eeb 100644 --- a/Sources/FeedbackKit/FeedbackViewModel.swift +++ b/Sources/FeedbackKit/FeedbackViewModel.swift @@ -1,5 +1,31 @@ +import Combine import SwiftUI class FeedbackViewModel: ObservableObject { + private var bag: Set + private let store: FeedbackStore + + var feedback: [Feedback] + + init() { + bag = Set() + store = FeedbackStore() + feedback = store.all() + + consume(object: store) + } + + private func consume( + object: Object + ) where ObjectWillChangePublisher == ObservableObjectPublisher { + bag.insert( + object.objectWillChange.sink( + receiveCompletion: { _ in }, + receiveValue: { [weak self] _ in + self?.objectWillChange.send() + } + ) + ) + } } diff --git a/Tests/FeedbackKitTests/FeedbackKitTests.swift b/Tests/FeedbackKitTests/FeedbackKitTests.swift index 6cb9789..6929268 100644 --- a/Tests/FeedbackKitTests/FeedbackKitTests.swift +++ b/Tests/FeedbackKitTests/FeedbackKitTests.swift @@ -3,10 +3,8 @@ import XCTest final class FeedbackKitTests: XCTestCase { func testExample() throws { - // XCTest Documentation - // https://developer.apple.com/documentation/xctest + let store = FeedbackStore() - // Defining Test Cases and Test Methods - // https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods + } }