-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into shendy/feedback-list-view
- Loading branch information
Showing
5 changed files
with
69 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters