Created
November 19, 2023 21:13
-
-
Save colinfwren/5927f57508068bf7e4d5e9ec1a449f1e to your computer and use it in GitHub Desktop.
Date Test App ViewModel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftData | |
import Foundation | |
enum DateGranularity: Codable { | |
case year | |
case month | |
case week | |
case day | |
} | |
class ApproachTodoListViewModel: Identifiable { | |
var modelContext: ModelContext | |
var title: String | |
var todos: [ApproachModel]() | |
var granularity: DateGranularity | |
// approach specific details here for how to handle dates | |
init(modelContext: ModelContext, title: String, granularity: DateGranularity) { | |
self.modelContext = modelContext | |
self.title = title | |
self.granularity = granularity | |
fetchData() | |
} | |
func fetchYearData() { | |
// implementation to fetch todos for year granularity | |
} | |
func fetchMonthData() { | |
// implementation to fetch todos for month granularity | |
} | |
func fetchWeekData() { | |
// implementation to fetch todos for week granularity | |
} | |
func fetchDayData() { | |
// implementation to fetch todos for day granularity | |
} | |
func fetchData() { | |
switch self.granularity { | |
case .year: | |
fetchYearData() | |
case .month: | |
fetchMonthData() | |
case .week: | |
fetchWeekData() | |
case .day: | |
fetchDayData() | |
} | |
} | |
} | |
extension ApproachContentView { | |
@Observable | |
class ApproachViewModel { | |
var modelContext: ModelContext | |
var todoLists: [ApproachTodoListViewModel] = [] | |
init(modelContext: ModelContext) { | |
self.modelContext = modelContext | |
self.todoLists = [ | |
ApproachTodoListViewModel(modelContext: modelContext, title: "Example Todo List", granularity: .year) | |
// other todo lists | |
] | |
fetchData() | |
} | |
func addSamples() { | |
// Create instances of the Approach's data model for each test case and add to the modelContext | |
let testTodo = ApproachModel(title: "Test todo", shownInYear: false, shownInMonth: false, shownInWeek: true, shownInDay: true) | |
modelContext.insert(testTodo) | |
fetchData() | |
} | |
func fetchData() { | |
self.todoLists.forEach{ todoList in | |
todoList.fetchData() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment