From 4f2838d32914e651782aff22282bc894a69c24a9 Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Tue, 25 Jun 2024 21:15:31 +0400 Subject: [PATCH] sub status view --- .../Shared/Views/ChatList/ChatListView.swift | 4 +- .../Views/ChatList/ServersSummaryView.swift | 80 ++++++++++++++++++- 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/apps/ios/Shared/Views/ChatList/ChatListView.swift b/apps/ios/Shared/Views/ChatList/ChatListView.swift index 07d20ba70e..ecbd89de63 100644 --- a/apps/ios/Shared/Views/ChatList/ChatListView.swift +++ b/apps/ios/Shared/Views/ChatList/ChatListView.swift @@ -137,8 +137,8 @@ struct ChatListView: View { Button { showServersSummary = true } label: { - Image(systemName: "wifi") - .foregroundColor(.secondary) + // TODO backend to periodically send updates to model + SubscriptionStatusView(activeSubs: 100, pendingSubs: 0) } } diff --git a/apps/ios/Shared/Views/ChatList/ServersSummaryView.swift b/apps/ios/Shared/Views/ChatList/ServersSummaryView.swift index 247b5658b3..e100220fb5 100644 --- a/apps/ios/Shared/Views/ChatList/ServersSummaryView.swift +++ b/apps/ios/Shared/Views/ChatList/ServersSummaryView.swift @@ -183,8 +183,15 @@ struct ServersSummaryView: View { .navigationBarTitle("SMP server") .navigationBarTitleDisplayMode(.large) } label: { - Text(serverAddress(server.smpServer)) - .lineLimit(1) + HStack { + if let subs = server.subs { + SubscriptionStatusView(activeSubs: subs.ssActive, pendingSubs: subs.ssPending) + .frame(width: 16, alignment: .center) + .padding(.trailing, 4) + } + Text(serverAddress(server.smpServer)) + .lineLimit(1) + } } } @@ -249,6 +256,75 @@ struct ServersSummaryView: View { } } +struct SubscriptionStatusView: View { + @EnvironmentObject var m: ChatModel + var activeSubs: Int + var pendingSubs: Int + + var body: some View { + let netInfo = m.networkInfo + if netInfo.online { + let (image, color, variableValue, opacity) = networkOnlineImage(netInfo.networkType) + if #available(iOS 16.0, *) { + Image(systemName: image, variableValue: variableValue) + .foregroundColor(color) + } else { + Image(systemName: image) + .foregroundColor(color.opacity(opacity)) + } + } else { + Image(systemName: "wifi.slash") + .foregroundColor(.secondary) + } + } + + func networkOnlineImage(_ networkType: UserNetworkType) -> (String, Color, Double, Double) { + switch networkType { + case .cellular: + let (color, variableValue, opacity) = cellularbarsColor + return ("cellularbars", color, variableValue, opacity) + default: + let (color, variableValue, opacity) = wifiColor + return ("wifi", color, variableValue, opacity) + } + } + + var wifiColor: (Color, Double, Double) { + if activeSubs > 0 { + let wifiVariableValue = ( // wifi has 3 sections + activeSubsPercentage >= 1 ? 1 + : (activeSubsPercentage >= 0.5 && activeSubsPercentage < 1) ? 0.6 + : (activeSubsPercentage > 0 && activeSubsPercentage < 0.5) ? 0.3 + : 0 + ) + return (.accentColor, wifiVariableValue, activeSubsPercentage) + } else { + return (.secondary, 1, 1) + } + } + + var cellularbarsColor: (Color, Double, Double) { + if activeSubs > 0 { + let wifiVariableValue = ( // cellularbars has 4 sections + activeSubsPercentage >= 1 ? 1 + : (activeSubsPercentage >= 0.67 && activeSubsPercentage < 1) ? 0.7 + : (activeSubsPercentage >= 0.33 && activeSubsPercentage < 0.67) ? 0.45 + : (activeSubsPercentage > 0 && activeSubsPercentage < 0.33) ? 0.2 + : 0 + ) + return (.accentColor, wifiVariableValue, activeSubsPercentage) + } else { + return (.secondary, 1, 1) + } + } + + var activeSubsPercentage: Double { + let total = activeSubs + pendingSubs + guard total != 0 else { return 0.0 } + return Double(activeSubs) / Double(total) + } +} + struct SMPServerSummaryView: View { var summary: SMPServerSummary var showReconnectButton: Bool