Skip to content

Commit 68bb78f

Browse files
bjorkertcodebymini
andauthored
Open statistics from home screen stats area (#564)
* Open statistics from home screen by tapping the stats area Closes #563 Modal (home screen tap) shows Done button, menu uses navigation push with native back chevron, and tab shows only Refresh. --------- Co-authored-by: codebymini <daniel@codebymini.se>
1 parent 047be8f commit 68bb78f

4 files changed

Lines changed: 65 additions & 7 deletions

File tree

LoopFollow/Settings/SettingsMenuView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ struct AggregatedStatsViewWrapper: View {
195195
var body: some View {
196196
Group {
197197
if let mainVC = mainViewController {
198-
AggregatedStatsView(viewModel: AggregatedStatsViewModel(mainViewController: mainVC))
198+
AggregatedStatsContentView(mainViewController: mainVC)
199199
} else {
200200
Text("Loading stats...")
201201
.onAppear {

LoopFollow/Stats/AggregatedStatsView.swift

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import UIKit
77
struct AggregatedStatsView: View {
88
@ObservedObject var viewModel: AggregatedStatsViewModel
99
@Environment(\.dismiss) var dismiss
10+
var onDismiss: (() -> Void)?
1011
@State private var showGMI: Bool
1112
@State private var showStdDev: Bool
1213
@State private var startDate: Date
@@ -17,8 +18,9 @@ struct AggregatedStatsView: View {
1718
@State private var loadingTimer: Timer?
1819
@State private var timeoutTimer: Timer?
1920

20-
init(viewModel: AggregatedStatsViewModel) {
21+
init(viewModel: AggregatedStatsViewModel, onDismiss: (() -> Void)? = nil) {
2122
self.viewModel = viewModel
23+
self.onDismiss = onDismiss
2224
_showGMI = State(initialValue: Storage.shared.showGMI.value)
2325
_showStdDev = State(initialValue: Storage.shared.showStdDev.value)
2426

@@ -105,6 +107,11 @@ struct AggregatedStatsView: View {
105107
}
106108
.navigationBarTitleDisplayMode(.inline)
107109
.toolbar {
110+
if let onDismiss {
111+
ToolbarItem(placement: .navigationBarLeading) {
112+
Button("Done", action: onDismiss)
113+
}
114+
}
108115
ToolbarItem(placement: .navigationBarTrailing) {
109116
Button("Refresh") {
110117
loadingError = false
@@ -163,6 +170,38 @@ struct AggregatedStatsView: View {
163170
}
164171
}
165172

173+
struct AggregatedStatsContentView: View {
174+
@StateObject private var viewModel: AggregatedStatsViewModel
175+
private let onDismiss: (() -> Void)?
176+
177+
init(mainViewController: MainViewController?, onDismiss: (() -> Void)? = nil) {
178+
_viewModel = StateObject(wrappedValue: AggregatedStatsViewModel(mainViewController: mainViewController))
179+
self.onDismiss = onDismiss
180+
}
181+
182+
var body: some View {
183+
AggregatedStatsView(viewModel: viewModel, onDismiss: onDismiss)
184+
.preferredColorScheme(Storage.shared.appearanceMode.value.colorScheme)
185+
}
186+
}
187+
188+
struct AggregatedStatsModalView: View {
189+
@Environment(\.dismiss) private var dismiss
190+
191+
let mainViewController: MainViewController?
192+
193+
var body: some View {
194+
NavigationView {
195+
AggregatedStatsContentView(
196+
mainViewController: mainViewController,
197+
onDismiss: { dismiss() }
198+
)
199+
.navigationBarTitleDisplayMode(.inline)
200+
}
201+
.preferredColorScheme(Storage.shared.appearanceMode.value.colorScheme)
202+
}
203+
}
204+
166205
struct StatCard: View {
167206
let title: String
168207
let value: String

LoopFollow/ViewControllers/MainViewController.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
196196
updateGraphVisibility()
197197
statsView.isHidden = !Storage.shared.showStats.value
198198

199+
// Tap on stats view to open full statistics screen
200+
let statsTap = UITapGestureRecognizer(target: self, action: #selector(statsViewTapped))
201+
statsView.addGestureRecognizer(statsTap)
202+
199203
BGChart.delegate = self
200204
BGChartFull.delegate = self
201205

@@ -667,7 +671,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
667671
return treatmentsVC
668672

669673
case .stats:
670-
let statsVC = UIHostingController(rootView: AggregatedStatsView(viewModel: AggregatedStatsViewModel(mainViewController: nil)))
674+
let statsVC = UIHostingController(rootView: AggregatedStatsContentView(mainViewController: nil))
671675
let navController = UINavigationController(rootViewController: statsVC)
672676
navController.tabBarItem = UITabBarItem(title: item.displayName, image: UIImage(systemName: item.icon), tag: tag)
673677
return navController
@@ -722,6 +726,22 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
722726
return nil
723727
}
724728

729+
@objc private func statsViewTapped() {
730+
#if !targetEnvironment(macCatalyst)
731+
let position = Storage.shared.position(for: .stats).normalized
732+
if position != .menu, let tabIndex = position.tabIndex, let tbc = tabBarController {
733+
tbc.selectedIndex = tabIndex
734+
return
735+
}
736+
#endif
737+
738+
let statsModalView = AggregatedStatsModalView(mainViewController: self)
739+
let hostingController = UIHostingController(rootView: statsModalView)
740+
hostingController.overrideUserInterfaceStyle = Storage.shared.appearanceMode.value.userInterfaceStyle
741+
hostingController.modalPresentationStyle = .fullScreen
742+
present(hostingController, animated: true)
743+
}
744+
725745
private func createViewController(for item: TabItem, position: TabPosition, storyboard: UIStoryboard) -> UIViewController? {
726746
let tag = position.tabIndex ?? 0
727747

@@ -756,7 +776,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
756776
return treatmentsVC
757777

758778
case .stats:
759-
let statsVC = UIHostingController(rootView: AggregatedStatsView(viewModel: AggregatedStatsViewModel(mainViewController: self)))
779+
let statsVC = UIHostingController(rootView: AggregatedStatsContentView(mainViewController: self))
760780
let navController = UINavigationController(rootViewController: statsVC)
761781
navController.tabBarItem = UITabBarItem(title: item.displayName, image: UIImage(systemName: item.icon), tag: tag)
762782
return navController

LoopFollow/ViewControllers/MoreMenuViewController.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,8 @@ class MoreMenuViewController: UIViewController {
314314
return
315315
}
316316

317-
let statsVC = UIHostingController(
318-
rootView: AggregatedStatsView(viewModel: AggregatedStatsViewModel(mainViewController: mainVC))
319-
)
317+
let statsView = AggregatedStatsContentView(mainViewController: mainVC)
318+
let statsVC = UIHostingController(rootView: statsView)
320319
statsVC.overrideUserInterfaceStyle = Storage.shared.appearanceMode.value.userInterfaceStyle
321320
navigationController?.pushViewController(statsVC, animated: true)
322321
}

0 commit comments

Comments
 (0)