Skip to content
Draft
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
88 changes: 88 additions & 0 deletions Sources/UBFoundation/Storage/Version/UBUpdateManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// UBUpdateMAnager.swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A → a

//
//
// Created by Marco Zimmermann on 09.02.23.
//

import Foundation

public class UBAppUpdateManager {
// MARK: - Shared Manager

public static let shared = UBAppUpdateManager()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private init


// MARK: - Function to call at app start

public func migrate(_ callback : ((UBAppUpdateState) -> Void)) {
if let lastVersion = lastMigratedVersion {
if lastVersion == currentVersion {
callback(.sameVersion(version: currentVersion))
} else {
let type = lastVersion.updateType(to: currentVersion)
callback(.update(fromVersion: lastVersion, toVersion: currentVersion, type: type))
}
} else {
callback(.newInstallation(toVersion: currentVersion))
}

lastMigratedVersion = currentVersion
}

// MARK: - Possibility to migrate from other mechanism used

public func overwriteLastVersion(versionString : String?) {
lastMigratedVersion = AppVersion(versionString: versionString)
}

// MARK: - Current app version

private var currentVersion : AppVersion {
AppVersion(versionString: Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String)
}

@UBUserDefault(key: "ubkit.ubupdatemanager.lastsavedversion.key", defaultValue: nil)
private var lastMigratedVersion: AppVersion?
}

public struct AppVersion : UBCodable, Equatable {

// MARK: - Init

fileprivate init(versionString: String?) {
var split = (versionString?.split(separator: ".") ?? []).map { Int(String($0)) }
major = split.removeFirst() ?? 1
minor = split.removeFirst() ?? 0
patch = split.removeFirst() ?? 0
}

// MARK: - API

var versionString : String {
"\(major).\(minor).\(patch)"
}

// MARK: - Version major.minor.patch (e.g. v3.1.4)

let major: Int
let minor: Int
let patch: Int

fileprivate func updateType(to toVersion: AppVersion) -> UBAppUpdateType {
if(self.major > toVersion.major) { return .major }
if(self.minor > toVersion.minor) { return .minor }
return .patch
}
}

public enum UBAppUpdateState {
case newInstallation(toVersion: AppVersion)
case sameVersion(version: AppVersion)
case update(fromVersion: AppVersion, toVersion: AppVersion, type: UBAppUpdateType)
}

public enum UBAppUpdateType {
case major
case minor
case patch
}