Skip to content

Commit 6ba6e39

Browse files
Merge pull request #192 from writefreely/ios15-beta-testing
Fix three-column navigation and various iOS 15 glitches
2 parents badf80e + ff7e7c3 commit 6ba6e39

File tree

7 files changed

+110
-98
lines changed

7 files changed

+110
-98
lines changed

Shared/Navigation/ContentView.swift

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct ContentView: View {
3535
.help("Create a new local draft.")
3636
}
3737
#else
38-
CollectionListView()
38+
CollectionListView(selectedCollection: model.selectedCollection)
3939
#endif
4040

4141
#if os(macOS)
@@ -49,36 +49,13 @@ struct ContentView: View {
4949
}
5050
}
5151
#else
52-
PostListView()
52+
PostListView(selectedCollection: model.selectedCollection, showAllPosts: model.showAllPosts)
5353
#endif
5454

5555
Text("Select a post, or create a new local draft.")
5656
.foregroundColor(.secondary)
5757
}
5858
.environmentObject(model)
59-
60-
#if os(iOS)
61-
EmptyView()
62-
.sheet(
63-
isPresented: $model.isPresentingSettingsView,
64-
onDismiss: { model.isPresentingSettingsView = false },
65-
content: {
66-
SettingsView()
67-
.environmentObject(model)
68-
}
69-
)
70-
.alert(isPresented: $model.isPresentingNetworkErrorAlert, content: {
71-
Alert(
72-
title: Text("Connection Error"),
73-
message: Text("""
74-
There is no internet connection at the moment. Please reconnect or try again later.
75-
"""),
76-
dismissButton: .default(Text("OK"), action: {
77-
model.isPresentingNetworkErrorAlert = false
78-
})
79-
)
80-
})
81-
#endif
8259
}
8360
}
8461

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import SwiftUI
2+
import CoreData
3+
4+
class CollectionListModel: NSObject, ObservableObject {
5+
@Published var list: [WFACollection] = []
6+
private let collectionsController: NSFetchedResultsController<WFACollection>
7+
8+
init(managedObjectContext: NSManagedObjectContext) {
9+
collectionsController = NSFetchedResultsController(fetchRequest: WFACollection.collectionsFetchRequest,
10+
managedObjectContext: managedObjectContext,
11+
sectionNameKeyPath: nil,
12+
cacheName: nil)
13+
14+
super.init()
15+
16+
collectionsController.delegate = self
17+
18+
do {
19+
try collectionsController.performFetch()
20+
list = collectionsController.fetchedObjects ?? []
21+
} catch {
22+
print("Failed to fetch collections!")
23+
}
24+
}
25+
}
26+
27+
extension CollectionListModel: NSFetchedResultsControllerDelegate {
28+
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
29+
guard let collections = controller.fetchedObjects as? [WFACollection] else { return }
30+
self.list = collections
31+
}
32+
}
33+
34+
extension WFACollection {
35+
static var collectionsFetchRequest: NSFetchRequest<WFACollection> {
36+
let request: NSFetchRequest<WFACollection> = WFACollection.createFetchRequest()
37+
request.sortDescriptors = [NSSortDescriptor(keyPath: \WFACollection.title, ascending: true)]
38+
return request
39+
}
40+
}

Shared/PostCollection/CollectionListView.swift

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,24 @@ import SwiftUI
22

33
struct CollectionListView: View {
44
@EnvironmentObject var model: WriteFreelyModel
5-
6-
@FetchRequest(
7-
entity: WFACollection.entity(),
8-
sortDescriptors: [NSSortDescriptor(keyPath: \WFACollection.title, ascending: true)]
9-
) var collections: FetchedResults<WFACollection>
5+
@ObservedObject var collections = CollectionListModel(managedObjectContext: LocalStorageManager.persistentContainer.viewContext)
6+
@State var selectedCollection: WFACollection?
107

118
var body: some View {
12-
List(selection: $model.selectedCollection) {
9+
List(selection: $selectedCollection) {
1310
if model.account.isLoggedIn {
14-
NavigationLink(
15-
destination: PostListView(),
16-
isActive: Binding<Bool>(
17-
get: { () -> Bool in
18-
model.selectedCollection == nil && model.showAllPosts
19-
}, set: { newValue in
20-
if newValue {
21-
self.model.showAllPosts = true
22-
self.model.selectedCollection = nil
23-
} else {
24-
// No-op
25-
}
26-
}
27-
),
28-
label: {
29-
Text("All Posts")
30-
})
31-
NavigationLink(
32-
destination: PostListView(),
33-
isActive: Binding<Bool>(
34-
get: { () -> Bool in
35-
model.selectedCollection == nil && !model.showAllPosts
36-
}, set: { newValue in
37-
if newValue {
38-
self.model.showAllPosts = false
39-
self.model.selectedCollection = nil
40-
} else {
41-
// No-op
42-
}
43-
}
44-
),
45-
label: {
46-
Text(model.account.server == "https://write.as" ? "Anonymous" : "Drafts")
47-
})
11+
NavigationLink("All Posts", destination: PostListView(selectedCollection: nil, showAllPosts: true))
12+
NavigationLink("Drafts", destination: PostListView(selectedCollection: nil, showAllPosts: false))
4813
Section(header: Text("Your Blogs")) {
49-
ForEach(collections, id: \.self) { collection in
50-
NavigationLink(
51-
destination: PostListView(),
52-
isActive: Binding<Bool>(
53-
get: { () -> Bool in
54-
model.selectedCollection == collection && !model.showAllPosts
55-
}, set: { newValue in
56-
if newValue {
57-
self.model.showAllPosts = false
58-
self.model.selectedCollection = collection
59-
} else {
60-
// No-op
61-
}
62-
}
63-
),
64-
label: { Text(collection.title) }
65-
)
14+
ForEach(collections.list, id: \.self) { collection in
15+
NavigationLink(destination: PostListView(selectedCollection: collection, showAllPosts: false),
16+
tag: collection,
17+
selection: $selectedCollection,
18+
label: { Text("\(collection.title)") })
6619
}
6720
}
6821
} else {
69-
NavigationLink(destination: PostListView()) {
22+
NavigationLink(destination: PostListView(selectedCollection: nil, showAllPosts: false)) {
7023
Text("Drafts")
7124
}
7225
}

Shared/PostList/PostListFilteredView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ struct PostListFilteredView: View {
9797
Alert(
9898
title: Text("Delete Post?"),
9999
message: Text("This action cannot be undone."),
100-
primaryButton: .cancel() {
100+
primaryButton: .cancel {
101101
model.postToDelete = nil
102102
},
103103
secondaryButton: .destructive(Text("Delete"), action: {

Shared/PostList/PostListView.swift

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ struct PostListView: View {
77

88
@State private var postCount: Int = 0
99

10+
var selectedCollection: WFACollection?
11+
var showAllPosts: Bool
12+
1013
#if os(iOS)
1114
private var frameHeight: CGFloat {
1215
var height: CGFloat = 50
@@ -20,12 +23,12 @@ struct PostListView: View {
2023
#if os(iOS)
2124
ZStack(alignment: .bottom) {
2225
PostListFilteredView(
23-
collection: model.selectedCollection,
24-
showAllPosts: model.showAllPosts,
26+
collection: selectedCollection,
27+
showAllPosts: showAllPosts,
2528
postCount: $postCount
2629
)
2730
.navigationTitle(
28-
model.showAllPosts ? "All Posts" : model.selectedCollection?.title ?? (
31+
showAllPosts ? "All Posts" : selectedCollection?.title ?? (
2932
model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
3033
)
3134
)
@@ -70,9 +73,28 @@ struct PostListView: View {
7073
})
7174
.accessibilityLabel(Text("Settings"))
7275
.accessibilityHint(Text("Open the Settings sheet"))
76+
.sheet(
77+
isPresented: $model.isPresentingSettingsView,
78+
onDismiss: { model.isPresentingSettingsView = false },
79+
content: {
80+
SettingsView()
81+
.environmentObject(model)
82+
}
83+
)
7384
Spacer()
7485
Text(postCount == 1 ? "\(postCount) post" : "\(postCount) posts")
7586
.foregroundColor(.secondary)
87+
.alert(isPresented: $model.isPresentingNetworkErrorAlert, content: {
88+
Alert(
89+
title: Text("Connection Error"),
90+
message: Text("""
91+
There is no internet connection at the moment. Please reconnect or try again later.
92+
"""),
93+
dismissButton: .default(Text("OK"), action: {
94+
model.isPresentingNetworkErrorAlert = false
95+
})
96+
)
97+
})
7698
Spacer()
7799
if model.isProcessingRequest {
78100
ProgressView()
@@ -103,10 +125,14 @@ struct PostListView: View {
103125
.overlay(Divider(), alignment: .top)
104126
}
105127
.ignoresSafeArea()
128+
.onAppear {
129+
model.selectedCollection = selectedCollection
130+
model.showAllPosts = showAllPosts
131+
}
106132
#else
107133
PostListFilteredView(
108-
collection: model.selectedCollection,
109-
showAllPosts: model.showAllPosts,
134+
collection: selectedCollection,
135+
showAllPosts: showAllPosts,
110136
postCount: $postCount
111137
)
112138
.toolbar {
@@ -129,7 +155,7 @@ struct PostListView: View {
129155
}
130156
}
131157
.navigationTitle(
132-
model.showAllPosts ? "All Posts" : model.selectedCollection?.title ?? (
158+
showAllPosts ? "All Posts" : selectedCollection?.title ?? (
133159
model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
134160
)
135161
)
@@ -142,7 +168,7 @@ struct PostListView_Previews: PreviewProvider {
142168
let context = LocalStorageManager.persistentContainer.viewContext
143169
let model = WriteFreelyModel()
144170

145-
return PostListView()
171+
return PostListView(showAllPosts: true)
146172
.environment(\.managedObjectContext, context)
147173
.environmentObject(model)
148174
}

Shared/WriteFreely_MultiPlatformApp.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,22 @@ struct WriteFreely_MultiPlatformApp: App {
3838
DispatchQueue.main.async {
3939
self.model.selectedCollection = nil
4040
self.model.showAllPosts = true
41+
showLastDraftOrCreateNewLocalPost()
4142
}
4243
} else {
4344
DispatchQueue.main.async {
4445
self.model.selectedCollection = model.editor.fetchSelectedCollectionFromAppStorage()
4546
self.model.showAllPosts = false
47+
showLastDraftOrCreateNewLocalPost()
4648
}
4749
}
48-
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
49-
if model.editor.lastDraftURL != nil {
50-
self.model.selectedPost = model.editor.fetchLastDraftFromAppStorage()
51-
} else {
52-
createNewLocalPost()
53-
}
54-
}
50+
// DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
51+
// if model.editor.lastDraftURL != nil {
52+
// self.model.selectedPost = model.editor.fetchLastDraftFromAppStorage()
53+
// } else {
54+
// createNewLocalPost()
55+
// }
56+
// }
5557
})
5658
.environmentObject(model)
5759
.environment(\.managedObjectContext, LocalStorageManager.persistentContainer.viewContext)
@@ -128,6 +130,14 @@ struct WriteFreely_MultiPlatformApp: App {
128130
#endif
129131
}
130132

133+
private func showLastDraftOrCreateNewLocalPost() {
134+
if model.editor.lastDraftURL != nil {
135+
self.model.selectedPost = model.editor.fetchLastDraftFromAppStorage()
136+
} else {
137+
createNewLocalPost()
138+
}
139+
}
140+
131141
private func createNewLocalPost() {
132142
withAnimation {
133143
// Un-set the currently selected post

WriteFreely-MultiPlatform.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
170A7EC126F5186A00F1CBD4 /* CollectionListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170A7EC026F5186A00F1CBD4 /* CollectionListModel.swift */; };
11+
170A7EC226F5186A00F1CBD4 /* CollectionListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170A7EC026F5186A00F1CBD4 /* CollectionListModel.swift */; };
1012
170DFA34251BBC44001D82A0 /* PostEditorModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170DFA33251BBC44001D82A0 /* PostEditorModel.swift */; };
1113
170DFA35251BBC44001D82A0 /* PostEditorModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170DFA33251BBC44001D82A0 /* PostEditorModel.swift */; };
1214
17120DA124E19839002B9F6C /* AccountView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17A5388D24DDEC7400DEFF9A /* AccountView.swift */; };
@@ -126,6 +128,7 @@
126128

127129
/* Begin PBXFileReference section */
128130
1709ADDF251B9A110053AF79 /* EditorLaunchingPolicy.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = EditorLaunchingPolicy.md; sourceTree = "<group>"; };
131+
170A7EC026F5186A00F1CBD4 /* CollectionListModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionListModel.swift; sourceTree = "<group>"; };
129132
170DFA33251BBC44001D82A0 /* PostEditorModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostEditorModel.swift; sourceTree = "<group>"; };
130133
17120DA424E19CBF002B9F6C /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
131134
17120DA824E1B2F5002B9F6C /* AccountLogoutView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountLogoutView.swift; sourceTree = "<group>"; };
@@ -496,6 +499,7 @@
496499
17DF32D224C8B78D00BCE2E3 /* PostCollection */ = {
497500
isa = PBXGroup;
498501
children = (
502+
170A7EC026F5186A00F1CBD4 /* CollectionListModel.swift */,
499503
171BFDF924D4AF8300888236 /* CollectionListView.swift */,
500504
);
501505
path = PostCollection;
@@ -755,6 +759,7 @@
755759
17B996DA2502D23E0017B536 /* WFAPost+CoreDataProperties.swift in Sources */,
756760
1756AE7724CB2EDD00FD7257 /* PostEditorView.swift in Sources */,
757761
17DF32D524C8CA3400BCE2E3 /* PostStatusBadgeView.swift in Sources */,
762+
170A7EC126F5186A00F1CBD4 /* CollectionListModel.swift in Sources */,
758763
17D435E824E3128F0036B539 /* PreferencesModel.swift in Sources */,
759764
1756AE7A24CB65DF00FD7257 /* PostListView.swift in Sources */,
760765
17B996D82502D23E0017B536 /* WFAPost+CoreDataClass.swift in Sources */,
@@ -791,6 +796,7 @@
791796
17C42E662509237800072984 /* PostListFilteredView.swift in Sources */,
792797
17120DAD24E1B99F002B9F6C /* AccountLoginView.swift in Sources */,
793798
17466626256C0D0600629997 /* MacEditorTextView.swift in Sources */,
799+
170A7EC226F5186A00F1CBD4 /* CollectionListModel.swift in Sources */,
794800
17E5DF8A2543610700DCDC9B /* PostTextEditingView.swift in Sources */,
795801
17C42E71250AAFD500072984 /* NSManagedObjectContext+ExecuteAndMergeChanges.swift in Sources */,
796802
1756AE7B24CB65DF00FD7257 /* PostListView.swift in Sources */,

0 commit comments

Comments
 (0)