-
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
- Loading branch information
1 parent
87add1a
commit 5a4eea7
Showing
9 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
Kickstarter-iOS/Features/PostPledgeOverview/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 | ||
self.title = "Activity" | ||
let ppoViewController = UIHostingController(rootView: PPOView()) | ||
ppoViewController.title = "Project Alerts" | ||
|
||
let activitiesViewController = ActivitiesViewController.instantiate() | ||
activitiesViewController.title = "Activity Feed" | ||
|
||
self.setPagedViewControllers([ppoViewController, activitiesViewController]) | ||
} | ||
} |
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/PostPledgeOverview/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/PostPledgeOverview/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
138 changes: 138 additions & 0 deletions
138
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,138 @@ | ||
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.receive(on: RunLoop.main) | ||
.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) | ||
} | ||
|
||
open override var shouldAutomaticallyForwardAppearanceMethods: Bool { | ||
return false | ||
} | ||
|
||
public override func viewWillAppear(_ animated: Bool) { | ||
super.viewWillAppear(animated) | ||
|
||
if self.toggle.selectedSegmentIndex == UISegmentedControl.noSegment { | ||
self.viewModel.didSelectPage(atIndex: 0) | ||
} else 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,24 @@ | ||
import Combine | ||
import Foundation | ||
import UIKit | ||
|
||
public class PagedContainerViewModel { | ||
// Internal | ||
private var childViewControllers: [UIViewController] = [] | ||
|
||
// Inputs | ||
func configure(withChildren children: [UIViewController]) { | ||
self.childViewControllers = children | ||
self.pageTitles.send(children.map { $0.title ?? "Page" }) | ||
} | ||
|
||
func didSelectPage(atIndex index: Int) { | ||
if index < self.childViewControllers.count { | ||
self.displayChildViewControllerAtIndex.send((self.childViewControllers[index], index)) | ||
} | ||
} | ||
|
||
// Outputs | ||
public let displayChildViewControllerAtIndex = PassthroughSubject<(UIViewController, Int), Never>() | ||
public let pageTitles = CurrentValueSubject<[String], Never>([]) | ||
} |
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