-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
111 additions
and
99 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import CoreData | ||
import Factory | ||
import Foundation | ||
|
||
@Observable | ||
final class TagsForEntryViewModel { | ||
@ObservationIgnored | ||
@Injected(\.wallabagSession) private var session | ||
|
||
var tags: [Tag] = [] | ||
var entry: Entry? | ||
var isLoading = false | ||
|
||
@ObservationIgnored | ||
@CoreDataViewContext var coreDataContext: NSManagedObjectContext | ||
|
||
@MainActor | ||
func load(for entry: Entry) async { | ||
tags = (try? coreDataContext.fetch(Tag.fetchRequestSorted())) ?? [] | ||
tags.filter { tag in | ||
entry.tags.contains(tag) | ||
}.forEach { | ||
$0.isChecked = true | ||
} | ||
} | ||
|
||
func toggle(tag: Tag, for entry: Entry) async { | ||
defer { | ||
isLoading = false | ||
} | ||
isLoading = true | ||
if tag.isChecked { | ||
await delete(tag: tag, for: entry) | ||
} else { | ||
await add(tag: tag, for: entry) | ||
} | ||
|
||
await load(for: entry) | ||
} | ||
|
||
func add(tag: String, for entry: Entry) async { | ||
try? await session.add(tag: tag, for: entry) | ||
await load(for: entry) | ||
} | ||
|
||
private func add(tag: Tag, for entry: Entry) async { | ||
await add(tag: tag.label, for: entry) | ||
tag.isChecked = true | ||
} | ||
|
||
private func delete(tag: Tag, for entry: Entry) async { | ||
tag.isChecked = false | ||
try? await session.delete(tag: tag, for: entry) | ||
} | ||
} |
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