Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Deprecated] Tutorial: Chapter 1.2 - Implementing MVVM #97

Merged
merged 10 commits into from
May 17, 2023
Prev Previous commit
Next Next commit
✨ Add code for chapter 1 / Implementing Model, View, ViewModel (MVVM)
Signed-off-by: Peter Friese <[email protected]>
  • Loading branch information
peterfriese committed May 15, 2023
commit ae02045c58894cabb3e84d1f462c17fc4a875821
64 changes: 54 additions & 10 deletions code/frontend/MakeItSo/MakeItSo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@
objects = {

/* Begin PBXBuildFile section */
880A10AD2A1268FD006072B8 /* MakeItSo.docc in Sources */ = {isa = PBXBuildFile; fileRef = 880A10AC2A1268FD006072B8 /* MakeItSo.docc */; };
8842941329AFEEA8002B16B9 /* MakeItSoApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8842941229AFEEA8002B16B9 /* MakeItSoApp.swift */; };
8842941529AFEEA8002B16B9 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8842941429AFEEA8002B16B9 /* ContentView.swift */; };
8842941529AFEEA8002B16B9 /* RemindersListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8842941429AFEEA8002B16B9 /* RemindersListView.swift */; };
8842941729AFEEA9002B16B9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8842941629AFEEA9002B16B9 /* Assets.xcassets */; };
8842941A29AFEEA9002B16B9 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8842941929AFEEA9002B16B9 /* Preview Assets.xcassets */; };
8842942529AFF792002B16B9 /* Reminder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8842942429AFF792002B16B9 /* Reminder.swift */; };
8842942729B275B9002B16B9 /* AddReminderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8842942629B275B9002B16B9 /* AddReminderView.swift */; };
886FAA722A1278FF004ACF43 /* ReminderListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 886FAA712A1278FF004ACF43 /* ReminderListViewModel.swift */; };
88AE85682A12951B0040C93D /* MakeItSo.docc in Sources */ = {isa = PBXBuildFile; fileRef = 88AE85672A12951B0040C93D /* MakeItSo.docc */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
880A10AC2A1268FD006072B8 /* MakeItSo.docc */ = {isa = PBXFileReference; lastKnownFileType = folder.documentationcatalog; path = MakeItSo.docc; sourceTree = "<group>"; };
8842940F29AFEEA8002B16B9 /* MakeItSo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MakeItSo.app; sourceTree = BUILT_PRODUCTS_DIR; };
8842941229AFEEA8002B16B9 /* MakeItSoApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MakeItSoApp.swift; sourceTree = "<group>"; };
8842941429AFEEA8002B16B9 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
8842941429AFEEA8002B16B9 /* RemindersListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemindersListView.swift; sourceTree = "<group>"; };
8842941629AFEEA9002B16B9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
8842941929AFEEA9002B16B9 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
8842942429AFF792002B16B9 /* Reminder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Reminder.swift; sourceTree = "<group>"; };
8842942629B275B9002B16B9 /* AddReminderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddReminderView.swift; sourceTree = "<group>"; };
886FAA712A1278FF004ACF43 /* ReminderListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReminderListViewModel.swift; sourceTree = "<group>"; };
88AE85672A12951B0040C93D /* MakeItSo.docc */ = {isa = PBXFileReference; lastKnownFileType = folder.documentationcatalog; path = MakeItSo.docc; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -41,7 +43,7 @@
8842940629AFEEA8002B16B9 = {
isa = PBXGroup;
children = (
880A10AC2A1268FD006072B8 /* MakeItSo.docc */,
88AE85672A12951B0040C93D /* MakeItSo.docc */,
8842941129AFEEA8002B16B9 /* MakeItSo */,
8842941029AFEEA8002B16B9 /* Products */,
);
Expand All @@ -58,12 +60,10 @@
8842941129AFEEA8002B16B9 /* MakeItSo */ = {
isa = PBXGroup;
children = (
886FAA6C2A127422004ACF43 /* Features */,
8842941229AFEEA8002B16B9 /* MakeItSoApp.swift */,
8842941429AFEEA8002B16B9 /* ContentView.swift */,
8842941629AFEEA9002B16B9 /* Assets.xcassets */,
8842941829AFEEA9002B16B9 /* Preview Content */,
8842942429AFF792002B16B9 /* Reminder.swift */,
8842942629B275B9002B16B9 /* AddReminderView.swift */,
);
path = MakeItSo;
sourceTree = "<group>";
Expand All @@ -76,6 +76,49 @@
path = "Preview Content";
sourceTree = "<group>";
};
886FAA6C2A127422004ACF43 /* Features */ = {
isa = PBXGroup;
children = (
886FAA6D2A12742C004ACF43 /* Reminders */,
);
path = Features;
sourceTree = "<group>";
};
886FAA6D2A12742C004ACF43 /* Reminders */ = {
isa = PBXGroup;
children = (
886FAA6E2A12743E004ACF43 /* Models */,
886FAA6F2A127444004ACF43 /* ViewModels */,
886FAA702A12744A004ACF43 /* Views */,
);
path = Reminders;
sourceTree = "<group>";
};
886FAA6E2A12743E004ACF43 /* Models */ = {
isa = PBXGroup;
children = (
8842942429AFF792002B16B9 /* Reminder.swift */,
);
path = Models;
sourceTree = "<group>";
};
886FAA6F2A127444004ACF43 /* ViewModels */ = {
isa = PBXGroup;
children = (
886FAA712A1278FF004ACF43 /* ReminderListViewModel.swift */,
);
path = ViewModels;
sourceTree = "<group>";
};
886FAA702A12744A004ACF43 /* Views */ = {
isa = PBXGroup;
children = (
8842941429AFEEA8002B16B9 /* RemindersListView.swift */,
8842942629B275B9002B16B9 /* AddReminderView.swift */,
);
path = Views;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -147,9 +190,10 @@
buildActionMask = 2147483647;
files = (
8842942529AFF792002B16B9 /* Reminder.swift in Sources */,
8842941529AFEEA8002B16B9 /* ContentView.swift in Sources */,
88AE85682A12951B0040C93D /* MakeItSo.docc in Sources */,
8842941529AFEEA8002B16B9 /* RemindersListView.swift in Sources */,
886FAA722A1278FF004ACF43 /* ReminderListViewModel.swift in Sources */,
8842942729B275B9002B16B9 /* AddReminderView.swift in Sources */,
880A10AD2A1268FD006072B8 /* MakeItSo.docc in Sources */,
8842941329AFEEA8002B16B9 /* MakeItSoApp.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// ReminderListViewModel.swift
// MakeItSo
//
// Created by Peter Friese on 15.05.23.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation

class RemindersListViewModel: ObservableObject {
@Published
var reminders = Reminder.samples

func addReminder(_ reminder: Reminder) {
reminders.append(reminder)
}

func toggleCompleted(_ reminder: Reminder) {
if let index = reminders.firstIndex(where: { $0.id == reminder.id} ) {
reminders[index].isCompleted.toggle()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import SwiftUI

struct ContentView: View {
@State
private var reminders = Reminder.samples
struct RemindersListView: View {
@StateObject
private var viewModel = RemindersListViewModel()

@State
private var isAddReminderDialogPresented = false
Expand All @@ -30,15 +30,15 @@ struct ContentView: View {
}

var body: some View {
List($reminders) { $reminder in
List($viewModel.reminders) { $reminder in
HStack {
Image(systemName: reminder.isCompleted
? "largecircle.fill.circle"
: "circle")
.imageScale(.large)
.foregroundColor(.accentColor)
.onTapGesture {
reminder.isCompleted.toggle()
viewModel.toggleCompleted(reminder)
}
Text(reminder.title)
}
Expand All @@ -56,7 +56,7 @@ struct ContentView: View {
}
.sheet(isPresented: $isAddReminderDialogPresented) {
AddReminderView { reminder in
reminders.append(reminder)
viewModel.addReminder(reminder)
}
}
}
Expand All @@ -65,7 +65,7 @@ struct ContentView: View {
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
NavigationStack {
ContentView()
RemindersListView()
.navigationTitle("Reminders")
}
}
Expand Down
2 changes: 1 addition & 1 deletion code/frontend/MakeItSo/MakeItSo/MakeItSoApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct MakeItSoApp: App {
var body: some Scene {
WindowGroup {
NavigationStack {
ContentView()
RemindersListView()
.navigationTitle("Reminders")
}
}
Expand Down