Skip to content

Commit

Permalink
Refactor logging settings to use DittoLogLevel directly
Browse files Browse the repository at this point in the history
- Replaced `LoggingOptions` with `DittoLogLevel` from the Ditto SDK, removing the reference to custom wrapper type.
- Added `isLoggingEnabled` state to separately track logging enablement alongside log levels.
- Updated `SettingsModel`:
  - Refactored `storedLoggingOption` to migrate legacy `UserDefaults` values to `DittoLogLevel` and clean up old keys.
  - Simplified future storage/retrieval to use `DittoLogLevel` directly.
- Modified `SettingsView` constructor to take a `Ditto` instance instead of `LoggingOptions`.

These changes simplify logging logic, align with the updated Ditto SDK, and ensure backward compatibility.
  • Loading branch information
rdas-ditto committed Jan 16, 2025
1 parent 6c78740 commit d14398c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 19 deletions.
2 changes: 1 addition & 1 deletion iOS/DittoPOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@
repositoryURL = "https://github.com/getditto/DittoSwiftTools";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 6.0.0;
minimumVersion = 7.0.0;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
21 changes: 12 additions & 9 deletions iOS/DittoPOS/Data/DittoService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ final class DittoInstance {
}
}

let defaultLoggingOption: DittoLogger.LoggingOptions = .error
let defaultLoggingOption: DittoLogLevel = .error

// Used to constrain orders subscriptions to 1 day old or newer
let OrderTTL: TimeInterval = 60 * 60 * 24 //24hrs

@MainActor class DittoService: ObservableObject {
@Published var loggingOption: DittoLogger.LoggingOptions

@State var isLoggingEnabled = DittoLogger.enabled

@Published var loggingOption: DittoLogLevel
private var cancellables = Set<AnyCancellable>()

@Published private(set) var allLocations = [Location]()
Expand Down Expand Up @@ -283,14 +286,14 @@ extension DittoService {
extension DittoService {

private func resetLogging() {
let logOption = Settings.dittoLoggingOption
switch logOption {
case .disabled:
DittoLogger.enabled = false
default:
DittoLogger.enabled = true
DittoLogger.minimumLogLevel = DittoLogLevel(rawValue: logOption.rawValue)!
// Update DittoLogger's enabled state based on the @State var
DittoLogger.enabled = isLoggingEnabled

if isLoggingEnabled {
// Set the minimum log level based on Settings.dittoLoggingOption
DittoLogger.minimumLogLevel = Settings.dittoLoggingOption

// Configure the log file URL if available
if let logFileURL = LogFileConfig.createLogFileURL() {
DittoLogger.setLogFileURL(logFileURL)
}
Expand Down
60 changes: 52 additions & 8 deletions iOS/DittoPOS/Settings/SettingsModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ extension Settings {

extension UserDefaults {
public struct UserDefaultsKeys {
static var loggingOption: String { "live.ditto.DittoPOS.loggingOption" }
static var currentLocationId: String { "live.ditto.DittoPOS.currentLocationId" }
static var selectedTab: String { "live.ditto.DittoPOS.selectedTab" }
//rename: keep legacy "userKey" key
Expand Down Expand Up @@ -118,16 +117,61 @@ extension UserDefaults {
}
}

var storedLoggingOption: DittoLogger.LoggingOptions {
var storedLoggingOption: DittoLogLevel {
get {
let logOption = integer(forKey: UserDefaultsKeys.loggingOption)
guard logOption != 0 else {
return DittoLogger.LoggingOptions(rawValue: defaultLoggingOption.rawValue)!
/// Migrates the stored logging option from the legacy key to the new system.
///
/// This function checks if a value exists under the legacy key (`"live.ditto.DittoPOS.loggingOption"`) in `UserDefaults`.
/// If a non-zero value is found, it is manually mapped to the appropriate `DittoLogLevel`
/// and stored using the new storage mechanism. The legacy key is removed after migration
/// to ensure it is no longer used.
///
/// - Important: This function must be called before accessing or restoring the current logging option
/// to ensure any legacy values are migrated correctly.
func migrateFromLegacyStorage() {
let oldKey = "live.ditto.DittoPOS.loggingOption"

// Check if a value exists for the old key
guard object(forKey: oldKey) != nil else { return }

// Retrieve the integer value stored with the legacy key
let rawValue = integer(forKey: oldKey)

// Manually map legacy values to the correct DittoLogLevel
let mappedLogLevel: DittoLogLevel?
switch rawValue {
case 0:
mappedLogLevel = nil // 0 indicates no logging, skip migration
case 1:
mappedLogLevel = .error
case 2:
mappedLogLevel = .warning
case 3:
mappedLogLevel = .info
case 4:
mappedLogLevel = .debug
default:
mappedLogLevel = nil // Unknown values, skip migration
}

// Save the mapped value if valid
if let logLevel = mappedLogLevel {
logLevel.saveToStorage()
}

// Remove the old key
removeObject(forKey: oldKey)
}
return DittoLogger.LoggingOptions(rawValue: logOption)!

// Perform migration before restoring the value
migrateFromLegacyStorage()

// Restore the log level from storage, defaulting to `.error` if no value is found
return DittoLogLevel.restoreFromStorage()
}
set(value) {
set(value.rawValue, forKey: UserDefaultsKeys.loggingOption)
set {
// Save the new log level to storage
newValue.saveToStorage()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion iOS/DittoPOS/Settings/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct SettingsView: View {
}
}
Section(header: Text("Exports")) {
NavigationLink(destination: LoggingDetailsView(loggingOption: $dittoService.loggingOption)) {
NavigationLink(destination: LoggingDetailsView(ditto: ditto)) {
DittoToolsListItem(title: "Logging", systemImage: "square.split.1x2", color: .green)
}

Expand Down

0 comments on commit d14398c

Please sign in to comment.