Skip to content

[Horizon] Add navigation bar #3034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import Core
import SwiftUI
import HorizonUI

struct DashboardView: View {
@ObservedObject private var viewModel: DashboardViewModel
Expand Down Expand Up @@ -50,53 +51,17 @@ struct DashboardView: View {
}
}
}
.navigationBarItems(leading: nameLabel)
.navigationBarItems(trailing: navBarIcons)
.navigationBarItems(leading: HorizonUI.NavigationBar.Leading(title: "Hi, Horizon App"))
.navigationBarItems(trailing: HorizonUI.NavigationBar.Trailing(onEvent: viewModel.onEvent))
.scrollIndicators(.hidden, axes: .vertical)
.background(Color.backgroundLight)
.onFirstAppear { viewModel.viewController = viewController}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love the pattern of requiring the view to set a property on the view model. I think it's better just to pass the viewController as an argument to the notebookDidTap method. That way it's always clear what is required. Otherwise it can be unclear to someone using the view model that they need to set that property before it'll work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok but i need to pass the closure direct to view model, because the navigation logic must be set in viewModel.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would just be one of the parameters passed in the onEvent handler:

func onEvent(event: HorizonUI.NavigationBar.Trailing.Event, viewController: WeakViewController)

You might also consider creating a separate ViewModel to pair with the HorizonUI.NavigationBar.Trailing and putting the onEvent handler in there. Otherwise we must handle the navigation logic in the ViewModel for every View that uses the HorizonUI.NavigationBar.Trailing view.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm being honest though, the amount of logic that would go into the Trailing view controller is so small and unlikely to be broken, I'd probably just put it into the view. So it's up to you what you want to do there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, HorizonUI is just for creating UI only and shouldn't have any logic in it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this component will be used only on the dashboard, I'd simply pass 3 named () -> Void closure to the HorizonUI.NavigationBar.Trailing initializer then we can have:

viewModel.notebookDidTap(viewController)
viewModel.notificationDidTap(viewController)
...the rest

that is readable easily.

}

private var nameLabel: some View {
Size16RegularTextDarkestTitle(title: viewModel.title)
}

private var navBarIcons: some View {
HStack(spacing: 0) {
Button {
viewModel.notebookDidTap(viewController)
} label: {
Image(systemName: "book.closed")
.tint(.backgroundDark)
.frame(width: 40, height: 40)
.background(Color.backgroundLightest)
.clipShape(.circle)
.shadow(color: .backgroundDark, radius: 2)
}

Button {
viewModel.notificationsDidTap()
} label: {
Image(systemName: "bell.badge")
.tint(.backgroundDark)
.frame(width: 40, height: 40)
.background(Color.backgroundLightest)
.clipShape(.circle)
.shadow(color: .backgroundDark, radius: 2)
}

Button {
viewModel.profileDidTap()
} label: {
Image(systemName: "person")
.tint(.backgroundDark)
.frame(width: 40, height: 40)
.background(Color.backgroundLightest)
.clipShape(.circle)
.shadow(color: .backgroundDark, radius: 2)
}
}
}

@ViewBuilder
private func moduleView(course: HCourse) -> some View {
if let module = course.currentModule, let moduleItem = course.currentModuleItem {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import Combine
import Core
import Foundation
import HorizonUI

final class DashboardViewModel: ObservableObject {
// MARK: - Outputs
Expand All @@ -27,6 +28,11 @@ final class DashboardViewModel: ObservableObject {
@Published private(set) var title: String = "Hi, John"
@Published private(set) var courses: [HCourse] = []

// MARK: - Input

var onEvent: (HorizonUI.NavigationBar.Trailing.Event) -> Void = { _ in}
var viewController: WeakViewController = .init()

// MARK: - Private variables

private var subscriptions = Set<AnyCancellable>()
Expand Down Expand Up @@ -55,15 +61,35 @@ final class DashboardViewModel: ObservableObject {
.map { "Hi, \($0)" }
.replaceError(with: "")
.assign(to: &$title)

configNavigationBarEvents()
}

// MARK: - Inputs

func notebookDidTap(_ viewController: WeakViewController) {
func notebookDidTap() {
router.route(to: "/notebook", from: viewController)
}

func notificationsDidTap() {}

func profileDidTap() {}
func mailDidTap() {}

// MARK: - Private Functions

private func configNavigationBarEvents() {
onEvent = { [weak self] event in
guard let self else {
return
}
switch event {
case .mail:
mailDidTap()
case .notebook:
notebookDidTap()
case .notification:
notificationsDidTap()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"images" : [
{
"filename" : "menu_book_notebook.svg",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"template-rendering-intent" : "template"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// This file is part of Canvas.
// Copyright (C) 2024-present Instructure, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

import SwiftUI
public extension HorizonUI.NavigationBar {
struct Storybook: View {
public var body: some View {
VStack {

}
.navigationBarItems(leading: HorizonUI.NavigationBar.Leading(title: "Hi HorizonUI"))
.navigationBarItems(trailing: HorizonUI.NavigationBar.Trailing(onEvent: { _ in }))
.navigationTitle("NavigationBar")
}
}
}

#Preview {
HorizonUI.NavigationBar.Storybook()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// This file is part of Canvas.
// Copyright (C) 2024-present Instructure, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

import SwiftUI

public extension HorizonUI.NavigationBar {
struct Leading: View {
let title: String

public init(title: String) {
self.title = title
}

public var body: some View {
Text(title)
.foregroundStyle(Color.huiColors.text.title)
.huiTypography(.p3)
.padding(.huiSpaces.primitives.mediumSmall)
.background(Color.huiColors.surface.pageTertiary)
}
}
}

#Preview {
HorizonUI.NavigationBar.Leading(title: "HorizonUI")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// This file is part of Canvas.
// Copyright (C) 2024-present Instructure, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

import SwiftUI

public extension HorizonUI.NavigationBar {
struct Trailing: View {
public enum Event {
case mail
case notebook
case notification
}

private let onEvent: (Event) -> Void

public init(onEvent: @escaping (Event) -> Void) {
self.onEvent = onEvent
}

public var body: some View {
HStack(spacing: .zero) {
Button {
onEvent(.notebook)
} label: {
Image.huiIcons.menuBookNotebook
.dropShadow()
}

Button {
onEvent(.notification)
} label: {
Image.huiIcons.notificationsUnread
.dropShadow()
}

Button {
onEvent(.mail)
} label: {
Image.huiIcons.mail
.dropShadow()
}
}
}
}
}

#Preview {
HorizonUI.NavigationBar.Trailing { _ in }
}

fileprivate extension HorizonUI.NavigationBar.Trailing {
struct DropShadowModifire: ViewModifier {
func body(content: Content) -> some View {
content
.foregroundStyle(Color.huiColors.icon.default)
.frame(width: 44, height: 44)
.background {
Rectangle()
.fill(Color.huiColors.surface.pageSecondary)
.huiCornerRadius(level: .level6)
.huiElevation(level: .level4)
}
}
}
}

fileprivate extension View {
func dropShadow() -> some View {
modifier(HorizonUI.NavigationBar.Trailing.DropShadowModifire())
}
}

public extension HorizonUI {
struct NavigationBar {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ public extension HorizonUI {
let circle = Image(.circle)
let ai = Image(.ai)
let aiFilled = Image(.aiFilled)
let menuBookNotebook = Image(.menuBookNotebook)

func allImages() -> [Image] {
let mirror = Mirror(reflecting: self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ public struct Storybook: View {
NavigationLink {} label: {
Text("Cards").tint(Color.black)
}
NavigationLink {} label: {
NavigationLink {
HorizonUI.NavigationBar.Storybook()
} label: {
Text("Navigation").tint(Color.black)
}
}
Expand Down