Skip to content

Commit

Permalink
Merge pull request #4 from d-date/fix-crash-ios15
Browse files Browse the repository at this point in the history
Avoid initialize cell registration each time
  • Loading branch information
noppefoxwolf authored Jun 22, 2021
2 parents c53d38a + 5254c77 commit b2a0c88
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 73 deletions.
2 changes: 0 additions & 2 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_ASSET_PATHS = "\"SwiftUIExample/Preview Content\"";
DEVELOPMENT_TEAM = FBQ6Z8AF3U;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = SwiftUIExample/Info.plist;
Expand All @@ -417,7 +416,6 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_ASSET_PATHS = "\"SwiftUIExample/Preview Content\"";
DEVELOPMENT_TEAM = FBQ6Z8AF3U;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = SwiftUIExample/Info.plist;
Expand Down
154 changes: 83 additions & 71 deletions Sources/DebugMenu/View/InAppDebuggerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import UIKit
class InAppDebuggerViewController: UIViewController {
let collectionView: UICollectionView
let debuggerItems: [AnyDebugItem]

var dataSource: UICollectionViewDiffableDataSource<Section, AnyDebugItem>!

enum Section: Int, CaseIterable {
case items
}
Expand All @@ -27,68 +28,7 @@ class InAppDebuggerViewController: UIViewController {
}

required init?(coder: NSCoder) { fatalError() }

lazy var dataSource = UICollectionViewDiffableDataSource<Section, AnyDebugItem>(collectionView: collectionView, cellProvider: { (collectionView, indexPath, item) in
let selectCellRegstration = UICollectionView.CellRegistration { (cell: UICollectionViewListCell, indexPath, title: String) in
var content = cell.defaultContentConfiguration()
content.text = title
cell.contentConfiguration = content
cell.accessories = [.disclosureIndicator()]
}
let executableCellRegstration = UICollectionView.CellRegistration { (cell: UICollectionViewListCell, indexPath, title: String) in
var content = cell.defaultContentConfiguration()
content.text = title
cell.contentConfiguration = content
}
let toggleCellRegstration = UICollectionView.CellRegistration { (cell: ToggleCell, indexPath, item: (title: String, current: () -> Bool, onChange: (Bool) -> Void)) in
var content = cell.defaultContentConfiguration()
content.text = item.title
cell.contentConfiguration = content
cell.current = item.current
cell.onChange = item.onChange
}
let sliderCellRegstration = UICollectionView.CellRegistration { (cell: SliderCell, indexPath, item: (title: String, current: () -> Double, range: ClosedRange<Double>, onChange: (Double) -> Void)) in
cell.title = item.title
cell.current = item.current
cell.range = item.range
cell.onChange = item.onChange
}
switch item.action {
case .didSelect:
return collectionView.dequeueConfiguredReusableCell(
using: selectCellRegstration,
for: indexPath,
item: item.debuggerItemTitle
)
case .execute:
return collectionView.dequeueConfiguredReusableCell(
using: executableCellRegstration,
for: indexPath,
item: item.debuggerItemTitle
)
case let .toggle(current, onChange):
return collectionView.dequeueConfiguredReusableCell(
using: toggleCellRegstration,
for: indexPath,
item: (item.debuggerItemTitle, current, { [weak self] (value) in
onChange(value, { [weak self] (result) in
self?.onCompleteAction(result)
})
})
)
case let .slider(current, range, onChange):
return collectionView.dequeueConfiguredReusableCell(
using: sliderCellRegstration,
for: indexPath,
item: (item.debuggerItemTitle, current, range, { [weak self] (value) in
onChange(value, { [weak self] (result) in
self?.onCompleteAction(result)
})
})
)
}
})


override func loadView() {
view = collectionView
}
Expand All @@ -98,7 +38,7 @@ class InAppDebuggerViewController: UIViewController {

navigationItem.title = "DebugMenu"
navigationItem.largeTitleDisplayMode = .always

navigation: do {
let rightItem = UIBarButtonItem(systemItem: .done, primaryAction: UIAction(handler: { [weak self] (_) in
self?.parent?.parent?.dismiss(animated: true)
Expand All @@ -123,14 +63,9 @@ class InAppDebuggerViewController: UIViewController {
navigationController?.isToolbarHidden = false
toolbarItems = [space, item, space]
}

configureDataSource()
collectionView.delegate = self
collectionView.dataSource = dataSource

var snapshot = NSDiffableDataSourceSnapshot<Section, AnyDebugItem>()
snapshot.appendSections([Section.items])
snapshot.appendItems(debuggerItems, toSection: .items)
dataSource.apply(snapshot)
performUpdate()
}

override func viewDidAppear(_ animated: Bool) {
Expand All @@ -156,6 +91,83 @@ class InAppDebuggerViewController: UIViewController {
}
}

extension InAppDebuggerViewController {

func configureDataSource() {
let selectCellRegstration = UICollectionView.CellRegistration { (cell: UICollectionViewListCell, indexPath, title: String) in
var content = cell.defaultContentConfiguration()
content.text = title
cell.contentConfiguration = content
cell.accessories = [.disclosureIndicator()]
}

let executableCellRegstration = UICollectionView.CellRegistration { (cell: UICollectionViewListCell, indexPath, title: String) in
var content = cell.defaultContentConfiguration()
content.text = title
cell.contentConfiguration = content
}

let toggleCellRegstration = UICollectionView.CellRegistration { (cell: ToggleCell, indexPath, item: (title: String, current: () -> Bool, onChange: (Bool) -> Void)) in
var content = cell.defaultContentConfiguration()
content.text = item.title
cell.contentConfiguration = content
cell.current = item.current
cell.onChange = item.onChange
}

let sliderCellRegstration = UICollectionView.CellRegistration { (cell: SliderCell, indexPath, item: (title: String, current: () -> Double, range: ClosedRange<Double>, onChange: (Double) -> Void)) in
cell.title = item.title
cell.current = item.current
cell.range = item.range
cell.onChange = item.onChange
}

dataSource = .init(collectionView: collectionView, cellProvider: { (collectionView, indexPath, item) in
switch item.action {
case .didSelect:
return collectionView.dequeueConfiguredReusableCell(
using: selectCellRegstration,
for: indexPath,
item: item.debuggerItemTitle
)
case .execute:
return collectionView.dequeueConfiguredReusableCell(
using: executableCellRegstration,
for: indexPath,
item: item.debuggerItemTitle
)
case let .toggle(current, onChange):
return collectionView.dequeueConfiguredReusableCell(
using: toggleCellRegstration,
for: indexPath,
item: (item.debuggerItemTitle, current, { [weak self] (value) in
onChange(value, { [weak self] (result) in
self?.onCompleteAction(result)
})
})
)
case let .slider(current, range, onChange):
return collectionView.dequeueConfiguredReusableCell(
using: sliderCellRegstration,
for: indexPath,
item: (item.debuggerItemTitle, current, range, { [weak self] (value) in
onChange(value, { [weak self] (result) in
self?.onCompleteAction(result)
})
})
)
}
})
}

func performUpdate() {
var snapshot = NSDiffableDataSourceSnapshot<Section, AnyDebugItem>()
snapshot.appendSections([Section.items])
snapshot.appendItems(debuggerItems, toSection: .items)
dataSource.apply(snapshot)
}
}

extension InAppDebuggerViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
switch Section(rawValue: indexPath.section) {
Expand Down

0 comments on commit b2a0c88

Please sign in to comment.