Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
helje5 committed Sep 29, 2023
2 parents 8549e0b + a7a4659 commit 9c61f0d
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 27 deletions.
4 changes: 3 additions & 1 deletion ManagedToDos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
E8548E8C2AC45EDD00809E08 /* ToDoEditor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ToDoEditor.swift; sourceTree = "<group>"; };
E8548E8D2AC45EDD00809E08 /* ToDoCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ToDoCell.swift; sourceTree = "<group>"; };
E8548E8E2AC45EDD00809E08 /* ToDoListsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ToDoListsView.swift; sourceTree = "<group>"; };
E8F59F782AC6158700E01163 /* ManagedToDos.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = ManagedToDos.xctestplan; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -55,6 +56,7 @@
E8548E592AC454F000809E08 = {
isa = PBXGroup;
children = (
E8F59F782AC6158700E01163 /* ManagedToDos.xctestplan */,
E8548E7B2AC455D500809E08 /* README.md */,
E8548E642AC454F000809E08 /* ManagedToDos */,
E8548E632AC454F000809E08 /* Products */,
Expand Down Expand Up @@ -425,7 +427,7 @@
repositoryURL = "[email protected]:Data-swift/ManagedModels.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.5.2;
minimumVersion = 0.6.0;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
36 changes: 36 additions & 0 deletions ManagedToDos.xctestplan
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"configurations" : [
{
"id" : "AB016975-AF24-4306-B5DC-F5E394956E9D",
"name" : "Test Scheme Action",
"options" : {

}
}
],
"defaultOptions" : {
"codeCoverage" : false,
"targetForVariableExpansion" : {
"containerPath" : "container:ManagedToDos.xcodeproj",
"identifier" : "E8548E612AC454F000809E08",
"name" : "ManagedToDos"
}
},
"testTargets" : [
{
"target" : {
"containerPath" : "container:..\/..\/..\/..\/dev\/Swift\/Data.swift\/ManagedModels",
"identifier" : "ManagedModelMacrosTests",
"name" : "ManagedModelMacrosTests"
}
},
{
"target" : {
"containerPath" : "container:..\/..\/..\/..\/dev\/Swift\/Data.swift\/ManagedModels",
"identifier" : "ManagedModelTests",
"name" : "ManagedModelTests"
}
}
],
"version" : 1
}
22 changes: 17 additions & 5 deletions ManagedToDos/Models/ToDo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,29 @@ final class ToDo: NSManagedObject {

var title : String
var isDone : Bool
var priority : Int
var priority : Priority
var created : Date
var due : Date?
var list : ToDoList

enum Priority: Int, Comparable, CaseIterable {
case veryLow = 1
case low = 2
case medium = 3
case high = 4
case veryHigh = 5

static func < (lhs: Self, rhs: Self) -> Bool {
lhs.rawValue < rhs.rawValue
}
}

convenience init(list : ToDoList,
title : String,
isDone : Bool = false,
priority : Int = 3,
created : Date = Date(),
due : Date? = nil)
isDone : Bool = false,
priority : Priority = .medium,
created : Date = Date(),
due : Date? = nil)
{
// This is important so that the objects don't end up in different
// contexts.
Expand Down
7 changes: 3 additions & 4 deletions ManagedToDos/Models/ToDoList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import ManagedModels
@Model
final class ToDoList: NSManagedObject {

var title : String
var toDos : Set<ToDo> // currently can't use `[ ToDo ]` here.
var title = ""
var toDos = [ ToDo ]()

convenience init(title: String) {
self.init()
self.title = title
self.toDos = []
}

var hasOverdueItems : Bool { toDos.contains { $0.isOverDue && !$0.isDone } }
Expand Down
4 changes: 2 additions & 2 deletions ManagedToDos/Preview Content/PreviewData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum PreviewData {
// The insert is not even necessary, done by init
ctx.insert(ToDo(list: list, title: "Apples"))
ctx.insert(ToDo(list: list, title: "Oranges"))
ctx.insert(ToDo(list: list, title: "Juice", priority: 4))
ctx.insert(ToDo(list: list, title: "Juice", priority: .high))
}
do {
let list = ToDoList(title: "To-Do's")
Expand All @@ -41,7 +41,7 @@ enum PreviewData {
ctx.insert(ToDo(list: list, title: "Wash 🚗",
due: Date().advanced(by: 7200)))
ctx.insert(ToDo(list: list, title: "Rebuild SwiftData",
priority: 2,
priority: .low,
due: Date().advanced(by: -7200)))
ctx.insert(ToDo(list: list, title: "Do Groceries"))
}
Expand Down
6 changes: 3 additions & 3 deletions ManagedToDos/Views/ToDoCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ struct ToDoCell: View {

Spacer()

if toDo.priority == 4 { Text("❗️") }
else if toDo.priority > 4 { Text("‼️") }
if toDo.priority == .high { Text("❗️") }
else if toDo.priority >= .veryHigh { Text("‼️") }
}

if let due = toDo.due {
Text("\(due, format: .dateTime)")
.font(.footnote)
.foregroundColor(toDo.isOverDue && toDo.priority > 2
.foregroundColor(toDo.isOverDue && toDo.priority > .low
? .red : nil)
}
}
Expand Down
13 changes: 6 additions & 7 deletions ManagedToDos/Views/ToDoEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,13 @@ struct ToDoEditor: View {

if !toDo.isDone {
Picker("Priority", selection: $toDo.priority) {
ForEach(1...5, id: \.self) { priority in
ForEach(ToDo.Priority.allCases, id: \.self) { priority in
switch priority {
case 1: Text("Very low")
case 2: Text("Low")
case 3: Text("Medium")
case 4: Text("High")
case 5: Text("Very High")
default: Text("\(priority)")
case .veryLow : Text("Very low")
case .low : Text("Low")
case .medium : Text("Medium")
case .high : Text("High")
case .veryHigh : Text("Very High")
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions ManagedToDos/Views/ToDoListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ struct ToDoListView: View {
_toDos = .init(
filter: NSPredicate(format: "list = %@", toDoList),
sort: [
SortDescriptor(\ToDo.isDone, order: .forward),
SortDescriptor(\ToDo.priority, order: .reverse),
SortDescriptor(\ToDo.due, order: .forward),
SortDescriptor(\ToDo.created, order: .reverse)
.init(\.isDone, order: .forward),
.init(\.priority, order: .reverse),
.init(\.due, order: .forward),
.init(\.created, order: .reverse)
],
animation: .default
)
Expand All @@ -47,7 +47,7 @@ struct ToDoListView: View {
// MARK: - Actions

private func addItem() {
let toDo = ToDo(list: toDoList, title: "", priority: 3)
let toDo = ToDo(list: toDoList, title: "", priority: .medium)
navigationPath.append(toDo)
try? viewContext.save()
}
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
[ManagedModels](https://github.com/Data-swift/ManagedModels/),
which adds some SwiftData-like `@Model` support to regular CoreData.

Blog article describing ManagedModels: [`@Model` for CoreData](https://www.alwaysrightinstitute.com/managedmodels/).


### Models

Expand Down Expand Up @@ -56,6 +58,7 @@ ManagedModels has no other dependencies.
#### Links

- [ManagedModels](https://github.com/Data-swift/ManagedModels/)
- Blog article: [`@Model` for CoreData](https://www.alwaysrightinstitute.com/managedmodels/).
- Apple:
- [CoreData](https://developer.apple.com/documentation/coredata)
- [SwiftData](https://developer.apple.com/documentation/swiftdata)
Expand Down

0 comments on commit 9c61f0d

Please sign in to comment.