fix: Make translatedContent a transient CoreData property

This commit is contained in:
Marcus Kida 2022-12-14 09:35:45 +01:00
parent 518941b10c
commit 855f626c42
No known key found for this signature in database
GPG Key ID: 19FF64E08013CA40
4 changed files with 16 additions and 8 deletions

View File

@ -60,9 +60,10 @@ private extension DataSourceFacade {
static func translateAndApply(provider: Provider, status: Status) async throws { static func translateAndApply(provider: Provider, status: Status) async throws {
do { do {
status.translatedContent = try await translateStatus(provider: provider, status: status) let translated = try await translateStatus(provider: provider, status: status)
status.update(translatedContent: translated)
} catch { } catch {
status.translatedContent = nil status.update(translatedContent: nil)
throw TranslationFailure.emptyOrInvalidResponse throw TranslationFailure.emptyOrInvalidResponse
} }
} }

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="21512" systemVersion="22A400" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier=""> <model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="21512" systemVersion="22C65" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="Application" representedClassName="CoreDataStack.Application" syncable="YES"> <entity name="Application" representedClassName="CoreDataStack.Application" syncable="YES">
<attribute name="identifier" optional="YES" attributeType="UUID" usesScalarValueType="NO"/> <attribute name="identifier" optional="YES" attributeType="UUID" usesScalarValueType="NO"/>
<attribute name="name" attributeType="String"/> <attribute name="name" attributeType="String"/>
@ -201,6 +201,7 @@
<attribute name="sensitive" attributeType="Boolean" usesScalarValueType="YES"/> <attribute name="sensitive" attributeType="Boolean" usesScalarValueType="YES"/>
<attribute name="spoilerText" optional="YES" attributeType="String"/> <attribute name="spoilerText" optional="YES" attributeType="String"/>
<attribute name="text" optional="YES" attributeType="String"/> <attribute name="text" optional="YES" attributeType="String"/>
<attribute name="translatedContent" optional="YES" transient="YES" attributeType="String"/>
<attribute name="updatedAt" attributeType="Date" usesScalarValueType="NO"/> <attribute name="updatedAt" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="uri" attributeType="String"/> <attribute name="uri" attributeType="String"/>
<attribute name="url" optional="YES" attributeType="String"/> <attribute name="url" optional="YES" attributeType="String"/>

View File

@ -100,7 +100,8 @@ public final class Status: NSManagedObject {
// sourcery: autoUpdatableObject // sourcery: autoUpdatableObject
@NSManaged public private(set) var revealedAt: Date? @NSManaged public private(set) var revealedAt: Date?
@Published public var translatedContent: String? // sourcery: autoUpdatableObject
@NSManaged public private(set) var translatedContent: String?
} }
extension Status { extension Status {
@ -497,6 +498,11 @@ extension Status: AutoUpdatableObject {
self.revealedAt = revealedAt self.revealedAt = revealedAt
} }
} }
public func update(translatedContent: String?) {
if self.translatedContent != translatedContent {
self.translatedContent = translatedContent
}
}
public func update(attachments: [MastodonAttachment]) { public func update(attachments: [MastodonAttachment]) {
if self.attachments != attachments { if self.attachments != attachments {
self.attachments = attachments self.attachments = attachments

View File

@ -57,8 +57,8 @@ extension StatusView {
configureFilter(status: status) configureFilter(status: status)
viewModel.originalStatus = status viewModel.originalStatus = status
[ [
status.$translatedContent, status.publisher(for: \.translatedContent),
status.reblog?.$translatedContent status.reblog?.publisher(for: \.translatedContent)
].compactMap { $0 } ].compactMap { $0 }
.last? .last?
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
@ -245,8 +245,8 @@ extension StatusView {
func revertTranslation() { func revertTranslation() {
guard let originalStatus = viewModel.originalStatus else { return } guard let originalStatus = viewModel.originalStatus else { return }
viewModel.translatedFromLanguage = nil viewModel.translatedFromLanguage = nil
originalStatus.reblog?.translatedContent = nil originalStatus.reblog?.update(translatedContent: nil)
originalStatus.translatedContent = nil originalStatus.update(translatedContent: nil)
configure(status: originalStatus) configure(status: originalStatus)
} }