Skip to content

Commit

Permalink
Make User model updates more like Goal and DataPoint (#518)
Browse files Browse the repository at this point in the history
Change the structure of User creation and update code to more closely
match Goal/DataPoint. This makes User responsible for updating from json
results itself. As part of this, change the incremental user update code
from just syncing notification properties to all properties.

This is a step towards fixing timezone issues, but isn't a complete fix
as we still don't actually trigger a user refresh on a regular basis.

Testing:
Verified the app can load and login
Verify the goal notification settings toggle still works
  • Loading branch information
theospears authored Nov 18, 2024
1 parent ea4d4c3 commit 95727e0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
23 changes: 8 additions & 15 deletions BeeKit/Managers/CurrentUserManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,7 @@ public class CurrentUserManager {
try deleteUser()

let context = container.newBackgroundContext()

let _ = User(context: context,
username: responseJSON[CurrentUserManager.usernameKey].string!,
deadbeat: responseJSON["deadbeat"].boolValue,
timezone: responseJSON[CurrentUserManager.beemTZKey].string!,
defaultAlertStart: responseJSON[CurrentUserManager.defaultAlertstartKey].intValue,
defaultDeadline: responseJSON[CurrentUserManager.defaultDeadlineKey].intValue,
defaultLeadTime: responseJSON[CurrentUserManager.defaultLeadtimeKey].intValue)
let _ = User(context: context, json: responseJSON)
try context.save()

if responseJSON["deadbeat"].boolValue {
Expand All @@ -217,19 +210,19 @@ public class CurrentUserManager {
}.value
}

public func syncNotificationDefaults() async throws {
public func refreshUser() async throws {
let response = try await requestManager.get(url: "api/v1/users/\(username!).json", parameters: [:])
let responseJSON = JSON(response!)

try! modifyUser { user in
user.defaultAlertStart = responseJSON["default_alertstart"].intValue
user.defaultDeadline = responseJSON["default_deadline"].intValue
user.defaultLeadTime = responseJSON["default_leadtime"].intValue
user.updateToMatch(json: responseJSON)
}

self.set(responseJSON["default_alertstart"].number!, forKey: "default_alertstart")
self.set(responseJSON["default_deadline"].number!, forKey: "default_deadline")
self.set(responseJSON["default_leadtime"].number!, forKey: "default_leadtime")
self.set(responseJSON[CurrentUserManager.usernameKey].string!, forKey: CurrentUserManager.usernameKey)
self.set(responseJSON[CurrentUserManager.defaultAlertstartKey].number!, forKey: CurrentUserManager.defaultAlertstartKey)
self.set(responseJSON[CurrentUserManager.defaultDeadlineKey].number!, forKey: CurrentUserManager.defaultDeadlineKey)
self.set(responseJSON[CurrentUserManager.defaultLeadtimeKey].number!, forKey: CurrentUserManager.defaultLeadtimeKey)
self.set(responseJSON[CurrentUserManager.beemTZKey].string!, forKey: CurrentUserManager.beemTZKey)
}

func handleFailedSignin(_ responseError: Error, errorMessage : String?) async throws {
Expand Down
20 changes: 20 additions & 0 deletions BeeKit/Model/User.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation
import CoreData

import SwiftyJSON

@objc(User)
public class User: NSManagedObject {
@NSManaged public var username: String
Expand Down Expand Up @@ -39,6 +41,24 @@ public class User: NSManagedObject {
lastModifiedLocal = Date()
}

public init(context: NSManagedObjectContext, json: JSON) {
let entity = NSEntityDescription.entity(forEntityName: "User", in: context)!
super.init(entity: entity, insertInto: context)

self.updateToMatch(json: json)
}

public func updateToMatch(json: JSON) {
self.username = json["username"].string!
self.deadbeat = json["deadbeat"].bool!
self.timezone = json["timezone"].string!
self.defaultAlertStart = json["default_alertstart"].int!
self.defaultDeadline = json["default_deadline"].int!
self.defaultLeadTime = json["default_leadtime"].int!

lastModifiedLocal = Date()
}

@available(*, unavailable)
public init() {
fatalError()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class EditGoalNotificationsViewController : EditNotificationsViewController {
}

do {
try await ServiceLocator.currentUserManager.syncNotificationDefaults()
try await ServiceLocator.currentUserManager.refreshUser()
} catch {
self.logger.error("Error syncing notification defaults")
// TODO: Show UI failure
Expand Down

0 comments on commit 95727e0

Please sign in to comment.