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
✨ Add DocC files for chapter 1 / Implementing Model, View, ViewModel …
…(MVVM)

Signed-off-by: Peter Friese <[email protected]>
  • Loading branch information
peterfriese committed May 15, 2023
commit c15001f5541579fe725f71baf429fa58ff66eca1
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ...implementing-mvvm/artwork/[email protected]
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import SwiftUI

struct RemindersListView: View {
@State
private var reminders = Reminder.samples

@State
private var isAddReminderDialogPresented = false

private func presentAddReminderView() {
isAddReminderDialogPresented.toggle()
}

var body: some View {
List($reminders) { $reminder in
HStack {
Image(systemName: reminder.isCompleted
? "largecircle.fill.circle"
: "circle")
.imageScale(.large)
.foregroundColor(.accentColor)
.onTapGesture {
reminder.isCompleted.toggle()
}
Text(reminder.title)
}
}
.toolbar {
ToolbarItemGroup(placement: .bottomBar) {
Button(action: presentAddReminderView) {
HStack {
Image(systemName: "plus.circle.fill")
Text("New Reminder")
}
}
Spacer()
}
}
.sheet(isPresented: $isAddReminderDialogPresented) {
AddReminderView { reminder in
reminders.append(reminder)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import SwiftUI

struct RemindersListView: View {
@State
private var reminders = Reminder.samples

@StateObject
private var viewModel = RemindersListViewModel()

@State
private var isAddReminderDialogPresented = false

private func presentAddReminderView() {
isAddReminderDialogPresented.toggle()
}

var body: some View {
List($reminders) { $reminder in
HStack {
Image(systemName: reminder.isCompleted
? "largecircle.fill.circle"
: "circle")
.imageScale(.large)
.foregroundColor(.accentColor)
.onTapGesture {
reminder.isCompleted.toggle()
}
Text(reminder.title)
}
}
.toolbar {
ToolbarItemGroup(placement: .bottomBar) {
Button(action: presentAddReminderView) {
HStack {
Image(systemName: "plus.circle.fill")
Text("New Reminder")
}
}
Spacer()
}
}
.sheet(isPresented: $isAddReminderDialogPresented) {
AddReminderView { reminder in
reminders.append(reminder)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import SwiftUI

struct RemindersListView: View {
// @State
// private var reminders = Reminder.samples

@StateObject
private var viewModel = RemindersListViewModel()

@State
private var isAddReminderDialogPresented = false

private func presentAddReminderView() {
isAddReminderDialogPresented.toggle()
}

var body: some View {
List($viewModel.reminders) { $reminder in
HStack {
Image(systemName: reminder.isCompleted
? "largecircle.fill.circle"
: "circle")
.imageScale(.large)
.foregroundColor(.accentColor)
.onTapGesture {
reminder.isCompleted.toggle()
}
Text(reminder.title)
}
}
.toolbar {
ToolbarItemGroup(placement: .bottomBar) {
Button(action: presentAddReminderView) {
HStack {
Image(systemName: "plus.circle.fill")
Text("New Reminder")
}
}
Spacer()
}
}
.sheet(isPresented: $isAddReminderDialogPresented) {
AddReminderView { reminder in
viewModel.reminders.append(reminder)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Foundation

class RemindersListViewModel: ObservableObject {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

class RemindersListViewModel: ObservableObject {
var reminders = Reminder.samples
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation

class RemindersListViewModel: ObservableObject {
@Published
var reminders = Reminder.samples
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import SwiftUI

struct RemindersListView: View {
@StateObject
private var viewModel = RemindersListViewModel()

@State
private var isAddReminderDialogPresented = false

private func presentAddReminderView() {
isAddReminderDialogPresented.toggle()
}

var body: some View {
List($viewModel.reminders) { $reminder in
HStack {
Image(systemName: reminder.isCompleted
? "largecircle.fill.circle"
: "circle")
.imageScale(.large)
.foregroundColor(.accentColor)
.onTapGesture {
reminder.isCompleted.toggle()
}
Text(reminder.title)
}
}
.toolbar {
ToolbarItemGroup(placement: .bottomBar) {
Button(action: presentAddReminderView) {
HStack {
Image(systemName: "plus.circle.fill")
Text("New Reminder")
}
}
Spacer()
}
}
.sheet(isPresented: $isAddReminderDialogPresented) {
AddReminderView { reminder in
viewModel.reminders.append(reminder)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import SwiftUI

struct RemindersListView: View {
@StateObject
private var viewModel = RemindersListViewModel()

@State
private var isAddReminderDialogPresented = false

private func presentAddReminderView() {
isAddReminderDialogPresented.toggle()
}

var body: some View {
List($viewModel.reminders) { $reminder in
HStack {
Image(systemName: reminder.isCompleted
? "largecircle.fill.circle"
: "circle")
.imageScale(.large)
.foregroundColor(.accentColor)
.onTapGesture {
reminder.isCompleted.toggle()
}
Text(reminder.title)
}
}
.toolbar {
ToolbarItemGroup(placement: .bottomBar) {
Button(action: presentAddReminderView) {
HStack {
Image(systemName: "plus.circle.fill")
Text("New Reminder")
}
}
Spacer()
}
}
.sheet(isPresented: $isAddReminderDialogPresented) {
AddReminderView { reminder in
viewModel.addReminder(reminder)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Foundation

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

func addReminder(_ reminder: Reminder) {
reminders.append(reminder)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import SwiftUI

struct RemindersListView: View {
@StateObject
private var viewModel = RemindersListViewModel()

@State
private var isAddReminderDialogPresented = false

private func presentAddReminderView() {
isAddReminderDialogPresented.toggle()
}

var body: some View {
List($viewModel.reminders) { $reminder in
HStack {
Image(systemName: reminder.isCompleted
? "largecircle.fill.circle"
: "circle")
.imageScale(.large)
.foregroundColor(.accentColor)
.onTapGesture {
reminder.isCompleted.toggle()
}
Text(reminder.title)
}
}
.toolbar {
ToolbarItemGroup(placement: .bottomBar) {
Button(action: presentAddReminderView) {
HStack {
Image(systemName: "plus.circle.fill")
Text("New Reminder")
}
}
Spacer()
}
}
.sheet(isPresented: $isAddReminderDialogPresented) {
AddReminderView { reminder in
viewModel.addReminder(reminder)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import SwiftUI

struct RemindersListView: View {
@StateObject
private var viewModel = RemindersListViewModel()

@State
private var isAddReminderDialogPresented = false

private func presentAddReminderView() {
isAddReminderDialogPresented.toggle()
}

var body: some View {
List($viewModel.reminders) { $reminder in
HStack {
Image(systemName: reminder.isCompleted
? "largecircle.fill.circle"
: "circle")
.imageScale(.large)
.foregroundColor(.accentColor)
.onTapGesture {
viewModel.toggleCompleted(reminder)
}
Text(reminder.title)
}
}
.toolbar {
ToolbarItemGroup(placement: .bottomBar) {
Button(action: presentAddReminderView) {
HStack {
Image(systemName: "plus.circle.fill")
Text("New Reminder")
}
}
Spacer()
}
}
.sheet(isPresented: $isAddReminderDialogPresented) {
AddReminderView { reminder in
viewModel.addReminder(reminder)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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()
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

In this chapter, you will learn how to use SwiftUI to create a simple UI for a todo list application.
@TutorialReference(tutorial: "doc:01-Building-a-Simple-Todo-List-UI")
@TutorialReference(tutorial: "doc:02-Implementing-MVVM")
}
}
}