构建时间段的数据模型

问题描述 投票:2回答:2

我正在构建一个提供类似于dog狗的服务的应用程序。 walk狗的人可以上传有空的日期和时间。与其让他们选择像1月1日星期一这样的实际日期,不如让他们选择一周中的任意几天以及什么时间。

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

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

[问题是,如果有人选择早上6点至早上9点,12 pm-3pm和6 pm-9pm到星期日,但他们也选择星期一6 am-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
}

进一步的解释是,如果想要让狗dog着的用户滚动浏览以查看谁有空,如果滚动的日期和时间不在发布者的日期和时间中(无法显示带有选定小时数的Sun&Mon),那么他们的帖子就不会出现在供稿中,但是如果是那一天中的某一天,并且是那些小时之一,那么他们的帖子就会出现在供稿中(例如,如果某人正在周日晚上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

UPDATE在@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)
}

我正在构建一个提供类似于dog狗的服务的应用程序。 walk狗的人可以上传有空的日期和时间。而不是选择实际的...

ios swift class datamodel
2个回答
4
投票

有很多方法可以为猫皮,但是我可以将可用性定义为日计数和时间范围:


0
投票

我建议以比您的UI允许的更一般的方式提供商店可用性。这使您可以轻松更改UI。我会将其存储为可用时间数组。每个availablTime将包含一周中的一天,开始时间和结束时间。

© www.soinside.com 2019 - 2024. All rights reserved.