Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contact image: fix for saving updates into storage #705

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ extension ContactTrick {
private let processQueue = DispatchQueue(label: "ContactTrickProvider.processQueue")

var contacts: [ContactTrickEntry] {
storage.retrieve(OpenAPS.Settings.contactTrick, as: [ContactTrickEntry].self)
?? [ContactTrickEntry](from: OpenAPS.defaults(for: OpenAPS.Settings.contactTrick))
?? []
contactTrickManager.currentContacts
}

func saveContacts(_ contacts: [ContactTrickEntry]) -> AnyPublisher<[ContactTrickEntry], Error> {
Expand Down
19 changes: 12 additions & 7 deletions FreeAPS/Sources/Services/ContactTrick/ContactTrickManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Swinject

protocol ContactTrickManager {
func updateContacts(contacts: [ContactTrickEntry], completion: @escaping (Result<[ContactTrickEntry], Error>) -> Void)
var currentContacts: [ContactTrickEntry] { get }
}

final class BaseContactTrickManager: NSObject, ContactTrickManager, Injectable {
Expand All @@ -20,6 +21,10 @@ final class BaseContactTrickManager: NSObject, ContactTrickManager, Injectable {
private var knownIds: [String] = []
private var contacts: [ContactTrickEntry] = []

var currentContacts: [ContactTrickEntry] {
contacts
}

private let coreDataStorage = CoreDataStorage()

init(resolver: Resolver) {
Expand All @@ -36,7 +41,7 @@ final class BaseContactTrickManager: NSObject, ContactTrickManager, Injectable {
knownIds = contacts.compactMap(\.contactId)

processQueue.async {
self.renderContacts()
self.renderContacts(forceSave: false)
}
}

Expand All @@ -54,13 +59,13 @@ final class BaseContactTrickManager: NSObject, ContactTrickManager, Injectable {
print("contacts cleanup, failed to delete contact \(contactId)")
}
}
self.renderContacts()
self.renderContacts(forceSave: true)
self.knownIds = self.contacts.compactMap(\.contactId)
completion(.success(self.contacts))
}
}

private func renderContacts() {
private func renderContacts(forceSave: Bool) {
if let workItem = workItem, !workItem.isCancelled {
workItem.cancel()
}
Expand Down Expand Up @@ -91,7 +96,7 @@ final class BaseContactTrickManager: NSObject, ContactTrickManager, Injectable {

let newContacts = contacts.enumerated().map { index, entry in renderContact(entry, index + 1, state) }

if newContacts != contacts {
if forceSave || newContacts != contacts {
// when we create new contacts we store the IDs, in that case we need to write into the settings storage
storage.save(newContacts, as: OpenAPS.Settings.contactTrick)
}
Expand All @@ -100,7 +105,7 @@ final class BaseContactTrickManager: NSObject, ContactTrickManager, Injectable {

workItem = DispatchWorkItem(block: {
print("in renderContacts, no updates received for more than 5 minutes")
self.renderContacts()
self.renderContacts(forceSave: false)
})
DispatchQueue.main.asyncAfter(deadline: .now() + 5 * 60 + 15, execute: workItem!)
}
Expand Down Expand Up @@ -325,10 +330,10 @@ extension BaseContactTrickManager:
SettingsObserver
{
func suggestionDidUpdate(_: Suggestion) {
renderContacts()
renderContacts(forceSave: false)
}

func settingsDidChange(_: FreeAPSSettings) {
renderContacts()
renderContacts(forceSave: false)
}
}