【发布时间】:2019-09-23 03:18:10
【问题描述】:
我正在构建一个应用程序,该应用程序提供类似于遛狗的服务。遛狗的人可以上传他们可用的日期和时间。我没有让他们选择像 Mon Jan 1st 这样的实际日期,而是让他们选择一周中的任何一天以及他们可用的任何时间。
我遇到的问题是我不知道如何为其构建数据模型。
照片中的内容是带有一个单元格的 collectionView,在每个单元格中我显示了他们可以选择的可用日期和时间段。一周中的每一天都有相同的 7 个时间段供想要成为遛狗者的用户选择。
问题是,如果有人选择 Sun 6am-9am、12pm-3pm 和 6pm-9pm,但他们也选择 Mon 6am-9m,我如何构建可以区分日期和时间的数据模型。例如周日早上 6 点到 9 点和周一早上 6 点到 9 点,如何区分?这些时隙应该是双精度还是字符串?
这是我目前用于 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
}
为了进一步解释,最终会发生的情况是,当希望他们的狗走路的用户滚动查看谁可用时,如果他们滚动的日期和时间不在发帖人的任何日期和时间(Sun & Mon with the selected hours) 不可用,则他们的帖子不应出现在提要中,但如果是其中一天和这些时间之一,那么他们的帖子将出现在提要中(在示例中,如果有人在周日晚上 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 数据源都必须是相同的数据类型。
更新 在@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)
}
【问题讨论】: