Skip to content

Commit e0f41ce

Browse files
author
Mithun
committed
Combine and Edit Data
1 parent 96e28ce commit e0f41ce

File tree

3 files changed

+69
-26
lines changed

3 files changed

+69
-26
lines changed

DesignCode.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
ABB215DC22DB353800424B38 /* BlurView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABB215DB22DB353800424B38 /* BlurView.swift */; };
1313
ABB215DE22DB713300424B38 /* UpdateList.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABB215DD22DB713300424B38 /* UpdateList.swift */; };
1414
ABB215E022DB7F4700424B38 /* UpdateDetail.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABB215DF22DB7F4700424B38 /* UpdateDetail.swift */; };
15+
ABB215E222DC472900424B38 /* UpdateStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABB215E122DC472900424B38 /* UpdateStore.swift */; };
1516
ABE07CBF22D74F5700D209BE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABE07CBE22D74F5700D209BE /* AppDelegate.swift */; };
1617
ABE07CC122D74F5700D209BE /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABE07CC022D74F5700D209BE /* SceneDelegate.swift */; };
1718
ABE07CC322D74F5700D209BE /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABE07CC222D74F5700D209BE /* ContentView.swift */; };
@@ -26,6 +27,7 @@
2627
ABB215DB22DB353800424B38 /* BlurView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlurView.swift; sourceTree = "<group>"; };
2728
ABB215DD22DB713300424B38 /* UpdateList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateList.swift; sourceTree = "<group>"; };
2829
ABB215DF22DB7F4700424B38 /* UpdateDetail.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateDetail.swift; sourceTree = "<group>"; };
30+
ABB215E122DC472900424B38 /* UpdateStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateStore.swift; sourceTree = "<group>"; };
2931
ABE07CBB22D74F5700D209BE /* DesignCode.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DesignCode.app; sourceTree = BUILT_PRODUCTS_DIR; };
3032
ABE07CBE22D74F5700D209BE /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
3133
ABE07CC022D74F5700D209BE /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
@@ -72,6 +74,7 @@
7274
AB62172622D870540039BC73 /* Home.swift */,
7375
ABB215DD22DB713300424B38 /* UpdateList.swift */,
7476
ABB215DF22DB7F4700424B38 /* UpdateDetail.swift */,
77+
ABB215E122DC472900424B38 /* UpdateStore.swift */,
7578
AB1D3B1B22D9CDEF00D0AEA6 /* HomeList.swift */,
7679
ABB215DB22DB353800424B38 /* BlurView.swift */,
7780
ABE07CC422D74F5900D209BE /* Assets.xcassets */,
@@ -162,6 +165,7 @@
162165
buildActionMask = 2147483647;
163166
files = (
164167
ABE07CBF22D74F5700D209BE /* AppDelegate.swift in Sources */,
168+
ABB215E222DC472900424B38 /* UpdateStore.swift in Sources */,
165169
ABB215DE22DB713300424B38 /* UpdateList.swift in Sources */,
166170
AB1D3B1C22D9CDEF00D0AEA6 /* HomeList.swift in Sources */,
167171
ABB215E022DB7F4700424B38 /* UpdateDetail.swift in Sources */,

DesignCode/UpdateList.swift

+41-26
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,58 @@ import SwiftUI
1111
struct UpdateList: View {
1212

1313
var updates = updateData
14+
@ObjectBinding var store = UpdateStore(updates: updateData)
15+
16+
func addUpdate() {
17+
store.updates.append(Update(image: "Certificate1", title: "New Title", text: "New Text", date: "JUL 1"))
18+
}
19+
20+
func move(from source: IndexSet, to destination: Int) {
21+
store.updates.swapAt(source.first!, destination)
22+
}
1423

1524
var body: some View {
1625
NavigationView {
17-
List(updates) { item in
18-
NavigationLink(destination: UpdateDetail(title: item.title, text: item.text, image: item.image)) {
19-
HStack(spacing: 12.0) {
20-
Image(item.image)
21-
.resizable()
22-
.aspectRatio(contentMode: .fit)
23-
.frame(width: 80, height: 80)
24-
.background(Color("background"))
25-
.cornerRadius(20)
26+
List {
27+
ForEach(store.updates) { item in
28+
NavigationLink(destination: UpdateDetail(title: item.title, text: item.text, image: item.image)) {
29+
HStack(spacing: 12.0) {
30+
Image(item.image)
31+
.resizable()
32+
.aspectRatio(contentMode: .fit)
33+
.frame(width: 80, height: 80)
34+
.background(Color("background"))
35+
.cornerRadius(20)
2636

27-
VStack(alignment: .leading) {
28-
Text(item.title)
29-
.font(.headline)
37+
VStack(alignment: .leading) {
38+
Text(item.title)
39+
.font(.headline)
3040

31-
Text(item.text)
32-
.lineLimit(2)
33-
.lineSpacing(4)
34-
.frame(height: 50.0)
35-
.font(.subheadline)
41+
Text(item.text)
42+
.lineLimit(2)
43+
.lineSpacing(4)
44+
.font(.subheadline)
45+
.frame(height: 50.0)
3646

37-
Text(item.date)
38-
.font(.caption)
39-
.fontWeight(.bold)
40-
.color(.gray)
47+
Text(item.date)
48+
.font(.caption)
49+
.fontWeight(.bold)
50+
.color(.gray)
51+
}
4152
}
4253
}
54+
.padding(.vertical, 8.0)
55+
}
56+
.onDelete { index in
57+
self.store.updates.remove(at: index.first!)
4358
}
44-
.padding(.vertical, 8.0)
59+
.onMove(perform: move)
4560
}
4661
.navigationBarTitle(Text("Updates"))
47-
.navigationBarItems(trailing:
48-
PresentationLink(destination: Text("Settings")) {
49-
Image(systemName: "gear")
50-
})
62+
.navigationBarItems(
63+
leading: Button(action: addUpdate) { Text("Add Update") },
64+
trailing: EditButton()
65+
)
5166
}
5267
}
5368
}

DesignCode/UpdateStore.swift

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// UpdateStore.swift
3+
// DesignCode
4+
//
5+
// Created by Mithun x on 7/15/19.
6+
// Copyright © 2019 Mithun. All rights reserved.
7+
//
8+
9+
import Combine
10+
import SwiftUI
11+
12+
class UpdateStore: BindableObject {
13+
var didChange = PassthroughSubject<Void, Never>()
14+
15+
var updates: [Update] {
16+
didSet {
17+
didChange.send()
18+
}
19+
}
20+
21+
init(updates: [Update] = []) {
22+
self.updates = updates
23+
}
24+
}

0 commit comments

Comments
 (0)