mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-05-21 23:22:08 +02:00
216 lines
7.8 KiB
Swift
216 lines
7.8 KiB
Swift
//
|
|
/*
|
|
* Copyright (c) 2021 BWI GmbH
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import MatrixSDK
|
|
|
|
@objcMembers class ServerDowntimeDefaultService : NSObject {
|
|
func nextDowntime() -> ServerDowntime? {
|
|
if let downtimes = UserDefaults.standard.object(forKey: "BWI_ServerDowntimes") as? [Data] {
|
|
var nextDowntime: ServerDowntime? = nil
|
|
|
|
for downtimeData in downtimes {
|
|
do {
|
|
let downtime = try JSONDecoder().decode(ServerDowntime.self, from: downtimeData)
|
|
|
|
if let endDate = downtime.endTime.iso8601LocalDate {
|
|
if endDate.timeIntervalSinceNow < 0 {
|
|
continue
|
|
}
|
|
}
|
|
if let warningStartDate = downtime.warningStartTime.iso8601LocalDate {
|
|
if warningStartDate.timeIntervalSinceNow > 0 {
|
|
continue
|
|
}
|
|
if let warningStartDateSaved = nextDowntime?.warningStartTime.iso8601LocalDate {
|
|
if warningStartDateSaved.timeIntervalSince(warningStartDate) > 0 {
|
|
nextDowntime = downtime
|
|
}
|
|
}
|
|
else {
|
|
nextDowntime = downtime
|
|
}
|
|
}
|
|
}
|
|
catch {
|
|
print(error.localizedDescription)
|
|
}
|
|
|
|
}
|
|
|
|
return nextDowntime
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func nextDowntimeType() -> ServerDowntimeType {
|
|
guard let downTime = self.nextDowntime() else {
|
|
return .none
|
|
}
|
|
|
|
if let endDate = downTime.endTime.iso8601LocalDate {
|
|
if endDate.timeIntervalSinceNow < 0 {
|
|
return .none
|
|
}
|
|
}
|
|
if let startDate = downTime.startTime.iso8601LocalDate {
|
|
if startDate.timeIntervalSinceNow < 0 {
|
|
return .ongoing
|
|
}
|
|
}
|
|
if let warningStartDate = downTime.warningStartTime.iso8601LocalDate {
|
|
if warningStartDate.timeIntervalSinceNow < 0 {
|
|
return .warning
|
|
}
|
|
}
|
|
|
|
return .none
|
|
}
|
|
|
|
private func readLocalFile(forName name: String) -> Data? {
|
|
do {
|
|
if let bundlePath = Bundle.main.path(forResource: name,
|
|
ofType: "json"),
|
|
let jsonData = try String(contentsOfFile: bundlePath).data(using: .utf8) {
|
|
return jsonData
|
|
}
|
|
} catch {
|
|
print(error)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func fetchDowntimes(completion: @escaping () -> Void) {
|
|
guard let exampleFetch = readLocalFile(forName: "exampleDowntime") else {
|
|
return
|
|
}
|
|
|
|
do {
|
|
if let serverDict = try JSONSerialization.jsonObject(with: exampleFetch, options: []) as? [String: Any] {
|
|
if let downtimeArray = serverDict["downtime"] as? [Any] {
|
|
var downtimes = Array<Data>()
|
|
|
|
for downtimeDict in downtimeArray {
|
|
if let serverDowntime = try? ServerDowntime(dict: downtimeDict as! [String : Any]) {
|
|
|
|
let data = try JSONEncoder().encode(serverDowntime)
|
|
downtimes.append(data)
|
|
}
|
|
}
|
|
UserDefaults.standard.set(downtimes, forKey: "BWI_ServerDowntimes")
|
|
}
|
|
|
|
if let versionsDict = serverDict["versions"] as? [String : Any] {
|
|
if let iosVersionsDict = versionsDict["ios"] as? [String : Any] {
|
|
if let validAppVersions = try? ValidAppVersions(dict: iosVersionsDict) {
|
|
let data = try JSONEncoder().encode(validAppVersions)
|
|
UserDefaults.standard.set(data, forKey: "BWI_ValidAppVersions")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch {
|
|
print(error.localizedDescription)
|
|
}
|
|
completion()
|
|
}
|
|
}
|
|
|
|
extension ServerDowntimeDefaultService : ServerDowntimeService {
|
|
|
|
@objc func isDowntimePresentable() -> Bool {
|
|
return nextDowntimeType() != .none
|
|
}
|
|
|
|
@objc func downtimeText() -> String {
|
|
var message = ""
|
|
|
|
if let downTime = self.nextDowntime(), let startDate = downTime.startTime.iso8601LocalDate, let endDate = downTime.endTime.iso8601LocalDate {
|
|
|
|
let dateFormatter = DateFormatter()
|
|
dateFormatter.dateFormat = "EE, dd.MM.yyyy HH:mm"
|
|
dateFormatter.calendar = Calendar.current
|
|
dateFormatter.timeZone = TimeZone.current
|
|
|
|
message = String.init( format: NSLocalizedString("settings_downtime_message", tableName: "Bwi", comment: ""), dateFormatter.string(from: startDate), dateFormatter.string(from: endDate))
|
|
}
|
|
|
|
return message
|
|
}
|
|
|
|
@objc func downtimeColor() -> UIColor {
|
|
switch nextDowntimeType() {
|
|
case .warning:
|
|
return .yellow
|
|
case .ongoing:
|
|
return .red
|
|
default:
|
|
return .clear
|
|
}
|
|
}
|
|
|
|
@objc func downtimeTextColor() -> UIColor {
|
|
switch nextDowntimeType() {
|
|
case .warning:
|
|
return .black
|
|
case .ongoing:
|
|
return .white
|
|
default:
|
|
return .clear
|
|
}
|
|
}
|
|
|
|
func fetchDowntimes(session: MXSession, completion: @escaping () -> Void) {
|
|
session.matrixRestClient.getDowntime(completion: { (jsonResponse, error) in
|
|
do {
|
|
if let serverDict = jsonResponse {
|
|
if let downtimeArray = serverDict["downtime"] as? [[String : Any]] {
|
|
|
|
var downtimes = Array<Data>()
|
|
|
|
for downtimeDict in downtimeArray {
|
|
if let serverDowntime = try? ServerDowntime(dict: downtimeDict) {
|
|
let data = try JSONEncoder().encode(serverDowntime)
|
|
downtimes.append(data)
|
|
}
|
|
}
|
|
|
|
UserDefaults.standard.set(downtimes, forKey: "BWI_ServerDowntimes")
|
|
}
|
|
|
|
if let versionsDict = serverDict["versions"] as? [String : Any] {
|
|
if let iosVersionsDict = versionsDict["ios"] as? [String : Any] {
|
|
if let validAppVersions = try? ValidAppVersions(dict: iosVersionsDict) {
|
|
let data = try JSONEncoder().encode(validAppVersions)
|
|
UserDefaults.standard.set(data, forKey: "BWI_ValidAppVersions")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch {
|
|
print(error.localizedDescription)
|
|
}
|
|
completion()
|
|
})
|
|
|
|
|
|
}
|
|
}
|