Nextcloud login flow v2, Network code rewrite

This commit is contained in:
Vicnet
2023-09-20 13:25:25 +02:00
parent 0f16b164d6
commit 26dd5c34ff
16 changed files with 504 additions and 364 deletions

View File

@@ -0,0 +1,34 @@
//
// DateFormatterExtension.swift
// Nextcloud Cookbook iOS Client
//
// Created by Vincent Meilinger on 14.09.23.
//
import Foundation
extension Formatter {
static let positional: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
return formatter
}()
}
func formatDate(duration: String) -> String {
var duration = duration
if duration.hasPrefix("PT") { duration.removeFirst(2) }
let hour, minute, second: Double
if let index = duration.firstIndex(of: "H") {
hour = Double(duration[..<index]) ?? 0
duration.removeSubrange(...index)
} else { hour = 0 }
if let index = duration.firstIndex(of: "M") {
minute = Double(duration[..<index]) ?? 0
duration.removeSubrange(...index)
} else { minute = 0 }
if let index = duration.firstIndex(of: "S") {
second = Double(duration[..<index]) ?? 0
} else { second = 0 }
return Formatter.positional.string(from: hour * 3600 + minute * 60 + second) ?? "0:00"
}

View File

@@ -0,0 +1,33 @@
//
// JSONCoderExtension.swift
// Nextcloud Cookbook iOS Client
//
// Created by Vincent Meilinger on 20.09.23.
//
import Foundation
extension JSONDecoder {
static func safeDecode<T: Decodable>(_ data: Data) -> T? {
let decoder = JSONDecoder()
do {
print("Decoding type ", T.self, " ...")
return try decoder.decode(T.self, from: data)
} catch (let error) {
print("JSONDecoder - safeDecode(): Failed to decode data.")
print("Error: ", error)
return nil
}
}
}
extension JSONEncoder {
static func safeEncode<T: Encodable>(_ object: T) -> Data? {
do {
return try JSONEncoder().encode(object)
} catch {
print("JSONDecoder - safeEncode(): Could not encode object \(T.self)")
}
return nil
}
}