gpt4 book ai didi

ios - 为时间段构建数据模型

转载 作者:可可西里 更新时间:2023-11-01 00:38:31 25 4
gpt4 key购买 nike

我正在构建一个应用程序,提供类似于遛狗的服务。遛狗的人可以上传可以遛狗的日期和时间。我没有让他们选择像 1 月 1 日星期一这样的实际日期,而是让他们选择一周中的任何几天以及他们有空的任何时间。

我遇到的问题是我不知道如何为它构建数据模型。

照片中的是一个带有单元格的 collectionView,在每个单元格中我都显示了他们可以选择的可用日期和时间段。一周中的每一天都有 7 个相同的时间段供想要遛狗的用户选择。

问题是,如果有人选择周日上午 6 点至上午 9 点、中午 2 点至下午 3 点和下午 6 点至晚上 9 点,但他们还选择周一上午 6 点至 9 点,我该如何构建一个可以区分日期和时间的数据模型。例如星期天早上 6 点到 9 点和星期一早上 6 点到 9 点,如何区分?这些时间段应该是 double 还是字符串?

这是我目前用于 collectionView 数据源和单元格的内容:

// the collectionView's data source
var tableData = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

//cellForItem
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: availabilityCell, for: indexPath) as! AvailabilityCell

cell.clearCellForReuse()
cell.dayOfWeek = tableData[indexPath.item]

// inside the AvailabilityCell itself
var dayOfWeek: String? {
didSet {

dayOfWeekLabel.text = dayOfWeek
}
}

func clearCellForReuse() {

dayOfWeekLabel.text = nil
// deselect whatever radio buttons were selected to prevent scrolling issues
}

进一步解释一下,当想要遛狗的用户滚动查看谁有空时,如果他们滚动的日期和时间不在发布者的任何日期和时间上,最终会发生什么(周日和周一的所选时间)不可用,那么他们的帖子不应出现在提要中,但如果是那些日子和其中一个小时,那么他们的帖子将出现在提要中(在示例中,如果有人在周日晚上 10 点滚动浏览此帖子不应该出现)。数据模型中的任何内容都将与帖子当前滚动的任何日期和时间进行比较。我在后端使用 Firebase。

我想出的东西相当复杂,这就是为什么我需要一些更合理的东西。

class Availability {

var monday: String?
var tuesday: String?
var wednesday: String?
var thursday: String?
var friday: String?
var saturday: String?
var sunday: String?

var slotOne: Double? // sunday 6am-9am I was thinking about putting military hours here that's why I used a double
var slotTwo: Double? // sunday 9am-12pm
var slotTwo: Double? // sunday 12pm-3pm
// these slots would continue all through saturday and this doesn't seem like the correct way to do this. There would be 49 slots in total (7 days of the week * 7 different slots per day)
}

我也考虑过可能将它们分成不同的数据模型,例如星期一类、星期二类等,但这似乎也不起作用,因为对于 collectionView 数据源,它们都必须是相同的数据类型。

enter image description here

更新在@rob 的回答中,他给了我一些见解,让我可以对我的代码进行一些更改。我还在消化它,但我仍然有几个问题。他做了一个cool project that shows his idea.

1- 由于我将数据保存到 Firebase 数据库,数据应该如何结构化才能保存?可以有多天时间相似。

2- 我仍然在思考 rob 的代码,因为我以前从未处理过时间范围,所以这对我来说很陌生。我仍然不知道要根据什么进行排序,尤其是针对回调内部的时间范围

// someone is looking for a dog walker on Sunday at 10pm so the initial user who posted their post shouldn't appear in the feed

let postsRef = Database().database.reference().child("posts")

postsRef.observe( .value, with: { (snapshot) in

guard let availabilityDict = snapshot.value as? [String: Any] else { return }

let availability = Availability(dictionary: availabilityDict)

let currentDayOfWeek = dayOfTheWeek()

// using rob;s code this compares the days and it 100% works
if currentDayOfWeek != availability.dayOfWeek.text {

// don't add this post to the array
return
}

let currentTime = Calendar.current.dateComponents([.hour,.minute,.second], from: Date())

// how to compare the time slots to the current time?
if currentTime != availability.??? {
// don't add this post to the array
return
}

// if it makes this far then the day and the time slots match up to append it to the array to get scrolled
})

func dayOfTheWeek() -> String? {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE"
return dateFormatter.stringFromDate(self)
}

最佳答案

给猫皮的方法有很多种,但我可能会将可用性定义为日期枚举和时间范围:

struct Availability {
let dayOfWeek: DayOfWeek
let timeRange: TimeRange
}

你星期几可能是:

enum DayOfWeek: String, CaseIterable {
case sunday, monday, tuesday, wednesday, thursday, friday, saturday
}

或者你也可以这样做:

enum DayOfWeek: Int, CaseIterable {
case sunday = 0, monday, tuesday, wednesday, thursday, friday, saturday
}

它们是 IntString 的优缺点。字符串表示形式在 Firestore 基于网络的 UI 中更易于阅读。整数表示提供了更容易排序的潜力。

您的时间范围:

typealias Time = Double
typealias TimeRange = Range<Time>

extension TimeRange {
static let allCases: [TimeRange] = [
6 ..< 9,
9 ..< 12,
12 ..< 15,
15 ..< 18,
18 ..< 21,
21 ..< 24,
24 ..< 30
]
}

在与 Firebase 交互方面,它不理解枚举和范围,所以我定义了一个 init 方法和 dictionary 属性来映射到和从 [String: Any] 可以与 Firebase 交换的字典:

struct Availability {
let dayOfWeek: DayOfWeek
let timeRange: TimeRange

init(dayOfWeek: DayOfWeek, timeRange: TimeRange) {
self.dayOfWeek = dayOfWeek
self.timeRange = timeRange
}

init?(dictionary: [String: Any]) {
guard
let dayOfWeekRaw = dictionary["dayOfWeek"] as? DayOfWeek.RawValue,
let dayOfWeek = DayOfWeek(rawValue: dayOfWeekRaw),
let startTime = dictionary["startTime"] as? Double,
let endTime = dictionary["endTime"] as? Double
else {
return nil
}

self.dayOfWeek = dayOfWeek
self.timeRange = startTime ..< endTime
}

var dictionary: [String: Any] {
return [
"dayOfWeek": dayOfWeek.rawValue,
"startTime": timeRange.lowerBound,
"endTime": timeRange.upperBound
]
}
}

您还可以定义一些扩展以使其更易于使用,例如,

extension Availability {
func overlaps(_ availability: Availability) -> Bool {
return dayOfWeek == availability.dayOfWeek && timeRange.overlaps(availability.timeRange)
}
}

extension TimeRange {
private func string(forHour hour: Int) -> String {
switch hour % 24 {
case 0: return NSLocalizedString("Midnight", comment: "Hour text")
case 1...11: return "\(hour % 12)" + NSLocalizedString("am", comment: "Hour text")
case 12: return NSLocalizedString("Noon", comment: "Hour text")
default: return "\(hour % 12)" + NSLocalizedString("pm", comment: "Hour text")
}
}

var text: String {
return string(forHour: Int(lowerBound)) + "-" + string(forHour: Int(upperBound))
}
}

extension DayOfWeek {
var text: String {
switch self {
case .sunday: return NSLocalizedString("Sunday", comment: "DayOfWeek text")
case .monday: return NSLocalizedString("Monday", comment: "DayOfWeek text")
case .tuesday: return NSLocalizedString("Tuesday", comment: "DayOfWeek text")
case .wednesday: return NSLocalizedString("Wednesday", comment: "DayOfWeek text")
case .thursday: return NSLocalizedString("Thursday", comment: "DayOfWeek text")
case .friday: return NSLocalizedString("Friday", comment: "DayOfWeek text")
case .saturday: return NSLocalizedString("Saturday", comment: "DayOfWeek text")
}
}
}

如果你不想使用Range,你可以将TimeRange定义为一个struct:

enum DayOfWeek: String, CaseIterable {
case sunday, monday, tuesday, wednesday, thursday, friday, saturday
}

extension DayOfWeek {
var text: String {
switch self {
case .sunday: return NSLocalizedString("Sunday", comment: "DayOfWeek text")
case .monday: return NSLocalizedString("Monday", comment: "DayOfWeek text")
case .tuesday: return NSLocalizedString("Tuesday", comment: "DayOfWeek text")
case .wednesday: return NSLocalizedString("Wednesday", comment: "DayOfWeek text")
case .thursday: return NSLocalizedString("Thursday", comment: "DayOfWeek text")
case .friday: return NSLocalizedString("Friday", comment: "DayOfWeek text")
case .saturday: return NSLocalizedString("Saturday", comment: "DayOfWeek text")
}
}
}

struct TimeRange {
typealias Time = Double

let startTime: Time
let endTime: Time
}

extension TimeRange {
static let allCases: [TimeRange] = [
TimeRange(startTime: 6, endTime: 9),
TimeRange(startTime: 9, endTime: 12),
TimeRange(startTime: 12, endTime: 15),
TimeRange(startTime: 15, endTime: 18),
TimeRange(startTime: 18, endTime: 21),
TimeRange(startTime: 21, endTime: 24),
TimeRange(startTime: 24, endTime: 30)
]

func overlaps(_ availability: TimeRange) -> Bool {
return (startTime ..< endTime).overlaps(availability.startTime ..< availability.endTime)
}
}

extension TimeRange {
private func string(forHour hour: Int) -> String {
switch hour % 24 {
case 0: return NSLocalizedString("Midnight", comment: "Hour text")
case 1...11: return "\(hour % 12)" + NSLocalizedString("am", comment: "Hour text")
case 12: return NSLocalizedString("Noon", comment: "Hour text")
default: return "\(hour % 12)" + NSLocalizedString("pm", comment: "Hour text")
}
}

var text: String {
return string(forHour: Int(startTime)) + "-" + string(forHour: Int(endTime))
}
}

struct Availability {
let dayOfWeek: DayOfWeek
let timeRange: TimeRange

init(dayOfWeek: DayOfWeek, timeRange: TimeRange) {
self.dayOfWeek = dayOfWeek
self.timeRange = timeRange
}

init?(dictionary: [String: Any]) {
guard
let dayOfWeekRaw = dictionary["dayOfWeek"] as? DayOfWeek.RawValue,
let dayOfWeek = DayOfWeek(rawValue: dayOfWeekRaw),
let startTime = dictionary["startTime"] as? Double,
let endTime = dictionary["endTime"] as? Double
else {
return nil
}

self.dayOfWeek = dayOfWeek
self.timeRange = TimeRange(startTime: startTime, endTime: endTime)
}

var dictionary: [String: Any] {
return [
"dayOfWeek": dayOfWeek.rawValue,
"startTime": timeRange.startTime,
"endTime": timeRange.endTime
]
}
}

extension Availability {
func overlaps(_ availability: Availability) -> Bool {
return dayOfWeek == availability.dayOfWeek && timeRange.overlaps(availability.timeRange)
}
}

关于ios - 为时间段构建数据模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55989075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com