HistoryToolbarMenuCases.swift 1.56 KB
//
//  HistoryToolbar.swift
//  browser
//
//  Created by Artem Talko on 14.11.2023.
//

import UIKit

enum menuCases: CaseIterable {
    case allTime
    case lastMonth
    case todayAndYesterday
    case today
    case lastHour
    //MARK: todo
    var action: UIAction {
        switch self {
        case .allTime:
            return UIAction(title: "All time") { _ in
                HistoryDBManager.shared.cleanHistory()
                
            }
        case .lastMonth:
            return UIAction(title: "The last 30 days") { _ in
                let lastMonth = Calendar.current.date(byAdding: .day, value: -30, to: Date()) ?? Date()
                HistoryDBManager.shared.deleteHistoryInRange(startDate: lastMonth)
            }
        case .todayAndYesterday:
            return UIAction(title: "Today and yesterday") { _ in
                let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: Date()) ?? Date()
                HistoryDBManager.shared.deleteHistoryInRange(startDate: yesterday)
            }
        case .today:
            return UIAction(title: "Today") { _ in
                let startOfToday = Calendar.current.startOfDay(for: Date())
                HistoryDBManager.shared.deleteHistoryInRange(startDate: startOfToday)
            }
        case .lastHour:
            return UIAction(title: "The last hour") { _ in
                let lastHour = Calendar.current.date(byAdding: .hour, value: -1, to: Date()) ?? Date()
                HistoryDBManager.shared.deleteHistoryInRange(startDate: lastHour)
            }
        }
    }
}