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

Make User model updates more like Goal and DataPoint #518

Merged
merged 1 commit into from
Nov 18, 2024
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
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