yattee/Model/Search/SearchQuery.swift

75 lines
1.8 KiB
Swift
Raw Normal View History

2021-07-22 12:43:13 +00:00
import Defaults
2021-07-07 22:39:18 +00:00
import Foundation
final class SearchQuery: ObservableObject {
2021-11-01 21:56:18 +00:00
enum Date: String, CaseIterable, Identifiable {
case any, hour, today, week, month, year
2021-07-22 12:43:13 +00:00
var id: SearchQuery.Date.RawValue {
rawValue
}
var name: String {
rawValue.capitalized
}
}
2021-11-01 21:56:18 +00:00
enum Duration: String, CaseIterable, Identifiable {
case any, short, long
2021-07-22 12:43:13 +00:00
var id: SearchQuery.Duration.RawValue {
rawValue
}
var name: String {
rawValue.capitalized
}
}
2021-11-01 21:56:18 +00:00
enum SortOrder: String, CaseIterable, Identifiable {
2021-07-22 12:43:13 +00:00
case relevance, rating, uploadDate, viewCount
var id: SearchQuery.SortOrder.RawValue {
rawValue
}
var name: String {
switch self {
case .uploadDate:
2021-09-25 12:17:58 +00:00
return "Date"
2021-07-22 12:43:13 +00:00
case .viewCount:
2021-09-25 12:17:58 +00:00
return "Views"
2021-07-22 12:43:13 +00:00
default:
return rawValue.capitalized
}
}
var parameter: String {
switch self {
case .uploadDate:
return "upload_date"
case .viewCount:
return "view_count"
default:
return rawValue
}
}
}
2021-07-07 22:39:18 +00:00
@Published var query: String
2021-07-22 12:43:13 +00:00
@Published var sortBy: SearchQuery.SortOrder = .relevance
@Published var date: SearchQuery.Date? = .month
@Published var duration: SearchQuery.Duration?
2021-07-07 22:39:18 +00:00
init(query: String = "", sortBy: SearchQuery.SortOrder = .relevance, date: SearchQuery.Date? = nil, duration: SearchQuery.Duration? = nil) {
2021-07-07 22:39:18 +00:00
self.query = query
self.sortBy = sortBy
self.date = date
self.duration = duration
}
2021-07-11 20:52:49 +00:00
var isEmpty: Bool {
query.isEmpty
}
2021-07-07 22:39:18 +00:00
}