DateManager.swift 1.24 KB
//
//  DateManager.swift
//  browser
//
//  Created by Artem Talko on 17.10.2023.
//

import Foundation
import UIKit


final class DateManager {
    static let shared = DateManager()

    let currentDate = Date()
    let currentTimestamp = Date().timeIntervalSince1970
    
    var dateFormatter: DateFormatter{
        let obj = DateFormatter()
        obj.dateFormat = "EEEE, d MMM"
        return obj
    }
    
    private init() {}
    
    func getTimeFromDB(_ date: Date?) -> String {
        let timeFormatter = DateFormatter()
        timeFormatter.dateFormat = "h:mm a"
        return timeFormatter.string(from: date ?? Date())
    }
    
    func getSubscriptionExpirationDate() {
        guard CachingManager.shared.expirationDate != nil else {
            Task {
                if let activeSubscription = SubscriptionManager.shared.purchasedSubscriptions.first {
                    switch await activeSubscription.latestTransaction {
                    case .verified(let transaction):
                        CachingManager.shared.expirationDate = transaction.expirationDate?.timeIntervalSince1970
                    default:
                        return
                    }
                }
            }
            return
        }
    }
}