From e9c88407bacf6a1a1c8ee7ce26900762f341fdd2 Mon Sep 17 00:00:00 2001 From: Peter Hajas Date: Tue, 10 May 2022 22:09:27 -0600 Subject: [PATCH] Update views on a timer --- Kordophone/Kordophone.xcodeproj/project.pbxproj | 6 ++++++ Kordophone/Shared/ConversationList.swift | 4 ++++ Kordophone/Shared/ConversationView.swift | 4 ++++ Kordophone/Shared/IntervalUpdater.swift | 12 ++++++++++++ 4 files changed, 26 insertions(+) create mode 100644 Kordophone/Shared/IntervalUpdater.swift diff --git a/Kordophone/Kordophone.xcodeproj/project.pbxproj b/Kordophone/Kordophone.xcodeproj/project.pbxproj index 616448a..9725690 100644 --- a/Kordophone/Kordophone.xcodeproj/project.pbxproj +++ b/Kordophone/Kordophone.xcodeproj/project.pbxproj @@ -19,6 +19,8 @@ 4F48B6B1282AD977001B669B /* ConversationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F48B6AF282AD977001B669B /* ConversationView.swift */; }; 4F48B6B3282AF3A6001B669B /* MessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F48B6B2282AF3A6001B669B /* MessageView.swift */; }; 4F48B6B4282AF3A6001B669B /* MessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F48B6B2282AF3A6001B669B /* MessageView.swift */; }; + 4F48B6B6282B6DB4001B669B /* IntervalUpdater.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F48B6B5282B6DB4001B669B /* IntervalUpdater.swift */; }; + 4F48B6B7282B6DB4001B669B /* IntervalUpdater.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F48B6B5282B6DB4001B669B /* IntervalUpdater.swift */; }; 4F8A65FC280A93B0007A7431 /* Tests_iOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F8A65FB280A93B0007A7431 /* Tests_iOS.swift */; }; 4F8A65FE280A93B0007A7431 /* Tests_iOSLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F8A65FD280A93B0007A7431 /* Tests_iOSLaunchTests.swift */; }; 4F8A6608280A93B0007A7431 /* Tests_macOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F8A6607280A93B0007A7431 /* Tests_macOS.swift */; }; @@ -56,6 +58,7 @@ 4F48B6AC282A3D00001B669B /* ConversationRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationRow.swift; sourceTree = ""; }; 4F48B6AF282AD977001B669B /* ConversationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationView.swift; sourceTree = ""; }; 4F48B6B2282AF3A6001B669B /* MessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageView.swift; sourceTree = ""; }; + 4F48B6B5282B6DB4001B669B /* IntervalUpdater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntervalUpdater.swift; sourceTree = ""; }; 4F8A65E3280A93AE007A7431 /* KordophoneApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KordophoneApp.swift; sourceTree = ""; }; 4F8A65E4280A93AE007A7431 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 4F8A65E5280A93AF007A7431 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -137,6 +140,7 @@ isa = PBXGroup; children = ( 4F8A65E3280A93AE007A7431 /* KordophoneApp.swift */, + 4F48B6B5282B6DB4001B669B /* IntervalUpdater.swift */, 4F8A65E4280A93AE007A7431 /* ContentView.swift */, 4F48B6A6282A3559001B669B /* LoginView.swift */, 4F48B6A9282A365B001B669B /* ConversationList.swift */, @@ -352,6 +356,7 @@ files = ( 4F48B6AA282A365B001B669B /* ConversationList.swift in Sources */, 4F48B6A7282A3559001B669B /* LoginView.swift in Sources */, + 4F48B6B6282B6DB4001B669B /* IntervalUpdater.swift in Sources */, 4F8A660D280A93B0007A7431 /* ContentView.swift in Sources */, 4F48B6AD282A3D00001B669B /* ConversationRow.swift in Sources */, 4F8A660B280A93B0007A7431 /* KordophoneApp.swift in Sources */, @@ -366,6 +371,7 @@ files = ( 4F48B6AB282A365B001B669B /* ConversationList.swift in Sources */, 4F48B6A8282A3559001B669B /* LoginView.swift in Sources */, + 4F48B6B7282B6DB4001B669B /* IntervalUpdater.swift in Sources */, 4F8A660E280A93B0007A7431 /* ContentView.swift in Sources */, 4F48B6AE282A3D00001B669B /* ConversationRow.swift in Sources */, 4F8A660C280A93B0007A7431 /* KordophoneApp.swift in Sources */, diff --git a/Kordophone/Shared/ConversationList.swift b/Kordophone/Shared/ConversationList.swift index b4ebdb5..287d86c 100644 --- a/Kordophone/Shared/ConversationList.swift +++ b/Kordophone/Shared/ConversationList.swift @@ -32,6 +32,7 @@ struct ConversationListWrapper : View { @State private var conversations = [Conversation]() @EnvironmentObject private var connection: Connection + @StateObject private var updater = IntervalUpdater(5) private func refresh() { Task { @@ -47,5 +48,8 @@ struct ConversationListWrapper : View { .onChange(of: connection.authenticated) { newValue in refresh() } + .onChange(of: updater.seed) { newValue in + refresh() + } } } diff --git a/Kordophone/Shared/ConversationView.swift b/Kordophone/Shared/ConversationView.swift index 3d41a39..87f7cc0 100644 --- a/Kordophone/Shared/ConversationView.swift +++ b/Kordophone/Shared/ConversationView.swift @@ -59,6 +59,7 @@ struct ConversationViewWrapper : View { var conversation: Conversation @State private var messages = [Message]() @EnvironmentObject private var connection: Connection + @StateObject private var updater = IntervalUpdater(5) private func refresh() { Task { @@ -79,5 +80,8 @@ struct ConversationViewWrapper : View { .onAppear { refresh() } + .onChange(of: updater.seed) { newValue in + refresh() + } } } diff --git a/Kordophone/Shared/IntervalUpdater.swift b/Kordophone/Shared/IntervalUpdater.swift new file mode 100644 index 0000000..ded4ebc --- /dev/null +++ b/Kordophone/Shared/IntervalUpdater.swift @@ -0,0 +1,12 @@ + +import Foundation + +class IntervalUpdater : ObservableObject { + @Published var seed = 0 + init(_ interval: TimeInterval) { + Timer.scheduledTimer(withTimeInterval: interval, + repeats: true) { [weak self] _ in + self?.seed += 1 + } + } +}