Skip to content

Commit

Permalink
Merge pull request #663 from OneBusAway/tweaks
Browse files Browse the repository at this point in the history
Adopt readableContentGuides to OBAListView
  • Loading branch information
ualch9 authored Apr 7, 2023
2 parents d50aad0 + 01243e8 commit 92e4fec
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// NSCollectionLayoutSection+ReadableContentGuide.swift
// OBAKit
//
// Created by Alan Chu on 4/7/23.
//

import Foundation

extension NSCollectionLayoutSection {
/// `NSCollectionLayoutSection.list` with content insets set to the Readable Content Guide.
@MainActor static func readableContentGuideList(
using configuration: UICollectionLayoutListConfiguration,
layoutEnvironment: NSCollectionLayoutEnvironment
) -> NSCollectionLayoutSection {
let section = self.list(using: configuration, layoutEnvironment: layoutEnvironment)

// Change the section's content insets reference to the readable content.
// This changes the way that the insets in the section's contentInsets property are interpreted.
section.contentInsetsReference = .readableContent

// Zero out the default leading/trailing contentInsets, but preserve the default top/bottom values.
// This ensures each section will be inset horizontally exactly to the readable content width.
var contentInsets = section.contentInsets
contentInsets.leading = 0
contentInsets.trailing = 0
section.contentInsets = contentInsets

return section
}
}
2 changes: 1 addition & 1 deletion OBAKit/Controls/ListView/OBAListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public class OBAListView: UICollectionView, UICollectionViewDelegate {
trailingSwipeActions(for: indexPath)
}

return NSCollectionLayoutSection.list(using: configuration, layoutEnvironment: environment)
return NSCollectionLayoutSection.readableContentGuideList(using: configuration, layoutEnvironment: environment)
}
}

Expand Down
2 changes: 2 additions & 0 deletions OBAKit/Mapping/MapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ class MapViewController: UIViewController,
// Set a content view controller.
panel.set(contentViewController: mapPanelController)

panel.contentMode = .fitToBounds

// Content Inset Adjustment + OBAListView don't play well together and causes undefined behavior,
// as described in "OBAListView "sticky" row behavior while scrolling in panel" (#321)
panel.contentInsetAdjustmentBehavior = .never
Expand Down
34 changes: 17 additions & 17 deletions OBAKit/Mapping/NearbyStopsListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,25 @@ class NearbyStopsListViewController: UIViewController, UICollectionViewDelegate,
// MARK: - UICollectionView properties
private var collectionView: UICollectionView!
private var diffableDataSource: UICollectionViewDiffableDataSource<Section, ItemType>!
private var headerCellRegistration: UICollectionView.CellRegistration<UICollectionViewListCell, ItemType>!
private var cellRegistration: UICollectionView.CellRegistration<UICollectionViewListCell, ItemType>!
private lazy var emptyDataView: EmptyDataSetView = {
let view = EmptyDataSetView(alignment: .top)
view.translatesAutoresizingMaskIntoConstraints = false
let view = EmptyDataSetView()
view.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin]
return view
}()

// MARK: - UIViewController lifecycle
override func viewDidLoad() {
super.viewDidLoad()

self.headerCellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, ItemType> { cell, _, item in
var config = cell.defaultContentConfiguration()
config.text = item.title
config.textProperties.font = .preferredFont(forTextStyle: .headline)
cell.contentConfiguration = config
}

self.cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, ItemType> { cell, _, item in
var config = cell.defaultContentConfiguration()
config.text = item.title
Expand Down Expand Up @@ -129,25 +137,17 @@ class NearbyStopsListViewController: UIViewController, UICollectionViewDelegate,
config.backgroundColor = .clear
config.headerMode = .firstItemInSection

let section = NSCollectionLayoutSection.list(using: config, layoutEnvironment: layoutEnvironment)

// Change the section's content insets reference to the readable content.
// This changes the way that the insets in the section's contentInsets property are interpreted.
section.contentInsetsReference = .readableContent

// Zero out the default leading/trailing contentInsets, but preserve the default top/bottom values.
// This ensures each section will be inset horizontally exactly to the readable content width.
var contentInsets = section.contentInsets
contentInsets.leading = 0
contentInsets.trailing = 0
section.contentInsets = contentInsets

return section
return NSCollectionLayoutSection.readableContentGuideList(using: config, layoutEnvironment: layoutEnvironment)
}
}

private func cellFor(_ collectionView: UICollectionView, indexPath: IndexPath, itemIdentifier: ItemType) -> UICollectionViewCell? {
return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: itemIdentifier)
switch itemIdentifier.type {
case .header:
return collectionView.dequeueConfiguredReusableCell(using: headerCellRegistration, for: indexPath, item: itemIdentifier)
case .alert, .stop:
return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: itemIdentifier)
}
}

private func toggleEmptyDataView(isShowing: Bool) {
Expand Down

0 comments on commit 92e4fec

Please sign in to comment.