-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PIA-1273: Add top navigation bar on the user authenticated flow
- Loading branch information
1 parent
0a2d002
commit 0c9fc1a
Showing
17 changed files
with
565 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
PIA VPN-tvOS/Navigation/CompositionRoot/TopNavigationFactory.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// TopNavigationFactory.swift | ||
// PIA VPN-tvOS | ||
// | ||
// Created by Laura S on 2/6/24. | ||
// Copyright © 2024 Private Internet Access Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
class TopNavigationFactory { | ||
|
||
static func makeLeadingSegmentedNavigationView() -> LeadingSegmentedNavigationView { | ||
return LeadingSegmentedNavigationView(viewModel: makeTopNavigationViewModel()) | ||
} | ||
|
||
static func makeTrailingNavigationView() -> TrailingNavigationView { | ||
return TrailingNavigationView(viewModel: makeTopNavigationViewModel()) | ||
} | ||
|
||
// TODO: Consider making the VM a single instance (for both leading and trailing views) | ||
static func makeTopNavigationViewModel() -> TopNavigationViewModel { | ||
return TopNavigationViewModel(appRouter: AppRouterFactory.makeAppRouter()) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
PIA VPN-tvOS/Navigation/Presentation/TopNavigationViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// | ||
// LeadingSegmentedNavigationViewModel.swift | ||
// PIA VPN-tvOS | ||
// | ||
// Created by Laura S on 2/6/24. | ||
// Copyright © 2024 Private Internet Access Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import SwiftUI | ||
|
||
class TopNavigationViewModel: ObservableObject { | ||
|
||
let appRouter: AppRouter | ||
|
||
private var cancellables = Set<AnyCancellable>() | ||
|
||
let leadingSections: [Sections] = [.vpn, .locations] | ||
let trailingSections: [Sections] = [.settings, .help] | ||
|
||
enum Sections: Equatable, Hashable, Identifiable { | ||
var id: Self { | ||
return self | ||
} | ||
|
||
case vpn | ||
case locations | ||
case settings | ||
case help | ||
|
||
var title: String { | ||
switch self { | ||
case .vpn: | ||
return "PIA VPN" | ||
case .locations: | ||
return "Location" | ||
case .settings: | ||
return "Settings" | ||
case .help: | ||
return "Help" | ||
} | ||
} | ||
|
||
var systemIconName: String { | ||
switch self { | ||
case .help: | ||
return "questionmark.circle" | ||
case .settings: | ||
return "gearshape" | ||
default: | ||
return "" | ||
} | ||
|
||
} | ||
} | ||
|
||
init(appRouter: AppRouter) { | ||
self.appRouter = appRouter | ||
self.selectedSection = calculateSelectedSection(for: appRouter.pathDestinations) | ||
subscribeToAppRouterDestinationsUpdates() | ||
} | ||
|
||
@Published var selectedSection: Sections = .vpn | ||
@Published var highlightedSection: Sections? = nil | ||
|
||
func sectionDidUpdateSelection(to section: Sections) { | ||
guard section != selectedSection else { return } | ||
selectedSection = section | ||
|
||
switch section { | ||
case .vpn: | ||
appRouter.goBackToRoot() | ||
case .locations: | ||
appRouter.navigate(to: RegionsDestinations.serversList) | ||
case .settings: | ||
// TODO: Implement me | ||
break | ||
case .help: | ||
// TODO: Implement me | ||
break | ||
} | ||
} | ||
|
||
func sectionDidUpdateFocus(to section: Sections?) { | ||
highlightedSection = section | ||
} | ||
|
||
func viewDidAppear() { | ||
// Remove the focus when the view appears | ||
sectionDidUpdateFocus(to: nil) | ||
} | ||
|
||
private func calculateSelectedSection(for pathDestinations: [any Destinations]) -> Sections { | ||
|
||
if let currentPath = pathDestinations.last { | ||
switch currentPath { | ||
case .serversList as RegionsDestinations: | ||
return .locations | ||
case .search as RegionsDestinations: | ||
return .locations | ||
case .home as DashboardDestinations: | ||
return .vpn | ||
// TODO: Add settings and help destinations when implemented | ||
default: | ||
return .vpn | ||
} | ||
} else { | ||
// The path is empty, so we are in the root | ||
return .vpn | ||
} | ||
|
||
} | ||
|
||
private func subscribeToAppRouterDestinationsUpdates() { | ||
appRouter.$pathDestinations | ||
.sink { [weak self] newPathDestinations in | ||
guard let self = self else { return } | ||
self.selectedSection = self.calculateSelectedSection(for: newPathDestinations) | ||
}.store(in: &cancellables) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// LeadingSegmentedNavigationView.swift | ||
// PIA VPN-tvOS | ||
// | ||
// Created by Laura S on 2/6/24. | ||
// Copyright © 2024 Private Internet Access Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
struct LeadingSegmentedNavigationView: View { | ||
|
||
@ObservedObject var viewModel: TopNavigationViewModel | ||
@FocusState var focusedSection: TopNavigationViewModel.Sections? | ||
|
||
func button(for section: TopNavigationViewModel.Sections) -> some View { | ||
Button { | ||
viewModel.sectionDidUpdateSelection(to: section) | ||
|
||
} label: { | ||
Text(section.title) | ||
.font(.system(size: 29, weight: .medium)) | ||
.padding(.vertical, 14) | ||
.padding(.horizontal, 28) | ||
.cornerRadius(32) | ||
.foregroundColor(section == viewModel.highlightedSection ? Color.black : Color.white) | ||
.background(Capsule().fill(section == viewModel.highlightedSection ? Color.pia_green : viewModel.selectedSection == section ? Color.pia_selection_background : Color.clear).shadow(radius: 3)) | ||
} | ||
.buttonBorderShape(.capsule) | ||
.buttonStyle(.borderless) | ||
.focused($focusedSection, equals: section) | ||
|
||
} | ||
|
||
|
||
var body: some View { | ||
VStack(alignment: .leading) { | ||
HStack(spacing: 2) { | ||
ForEach(viewModel.leadingSections, id: \.self) { section in | ||
button(for: section) | ||
} | ||
} | ||
.background(Capsule().fill(Color.pia_region_tile_background).shadow(radius: 3)) | ||
|
||
|
||
} | ||
.padding() | ||
.onChange(of: focusedSection) { oldValue, newValue in | ||
viewModel.sectionDidUpdateFocus(to: newValue) | ||
|
||
} | ||
.onAppear { | ||
viewModel.viewDidAppear() | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
struct TrailingNavigationView: View { | ||
@ObservedObject var viewModel: TopNavigationViewModel | ||
@FocusState var focusedSection: TopNavigationViewModel.Sections? | ||
|
||
var body: some View { | ||
VStack(alignment: .trailing) { | ||
HStack(spacing: 16) { | ||
ForEach(viewModel.trailingSections, id: \.self) { section in | ||
Button { | ||
viewModel.sectionDidUpdateSelection(to: section) | ||
} label: { | ||
Image(systemName: section.systemIconName) | ||
.padding(12) | ||
.foregroundColor(section == viewModel.highlightedSection ? Color.black : Color.white) | ||
.background(section == viewModel.highlightedSection ? Color.pia_green : Color.pia_region_tile_background) | ||
.clipShape(Circle()) | ||
} | ||
|
||
.focused($focusedSection, equals: section) | ||
.buttonBorderShape(.circle) | ||
.buttonStyle(.card) | ||
} | ||
} | ||
|
||
} | ||
.onChange(of: focusedSection) { oldValue, newValue in | ||
viewModel.sectionDidUpdateFocus(to: newValue) | ||
|
||
} | ||
.onAppear { | ||
viewModel.viewDidAppear() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.