-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MBL-1456: Stub PPO view and container (#2080)
* MBL-1456: Stub PPO view and container * Changes per review #2080
- Loading branch information
1 parent
d02fc1f
commit 4c0ee6e
Showing
9 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
Kickstarter-iOS/Features/PledgedProjectsOverview/PPOContainerViewController.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,19 @@ | ||
import Foundation | ||
import Library | ||
import SwiftUI | ||
|
||
public class PPOContainerViewController: PagedContainerViewController { | ||
public override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
// TODO: Translate these strings (MBL-1558) | ||
self.title = "Activity" | ||
let ppoViewController = UIHostingController(rootView: PPOView()) | ||
ppoViewController.title = "Project Alerts" | ||
|
||
let activitiesViewController = ActivitiesViewController.instantiate() | ||
activitiesViewController.title = "Activity Feed" | ||
|
||
self.setPagedViewControllers([ppoViewController, activitiesViewController]) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Kickstarter-iOS/Features/PledgedProjectsOverview/PPOView.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,12 @@ | ||
import SwiftUI | ||
|
||
struct PPOView: View { | ||
@StateObject private var viewModel = PPOViewModel() | ||
var body: some View { | ||
Text(self.viewModel.greeting) | ||
} | ||
} | ||
|
||
#Preview { | ||
PPOView() | ||
} |
6 changes: 6 additions & 0 deletions
6
Kickstarter-iOS/Features/PledgedProjectsOverview/PPOViewModel.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,6 @@ | ||
import Combine | ||
import Foundation | ||
|
||
public class PPOViewModel: ObservableObject { | ||
let greeting = "Hello, PPO" | ||
} |
9 changes: 9 additions & 0 deletions
9
Kickstarter-iOS/Features/PledgedProjectsOverview/PPOViewModelTests.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,9 @@ | ||
import Foundation | ||
@testable import Kickstarter_Framework | ||
import XCTest | ||
|
||
class PPOViewModelTests: XCTestCase { | ||
func test_stub() { | ||
let vm = PPOViewModel() | ||
} | ||
} |
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
150 changes: 150 additions & 0 deletions
150
Library/PagedContainer/PagedContainerViewController.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,150 @@ | ||
import Combine | ||
import Foundation | ||
import UIKit | ||
|
||
open class PagedContainerViewController: UIViewController { | ||
private weak var activeController: UIViewController? = nil | ||
private var subscriptions = Set<AnyCancellable>() | ||
private let viewModel = PagedContainerViewModel() | ||
|
||
// TODO: Use the correct page control, per the designs. | ||
// This may exist already in SortPagerViewController, or we can write one in SwiftUI. | ||
private lazy var toggle = UISegmentedControl( | ||
frame: .zero, | ||
actions: [] | ||
) | ||
|
||
open override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
self.view.backgroundColor = .white | ||
self.view.addSubview(self.toggle) | ||
self.toggle.translatesAutoresizingMaskIntoConstraints = false | ||
self.toggle.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true | ||
self.toggle.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true | ||
|
||
self.viewModel.pageTitles | ||
.sink { [weak self] titles in | ||
self?.configureSegmentedControl(withTitles: titles) | ||
}.store(in: &self.subscriptions) | ||
|
||
self.viewModel.displayChildViewControllerAtIndex.receive(on: RunLoop.main) | ||
.sink { [weak self] controller, index in | ||
self?.showChildController(controller, atIndex: index) | ||
}.store(in: &self.subscriptions) | ||
} | ||
|
||
/* | ||
The custom appearanceTransition code in this UIViewController was implemented | ||
so that the child view controllers will receive viewWillAppear with animated = true | ||
when a tab is selected. | ||
|
||
I initially implemented this because ActivitiesViewController filters unanimated | ||
calls to viewWillAppear; this behavior is relied on by our screenshot tests. | ||
|
||
While we should refactor the ActivitiesViewController, it also makes sense that transitions | ||
between pages in this container view controller will (eventually) be animated, even if | ||
I didn't animate them in this stub. | ||
*/ | ||
open override var shouldAutomaticallyForwardAppearanceMethods: Bool { | ||
return false | ||
} | ||
|
||
public override func viewWillAppear(_ animated: Bool) { | ||
super.viewWillAppear(animated) | ||
|
||
self.viewModel.viewWillAppear() | ||
|
||
if let activeController = self.activeController { | ||
activeController.beginAppearanceTransition(true, animated: animated) | ||
} | ||
} | ||
|
||
public override func viewDidAppear(_ animated: Bool) { | ||
super.viewDidAppear(animated) | ||
|
||
if let activeController = self.activeController { | ||
activeController.endAppearanceTransition() | ||
} | ||
} | ||
|
||
override open func viewWillDisappear(_ animated: Bool) { | ||
super.viewWillDisappear(animated) | ||
|
||
if let activeController = self.activeController { | ||
activeController.beginAppearanceTransition(false, animated: animated) | ||
} | ||
} | ||
|
||
open override func viewDidDisappear(_ animated: Bool) { | ||
super.viewDidDisappear(animated) | ||
|
||
if let activeController = self.activeController { | ||
activeController.endAppearanceTransition() | ||
} | ||
} | ||
|
||
public func setPagedViewControllers(_ controllers: [UIViewController]) { | ||
self.viewModel.configure(withChildren: controllers) | ||
} | ||
|
||
private func configureSegmentedControl(withTitles titles: [String]) { | ||
self.toggle.removeAllSegments() | ||
|
||
for (idx, title) in titles.enumerated() { | ||
let action = UIAction( | ||
title: title, | ||
handler: { [weak self] _ in self?.viewModel.didSelectPage(atIndex: idx) } | ||
) | ||
self.toggle.insertSegment(action: action, at: idx, animated: false) | ||
} | ||
} | ||
|
||
private func showChildController(_ controller: UIViewController, atIndex index: Int) { | ||
if self.toggle.selectedSegmentIndex == UISegmentedControl.noSegment { | ||
self.toggle.selectedSegmentIndex = index | ||
} | ||
|
||
if let activeController = self.activeController { | ||
self.stopDisplayingChildViewController(activeController) | ||
} | ||
|
||
self.displayChildViewController(controller) | ||
self.activeController = controller | ||
} | ||
|
||
func displayChildViewController(_ controller: UIViewController) { | ||
guard let childView = controller.view else { | ||
return | ||
} | ||
|
||
controller.beginAppearanceTransition(true, animated: true) | ||
|
||
addChild(controller) | ||
|
||
self.view.addSubview(childView) | ||
|
||
childView.translatesAutoresizingMaskIntoConstraints = false | ||
childView.topAnchor.constraint(equalTo: self.toggle.bottomAnchor).isActive = true | ||
childView.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor).isActive = true | ||
childView.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor).isActive = true | ||
childView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor).isActive = true | ||
|
||
controller.didMove(toParent: self) | ||
|
||
controller.endAppearanceTransition() | ||
} | ||
|
||
func stopDisplayingChildViewController(_ controller: UIViewController) { | ||
controller.beginAppearanceTransition(false, animated: true) | ||
|
||
controller.willMove(toParent: nil) | ||
for constraint in controller.view.constraints { | ||
constraint.isActive = false | ||
} | ||
controller.view.removeFromSuperview() | ||
controller.removeFromParent() | ||
|
||
controller.endAppearanceTransition() | ||
} | ||
} |
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,48 @@ | ||
import Combine | ||
import Foundation | ||
import UIKit | ||
|
||
public class PagedContainerViewModel { | ||
// Internal | ||
private var subscriptions = Set<AnyCancellable>() | ||
|
||
init() { | ||
self.pageTitles = self.configureWithChildrenSubject.map { controllers in | ||
controllers.map { $0.title ?? "Page " } | ||
}.eraseToAnyPublisher() | ||
|
||
self.displayChildViewControllerAtIndex = Publishers.CombineLatest( | ||
self.configureWithChildrenSubject, | ||
self.selectedIndex.compactMap { $0 } | ||
) | ||
.map { (controllers: [UIViewController], index: Int) -> (UIViewController, Int)? in | ||
if index < controllers.count { | ||
return (controllers[index], index) | ||
} else { | ||
return nil | ||
} | ||
}.compactMap { $0 } | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
// Inputs | ||
func viewWillAppear() { | ||
if self.selectedIndex.value == nil { | ||
self.didSelectPage(atIndex: 0) | ||
} | ||
} | ||
|
||
private let configureWithChildrenSubject = CurrentValueSubject<[UIViewController], Never>([]) | ||
func configure(withChildren children: [UIViewController]) { | ||
self.configureWithChildrenSubject.send(children) | ||
} | ||
|
||
private let selectedIndex = CurrentValueSubject<Int?, Never>(nil) | ||
func didSelectPage(atIndex index: Int) { | ||
self.selectedIndex.send(index) | ||
} | ||
|
||
// Outputs | ||
public let displayChildViewControllerAtIndex: AnyPublisher<(UIViewController, Int), Never> | ||
public let pageTitles: AnyPublisher<[String], Never> | ||
} |
Oops, something went wrong.