forked from openedx/openedx-app-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openedx#389 from edx/feat/back-navigation-popup
[iOS] Add long tap and menu for custom navbar
- Loading branch information
Showing
19 changed files
with
249 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
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
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
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
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
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
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
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
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,93 @@ | ||
// | ||
// BackNavigationButton.swift | ||
// Core | ||
// | ||
// Created by Vadim Kuznetsov on 3.04.24. | ||
// | ||
|
||
import SwiftUI | ||
import Theme | ||
|
||
class BackButton: UIButton { | ||
override func menuAttachmentPoint(for configuration: UIContextMenuConfiguration) -> CGPoint { | ||
return .zero | ||
} | ||
} | ||
|
||
public struct BackNavigationButtonRepresentable: UIViewRepresentable { | ||
@ObservedObject var viewModel: BackNavigationButtonViewModel | ||
var action: (() -> Void)? | ||
var color: Color | ||
|
||
init(action: (() -> Void)? = nil, color: Color, viewModel: BackNavigationButtonViewModel) { | ||
self.viewModel = viewModel | ||
self.action = action | ||
self.color = color | ||
} | ||
|
||
public func makeUIView(context: Context) -> UIButton { | ||
let button = BackButton(type: .custom) | ||
let image = CoreAssets.arrowLeft.image.withRenderingMode(.alwaysTemplate) | ||
button.setImage(image, for: .normal) | ||
button.tintColor = UIColor(color) | ||
button.contentHorizontalAlignment = .leading | ||
button.addTarget(context.coordinator, action: #selector(Coordinator.buttonAction), for: .touchUpInside) | ||
button.accessibilityIdentifier = "back_button" | ||
return button | ||
} | ||
|
||
public func updateUIView(_ button: UIButton, context: Context) { | ||
var actions: [UIAction] = [] | ||
for item in viewModel.items { | ||
let action = UIAction(title: item.title) {[weak viewModel] _ in | ||
viewModel?.navigateTo(item: item) | ||
} | ||
actions.append(action) | ||
} | ||
button.menu = UIMenu(title: "", children: actions) | ||
} | ||
|
||
public func makeCoordinator() -> Coordinator { | ||
Coordinator(action: action) | ||
} | ||
|
||
public class Coordinator: NSObject { | ||
var action: (() -> Void)? | ||
init(action: (() -> Void)?) { | ||
self.action = action | ||
} | ||
|
||
@objc func buttonAction() { | ||
action?() | ||
} | ||
} | ||
} | ||
|
||
public struct BackNavigationButton: View { | ||
@StateObject var viewModel = BackNavigationButtonViewModel() | ||
private let color: Color | ||
private let action: (() -> Void)? | ||
|
||
public init( | ||
color: Color = Theme.Colors.accentXColor, | ||
action: (() -> Void)? = nil | ||
) { | ||
self.color = color | ||
self.action = action | ||
} | ||
|
||
public var body: some View { | ||
BackNavigationButtonRepresentable(action: action, color: color, viewModel: viewModel) | ||
.onAppear { | ||
viewModel.loadItems() | ||
} | ||
|
||
} | ||
} | ||
#if DEBUG | ||
struct BackNavigationButton_Previews: PreviewProvider { | ||
static var previews: some View { | ||
BackNavigationButton() | ||
} | ||
} | ||
#endif |
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,41 @@ | ||
// | ||
// BackNavigationButtonViewModel.swift | ||
// Core | ||
// | ||
// Created by Vadim Kuznetsov on 3.04.24. | ||
// | ||
|
||
import Swinject | ||
import UIKit | ||
|
||
public protocol BackNavigationProtocol { | ||
func getBackMenuItems() -> [BackNavigationMenuItem] | ||
func navigateTo(item: BackNavigationMenuItem) | ||
} | ||
|
||
public struct BackNavigationMenuItem: Identifiable { | ||
public var id: Int | ||
public var title: String | ||
|
||
public init(id: Int, title: String) { | ||
self.id = id | ||
self.title = title | ||
} | ||
} | ||
|
||
class BackNavigationButtonViewModel: ObservableObject { | ||
private let helper: BackNavigationProtocol | ||
@Published var items: [BackNavigationMenuItem] = [] | ||
|
||
init() { | ||
self.helper = Container.shared.resolve(BackNavigationProtocol.self)! | ||
} | ||
|
||
func loadItems() { | ||
self.items = helper.getBackMenuItems() | ||
} | ||
|
||
func navigateTo(item: BackNavigationMenuItem) { | ||
helper.navigateTo(item: item) | ||
} | ||
} |
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
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
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
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
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
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.