Skip to content

Commit

Permalink
Add some basic feedback storage (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLeif authored May 5, 2024
1 parent a67e1f9 commit 816cada
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 8 deletions.
5 changes: 4 additions & 1 deletion Sources/FeedbackKit/Feedback.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
struct Feedback {
import Foundation

struct Feedback: Codable, Identifiable {
let id: UUID
let type: FeedbackType
let title: String
let description: String
Expand Down
42 changes: 40 additions & 2 deletions Sources/FeedbackKit/FeedbackStore.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion Sources/FeedbackKit/FeedbackType.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
enum FeedbackType: String {
enum FeedbackType: String, Codable {
case comment
case bug
case feature
Expand Down
26 changes: 26 additions & 0 deletions Sources/FeedbackKit/FeedbackViewModel.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import Combine
import SwiftUI

class FeedbackViewModel: ObservableObject {
private var bag: Set<AnyCancellable>

private let store: FeedbackStore

var feedback: [Feedback]

init() {
bag = Set()
store = FeedbackStore()
feedback = store.all()

consume(object: store)
}

private func consume<Object: ObservableObject>(
object: Object
) where ObjectWillChangePublisher == ObservableObjectPublisher {
bag.insert(
object.objectWillChange.sink(
receiveCompletion: { _ in },
receiveValue: { [weak self] _ in
self?.objectWillChange.send()
}
)
)
}
}
6 changes: 2 additions & 4 deletions Tests/FeedbackKitTests/FeedbackKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
}

0 comments on commit 816cada

Please sign in to comment.