Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LoopFollow/Settings/SettingsMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ struct AggregatedStatsViewWrapper: View {
var body: some View {
Group {
if let mainVC = mainViewController {
AggregatedStatsView(viewModel: AggregatedStatsViewModel(mainViewController: mainVC))
AggregatedStatsContentView(mainViewController: mainVC)
} else {
Text("Loading stats...")
.onAppear {
Expand Down
41 changes: 40 additions & 1 deletion LoopFollow/Stats/AggregatedStatsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import UIKit
struct AggregatedStatsView: View {
@ObservedObject var viewModel: AggregatedStatsViewModel
@Environment(\.dismiss) var dismiss
var onDismiss: (() -> Void)?
@State private var showGMI: Bool
@State private var showStdDev: Bool
@State private var startDate: Date
Expand All @@ -17,8 +18,9 @@ struct AggregatedStatsView: View {
@State private var loadingTimer: Timer?
@State private var timeoutTimer: Timer?

init(viewModel: AggregatedStatsViewModel) {
init(viewModel: AggregatedStatsViewModel, onDismiss: (() -> Void)? = nil) {
self.viewModel = viewModel
self.onDismiss = onDismiss
_showGMI = State(initialValue: Storage.shared.showGMI.value)
_showStdDev = State(initialValue: Storage.shared.showStdDev.value)

Expand Down Expand Up @@ -105,6 +107,11 @@ struct AggregatedStatsView: View {
}
.navigationBarTitleDisplayMode(.inline)
.toolbar {
if let onDismiss {
ToolbarItem(placement: .navigationBarLeading) {
Button("Done", action: onDismiss)
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button("Refresh") {
loadingError = false
Expand Down Expand Up @@ -163,6 +170,38 @@ struct AggregatedStatsView: View {
}
}

struct AggregatedStatsContentView: View {
@StateObject private var viewModel: AggregatedStatsViewModel
private let onDismiss: (() -> Void)?

init(mainViewController: MainViewController?, onDismiss: (() -> Void)? = nil) {
_viewModel = StateObject(wrappedValue: AggregatedStatsViewModel(mainViewController: mainViewController))
self.onDismiss = onDismiss
}

var body: some View {
AggregatedStatsView(viewModel: viewModel, onDismiss: onDismiss)
.preferredColorScheme(Storage.shared.appearanceMode.value.colorScheme)
}
}

struct AggregatedStatsModalView: View {
@Environment(\.dismiss) private var dismiss

let mainViewController: MainViewController?

var body: some View {
NavigationView {
AggregatedStatsContentView(
mainViewController: mainViewController,
onDismiss: { dismiss() }
)
.navigationBarTitleDisplayMode(.inline)
}
.preferredColorScheme(Storage.shared.appearanceMode.value.colorScheme)
}
}

struct StatCard: View {
let title: String
let value: String
Expand Down
24 changes: 22 additions & 2 deletions LoopFollow/ViewControllers/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
updateGraphVisibility()
statsView.isHidden = !Storage.shared.showStats.value

// Tap on stats view to open full statistics screen
let statsTap = UITapGestureRecognizer(target: self, action: #selector(statsViewTapped))
statsView.addGestureRecognizer(statsTap)

BGChart.delegate = self
BGChartFull.delegate = self

Expand Down Expand Up @@ -667,7 +671,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
return treatmentsVC

case .stats:
let statsVC = UIHostingController(rootView: AggregatedStatsView(viewModel: AggregatedStatsViewModel(mainViewController: nil)))
let statsVC = UIHostingController(rootView: AggregatedStatsContentView(mainViewController: nil))
let navController = UINavigationController(rootViewController: statsVC)
navController.tabBarItem = UITabBarItem(title: item.displayName, image: UIImage(systemName: item.icon), tag: tag)
return navController
Expand Down Expand Up @@ -722,6 +726,22 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
return nil
}

@objc private func statsViewTapped() {
#if !targetEnvironment(macCatalyst)
let position = Storage.shared.position(for: .stats).normalized
if position != .menu, let tabIndex = position.tabIndex, let tbc = tabBarController {
tbc.selectedIndex = tabIndex
return
}
#endif

let statsModalView = AggregatedStatsModalView(mainViewController: self)
let hostingController = UIHostingController(rootView: statsModalView)
hostingController.overrideUserInterfaceStyle = Storage.shared.appearanceMode.value.userInterfaceStyle
hostingController.modalPresentationStyle = .fullScreen
present(hostingController, animated: true)
}

private func createViewController(for item: TabItem, position: TabPosition, storyboard: UIStoryboard) -> UIViewController? {
let tag = position.tabIndex ?? 0

Expand Down Expand Up @@ -756,7 +776,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
return treatmentsVC

case .stats:
let statsVC = UIHostingController(rootView: AggregatedStatsView(viewModel: AggregatedStatsViewModel(mainViewController: self)))
let statsVC = UIHostingController(rootView: AggregatedStatsContentView(mainViewController: self))
let navController = UINavigationController(rootViewController: statsVC)
navController.tabBarItem = UITabBarItem(title: item.displayName, image: UIImage(systemName: item.icon), tag: tag)
return navController
Expand Down
5 changes: 2 additions & 3 deletions LoopFollow/ViewControllers/MoreMenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,8 @@ class MoreMenuViewController: UIViewController {
return
}

let statsVC = UIHostingController(
rootView: AggregatedStatsView(viewModel: AggregatedStatsViewModel(mainViewController: mainVC))
)
let statsView = AggregatedStatsContentView(mainViewController: mainVC)
let statsVC = UIHostingController(rootView: statsView)
statsVC.overrideUserInterfaceStyle = Storage.shared.appearanceMode.value.userInterfaceStyle
navigationController?.pushViewController(statsVC, animated: true)
}
Expand Down