Skip to content

Commit

Permalink
Merge branch 'dev_contactTrick' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
mountrcg committed May 8, 2024
2 parents c2a90d3 + 12a8ecd commit 8ac2004
Show file tree
Hide file tree
Showing 7 changed files with 469 additions and 312 deletions.
18 changes: 7 additions & 11 deletions FreeAPS/Sources/Models/ContactTrickEntry.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@

struct ContactTrickEntry: JSON, Equatable {
var enabled: Bool = false
struct ContactTrickEntry: JSON, Equatable, Hashable {
var layout: ContactTrickLayout = .single
var ring1: ContactTrickLargeRing = .none
var primary: ContactTrickValue = .glucose
var top: ContactTrickValue = .none
var bottom: ContactTrickValue = .none
var contactId: String? = nil
var displayName: String? = nil
var darkMode: Bool = true
var ringWidth: Int = 7
var ringGap: Int = 2
var fontSize: Int = 100
var fontSize: Int = 300
var secondaryFontSize: Int = 250
var fontName: String = "Default Font"
var fontWeight: FontWeight = .medium
var fontTracking: FontTracking = .normal
Expand All @@ -27,54 +26,51 @@ protocol ContactTrickObserver {

extension ContactTrickEntry {
private enum CodingKeys: String, CodingKey {
case enabled
case layout
case ring1
case primary
case top
case bottom
case contactId
case displayName
case darkMode
case ringWidth
case ringGap
case fontSize
case secondaryFontSize
case fontName
case fontWeight
case fontTracking
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let enabled = try container.decodeIfPresent(Bool.self, forKey: .enabled) ?? false
let layout = try container.decodeIfPresent(ContactTrickLayout.self, forKey: .layout) ?? .single
let ring1 = try container.decodeIfPresent(ContactTrickLargeRing.self, forKey: .ring1) ?? .none
let primary = try container.decodeIfPresent(ContactTrickValue.self, forKey: .primary) ?? .glucose
let top = try container.decodeIfPresent(ContactTrickValue.self, forKey: .top) ?? .none
let bottom = try container.decodeIfPresent(ContactTrickValue.self, forKey: .bottom) ?? .none
let contactId = try container.decodeIfPresent(String.self, forKey: .contactId)
let displayName = try container.decodeIfPresent(String.self, forKey: .displayName)
let darkMode = try container.decodeIfPresent(Bool.self, forKey: .darkMode) ?? true
let ringWidth = try container.decodeIfPresent(Int.self, forKey: .ringWidth) ?? 7
let ringGap = try container.decodeIfPresent(Int.self, forKey: .ringGap) ?? 2
let fontSize = try container.decodeIfPresent(Int.self, forKey: .fontSize) ?? 100
let fontSize = try container.decodeIfPresent(Int.self, forKey: .fontSize) ?? 300
let secondaryFontSize = try container.decodeIfPresent(Int.self, forKey: .secondaryFontSize) ?? 250
let fontName = try container.decodeIfPresent(String.self, forKey: .fontName) ?? "Default Font"
let fontWeight = try container.decodeIfPresent(FontWeight.self, forKey: .fontWeight) ?? .regular
let fontTracking = try container.decodeIfPresent(FontTracking.self, forKey: .fontTracking) ?? .normal

self = ContactTrickEntry(
enabled: enabled,
layout: layout,
ring1: ring1,
primary: primary,
top: top,
bottom: bottom,
contactId: contactId,
displayName: displayName,
darkMode: darkMode,
ringWidth: ringWidth,
ringGap: ringGap,
fontSize: fontSize,
secondaryFontSize: secondaryFontSize,
fontName: fontName,
fontWeight: fontWeight,
fontTracking: fontTracking
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ enum ContactTrick {

protocol ContactTrickProvider: Provider {
var contacts: [ContactTrickEntry] { get }
func saveContacts(_ contacts: [ContactTrickEntry]) -> AnyPublisher<Void, Error>
func saveContacts(_ contacts: [ContactTrickEntry]) -> AnyPublisher<[ContactTrickEntry], Error>
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ extension ContactTrick {
?? []
}

func saveContacts(_ contacts: [ContactTrickEntry]) -> AnyPublisher<Void, Error> {
func saveContacts(_ contacts: [ContactTrickEntry]) -> AnyPublisher<[ContactTrickEntry], Error> {
Future { promise in
self.storage.save(contacts, as: OpenAPS.Settings.contactTrick)
self.contactTrickManager.updateContacts(contacts: contacts) { result in
switch result {
case .success:
promise(.success(()))
case let .success(updated):
promise(.success(updated))
case let .failure(error):
promise(.failure(error))
}
Expand Down
36 changes: 22 additions & 14 deletions FreeAPS/Sources/Modules/ContactTrick/ContactTrickStateModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ enum ContactTrickLargeRing: String, JSON, CaseIterable, Identifiable, Codable {

extension ContactTrick {
final class StateModel: BaseStateModel<Provider> {
@Published var syncInProgress = false
@Published var items: [Item] = []
@Published private(set) var syncInProgress = false
@Published private(set) var items: [Item] = []
@Published private(set) var changed: Bool = false

override func subscribe() {
items = provider.contacts.enumerated().map { index, contact in
Expand All @@ -88,24 +89,27 @@ extension ContactTrick {
entry: contact
)
}
changed = false
}

func add() {
let newItem = Item(
index: items.count,
entry: ContactTrickEntry(
enabled: false,
layout: .single,
contactId: nil,
displayName: nil,
darkMode: true,
fontSize: 100,
fontName: "Default Font",
fontWeight: .medium
)
entry: ContactTrickEntry()
)

items.append(newItem)
changed = true
}

func update(_ atIndex: Int, _ value: ContactTrickEntry) {
items[atIndex].entry = value
changed = true
}

func remove(atOffsets: IndexSet) {
items.remove(atOffsets: atOffsets)
changed = true
}

func save() {
Expand All @@ -116,9 +120,13 @@ extension ContactTrick {
provider.saveContacts(contacts)
.receive(on: DispatchQueue.main)
.sink { _ in
print("saved!")
self.syncInProgress = false
} receiveValue: {}
self.changed = false
} receiveValue: { contacts in
contacts.enumerated().forEach { index, item in
self.items[index].entry = item
}
}
.store(in: &lifetime)
}
}
Expand Down
Loading

0 comments on commit 8ac2004

Please sign in to comment.