Initial commit
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
import UIKit
|
||||
|
||||
@UIApplicationMain
|
||||
final class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
var window: UIWindow?
|
||||
|
||||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
|
||||
let window = UIWindow()
|
||||
let navigationController = UINavigationController(rootViewController: TopViewController())
|
||||
navigationController.navigationBar.tintColor = .black
|
||||
window.rootViewController = navigationController
|
||||
window.makeKeyAndVisible()
|
||||
self.window = window
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "20x20",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "20x20",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "29x29",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "29x29",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "40x40",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "40x40",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "60x60",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "60x60",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "20x20",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "20x20",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "29x29",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "29x29",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "40x40",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "40x40",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "76x76",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "76x76",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "83.5x83.5",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ios-marketing",
|
||||
"size" : "1024x1024",
|
||||
"scale" : "1x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
|
||||
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<scenes>
|
||||
<!--View Controller-->
|
||||
<scene sceneID="EHf-IW-A2E">
|
||||
<objects>
|
||||
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
|
||||
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
|
||||
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
|
||||
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
|
||||
</view>
|
||||
</viewController>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
|
||||
</objects>
|
||||
<point key="canvasLocation" x="53" y="375"/>
|
||||
</scene>
|
||||
</scenes>
|
||||
</document>
|
||||
@@ -0,0 +1,13 @@
|
||||
import UIKit
|
||||
|
||||
extension UITableViewCell {
|
||||
static var name: String {
|
||||
return String(describing: self)
|
||||
}
|
||||
}
|
||||
|
||||
extension UICollectionViewCell {
|
||||
static var name: String {
|
||||
return String(describing: self)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>APPL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
<key>LSRequiresIPhoneOS</key>
|
||||
<true/>
|
||||
<key>UILaunchStoryboardName</key>
|
||||
<string>LaunchScreen</string>
|
||||
<key>UIMainStoryboardFile</key>
|
||||
<string></string>
|
||||
<key>UIRequiredDeviceCapabilities</key>
|
||||
<array>
|
||||
<string>armv7</string>
|
||||
</array>
|
||||
<key>UISupportedInterfaceOrientations</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
</array>
|
||||
<key>UISupportedInterfaceOrientations~ipad</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationPortraitUpsideDown</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,172 @@
|
||||
import UIKit
|
||||
import DiffableDataSources
|
||||
|
||||
final class InsertionSortViewController: UIViewController {
|
||||
final class Section: Hashable {
|
||||
var id = UUID()
|
||||
var nodes: [Node]
|
||||
private(set) var isSorted = false
|
||||
private var currentIndex = 1
|
||||
|
||||
init(count: Int) {
|
||||
nodes = (0..<count).map { Node(value: $0, maxValue: count) }.shuffled()
|
||||
}
|
||||
|
||||
func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(id)
|
||||
}
|
||||
|
||||
func sortNext() {
|
||||
guard !isSorted, nodes.count > 1 else {
|
||||
return isSorted = true
|
||||
}
|
||||
|
||||
var index = currentIndex
|
||||
let currentNode = nodes[index]
|
||||
index -= 1
|
||||
|
||||
while index >= 0 && currentNode.value < nodes[index].value {
|
||||
let node = nodes[index]
|
||||
nodes[index] = currentNode
|
||||
nodes[index + 1] = node
|
||||
index -= 1
|
||||
}
|
||||
|
||||
currentIndex += 1
|
||||
|
||||
if currentIndex >= nodes.count {
|
||||
isSorted = true
|
||||
}
|
||||
}
|
||||
|
||||
static func == (lhs: Section, rhs: Section) -> Bool {
|
||||
return lhs.id == rhs.id
|
||||
}
|
||||
}
|
||||
|
||||
struct Node: Hashable {
|
||||
var id = UUID()
|
||||
var value: Int
|
||||
var color: UIColor
|
||||
|
||||
init(value: Int, maxValue: Int) {
|
||||
let hue = CGFloat(value) / CGFloat(maxValue)
|
||||
self.value = value
|
||||
color = UIColor(hue: hue, saturation: 1, brightness: 1, alpha: 1)
|
||||
id = UUID()
|
||||
}
|
||||
|
||||
func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(id)
|
||||
}
|
||||
|
||||
static func == (lhs: Node, rhs: Node) -> Bool {
|
||||
return lhs.id == rhs.id
|
||||
}
|
||||
}
|
||||
|
||||
@IBOutlet private var collectionView: UICollectionView!
|
||||
private var isSorting = false
|
||||
|
||||
let nodeSize = CGSize(width: 16, height: 34)
|
||||
|
||||
private lazy var dataSource = CollectionViewDiffableDataSource<Section, Node>(collectionView: collectionView) { collectionView, indexPath, node in
|
||||
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: UICollectionViewCell.name, for: indexPath)
|
||||
cell.backgroundColor = node.color
|
||||
return cell
|
||||
}
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
title = "Insertion Sort"
|
||||
|
||||
collectionView.delegate = self
|
||||
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: UICollectionViewCell.name)
|
||||
navigationItem.rightBarButtonItem = UIBarButtonItem(title: nil, style: .plain, target: self, action: #selector(toggleSort))
|
||||
|
||||
updateSortButtonTitle()
|
||||
}
|
||||
|
||||
override func viewDidLayoutSubviews() {
|
||||
super.viewDidLayoutSubviews()
|
||||
randmize(animated: false)
|
||||
}
|
||||
|
||||
@objc func toggleSort() {
|
||||
isSorting.toggle()
|
||||
updateSortButtonTitle()
|
||||
|
||||
if isSorting {
|
||||
startInsertionSort()
|
||||
}
|
||||
}
|
||||
|
||||
func updateSortButtonTitle() {
|
||||
navigationItem.rightBarButtonItem?.title = isSorting ? "Stop" : "Sort"
|
||||
}
|
||||
|
||||
func randmize(animated: Bool) {
|
||||
let snapshot = DiffableDataSourceSnapshot<Section, Node>()
|
||||
let rows = Int(collectionView.bounds.height / nodeSize.height) - 1
|
||||
let columns = Int(collectionView.bounds.width / nodeSize.width)
|
||||
|
||||
for _ in 0..<rows {
|
||||
let section = Section(count: columns)
|
||||
snapshot.appendSections([section])
|
||||
snapshot.appendItems(section.nodes)
|
||||
}
|
||||
|
||||
dataSource.apply(snapshot, animatingDifferences: animated)
|
||||
}
|
||||
|
||||
func startInsertionSort() {
|
||||
guard isSorting else {
|
||||
return
|
||||
}
|
||||
|
||||
var isNextSortRequired = false
|
||||
let snapshot = dataSource.snapshot()
|
||||
|
||||
for section in snapshot.sectionIdentifiers where !section.isSorted {
|
||||
section.sortNext()
|
||||
let items = section.nodes
|
||||
snapshot.deleteItems(items)
|
||||
snapshot.appendItems(items, toSection: section)
|
||||
isNextSortRequired = true
|
||||
}
|
||||
|
||||
if isNextSortRequired {
|
||||
dataSource.apply(snapshot)
|
||||
}
|
||||
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(isNextSortRequired ? 150 : 1000)) { [weak self] in
|
||||
guard let self = self else {
|
||||
return
|
||||
}
|
||||
|
||||
if !isNextSortRequired {
|
||||
self.randmize(animated: false)
|
||||
}
|
||||
self.startInsertionSort()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension InsertionSortViewController: UICollectionViewDelegateFlowLayout {
|
||||
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
|
||||
return 0
|
||||
}
|
||||
|
||||
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
|
||||
return 0
|
||||
}
|
||||
|
||||
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
|
||||
return .zero
|
||||
}
|
||||
|
||||
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
||||
return nodeSize
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
|
||||
<device id="retina6_1" orientation="portrait">
|
||||
<adaptation id="fullscreen"/>
|
||||
</device>
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
|
||||
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<objects>
|
||||
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="InsertionSortViewController" customModule="Example_iOS" customModuleProvider="target">
|
||||
<connections>
|
||||
<outlet property="collectionView" destination="6GZ-26-LaT" id="93h-pF-UdX"/>
|
||||
<outlet property="view" destination="iN0-l3-epB" id="SEV-ah-gIe"/>
|
||||
</connections>
|
||||
</placeholder>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
|
||||
<view contentMode="scaleToFill" id="iN0-l3-epB">
|
||||
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<subviews>
|
||||
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" bounces="NO" showsHorizontalScrollIndicator="NO" showsVerticalScrollIndicator="NO" bouncesZoom="NO" dataMode="none" translatesAutoresizingMaskIntoConstraints="NO" id="6GZ-26-LaT">
|
||||
<rect key="frame" x="0.0" y="44" width="414" height="852"/>
|
||||
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
|
||||
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="IOA-0B-TAt">
|
||||
<size key="itemSize" width="50" height="50"/>
|
||||
<size key="headerReferenceSize" width="0.0" height="0.0"/>
|
||||
<size key="footerReferenceSize" width="0.0" height="0.0"/>
|
||||
<inset key="sectionInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
|
||||
</collectionViewFlowLayout>
|
||||
</collectionView>
|
||||
</subviews>
|
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
|
||||
<constraints>
|
||||
<constraint firstItem="6GZ-26-LaT" firstAttribute="top" secondItem="vUN-kp-3ea" secondAttribute="top" id="N1l-xD-bVR"/>
|
||||
<constraint firstAttribute="trailing" secondItem="6GZ-26-LaT" secondAttribute="trailing" id="WAV-gN-b0a"/>
|
||||
<constraint firstAttribute="bottom" secondItem="6GZ-26-LaT" secondAttribute="bottom" id="cwF-t5-pdY"/>
|
||||
<constraint firstItem="6GZ-26-LaT" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" id="lef-dU-E4k"/>
|
||||
</constraints>
|
||||
<viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
|
||||
</view>
|
||||
</objects>
|
||||
</document>
|
||||
@@ -0,0 +1,12 @@
|
||||
import UIKit
|
||||
|
||||
final class LabelCell: UICollectionViewCell {
|
||||
@IBOutlet var label: UILabel!
|
||||
|
||||
override func awakeFromNib() {
|
||||
super.awakeFromNib()
|
||||
|
||||
layer.borderWidth = 1
|
||||
layer.borderColor = UIColor.gray.cgColor
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
|
||||
<device id="retina6_1" orientation="portrait">
|
||||
<adaptation id="fullscreen"/>
|
||||
</device>
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
|
||||
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<objects>
|
||||
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
|
||||
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" id="pYo-4D-KR8" customClass="LabelCell" customModule="Example_iOS" customModuleProvider="target">
|
||||
<rect key="frame" x="0.0" y="0.0" width="300" height="32"/>
|
||||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
|
||||
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO">
|
||||
<rect key="frame" x="0.0" y="0.0" width="300" height="32"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<subviews>
|
||||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="qKi-9G-J20">
|
||||
<rect key="frame" x="10" y="0.0" width="280" height="32"/>
|
||||
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
|
||||
<nil key="textColor"/>
|
||||
<nil key="highlightedColor"/>
|
||||
</label>
|
||||
</subviews>
|
||||
</view>
|
||||
<constraints>
|
||||
<constraint firstItem="qKi-9G-J20" firstAttribute="leading" secondItem="pYo-4D-KR8" secondAttribute="leading" constant="10" id="UJN-ZW-lz4"/>
|
||||
<constraint firstAttribute="trailing" secondItem="qKi-9G-J20" secondAttribute="trailing" constant="10" id="VbK-qH-sMr"/>
|
||||
<constraint firstItem="qKi-9G-J20" firstAttribute="top" secondItem="pYo-4D-KR8" secondAttribute="top" id="YkZ-vn-KK6"/>
|
||||
<constraint firstAttribute="bottom" secondItem="qKi-9G-J20" secondAttribute="bottom" id="au1-M8-Hh2"/>
|
||||
</constraints>
|
||||
<viewLayoutGuide key="safeArea" id="wQ5-Q0-9Pt"/>
|
||||
<connections>
|
||||
<outlet property="label" destination="qKi-9G-J20" id="hp7-fh-rao"/>
|
||||
</connections>
|
||||
<point key="canvasLocation" x="-1" y="74"/>
|
||||
</collectionViewCell>
|
||||
</objects>
|
||||
</document>
|
||||
@@ -0,0 +1,84 @@
|
||||
import UIKit
|
||||
import DiffableDataSources
|
||||
|
||||
final class MountainsViewController: UIViewController {
|
||||
enum Section {
|
||||
case main
|
||||
}
|
||||
|
||||
struct Mountain: Hashable {
|
||||
var name: String
|
||||
|
||||
func contains(_ filter: String) -> Bool {
|
||||
guard !filter.isEmpty else {
|
||||
return true
|
||||
}
|
||||
|
||||
let lowercasedFilter = filter.lowercased()
|
||||
return name.lowercased().contains(lowercasedFilter)
|
||||
}
|
||||
}
|
||||
|
||||
@IBOutlet private var searchBar: UISearchBar!
|
||||
@IBOutlet private var collectionView: UICollectionView!
|
||||
|
||||
private lazy var dataSource = CollectionViewDiffableDataSource<Section, Mountain>(collectionView: collectionView) { collectionView, indexPath, mountain in
|
||||
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LabelCell.name, for: indexPath) as! LabelCell
|
||||
cell.label.text = mountain.name
|
||||
return cell
|
||||
}
|
||||
|
||||
private let allMountains: [Mountain] = mountainsRawData.components(separatedBy: .newlines).map { line in
|
||||
let name = line.components(separatedBy: ",")[0]
|
||||
return Mountain(name: name)
|
||||
}
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
title = "Mountains Search"
|
||||
|
||||
searchBar.delegate = self
|
||||
collectionView.delegate = self
|
||||
collectionView.register(UINib(nibName: LabelCell.name, bundle: .main), forCellWithReuseIdentifier: LabelCell.name)
|
||||
|
||||
search(filter: "")
|
||||
}
|
||||
|
||||
func search(filter: String) {
|
||||
let mountains = allMountains.lazy
|
||||
.filter { $0.contains(filter) }
|
||||
.sorted { $0.name < $1.name }
|
||||
|
||||
let snapshot = DiffableDataSourceSnapshot<Section, Mountain>()
|
||||
snapshot.appendSections([.main])
|
||||
snapshot.appendItems(mountains)
|
||||
dataSource.apply(snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
extension MountainsViewController: UISearchBarDelegate {
|
||||
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
|
||||
search(filter: searchText)
|
||||
}
|
||||
}
|
||||
|
||||
extension MountainsViewController: UICollectionViewDelegateFlowLayout {
|
||||
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
|
||||
return 10
|
||||
}
|
||||
|
||||
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
|
||||
return 10
|
||||
}
|
||||
|
||||
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
|
||||
return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
|
||||
}
|
||||
|
||||
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
||||
let column = 2
|
||||
let width = (collectionView.bounds.width - 10 * CGFloat(column + 1)) / CGFloat(column)
|
||||
return CGSize(width: width, height: 32)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
|
||||
<device id="retina6_1" orientation="portrait">
|
||||
<adaptation id="fullscreen"/>
|
||||
</device>
|
||||
<dependencies>
|
||||
<deployment identifier="iOS"/>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
|
||||
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<objects>
|
||||
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="MountainsViewController" customModule="DiffableDataSourcesExample_iOS" customModuleProvider="target">
|
||||
<connections>
|
||||
<outlet property="collectionView" destination="kRf-0w-rJc" id="OBe-Sd-A2L"/>
|
||||
<outlet property="searchBar" destination="t4I-uz-ABD" id="NtR-qE-qZn"/>
|
||||
<outlet property="view" destination="a1D-Hd-aJU" id="Oyu-9w-QKH"/>
|
||||
</connections>
|
||||
</placeholder>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
|
||||
<view contentMode="scaleToFill" id="a1D-Hd-aJU">
|
||||
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
|
||||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
|
||||
<subviews>
|
||||
<searchBar contentMode="redraw" translatesAutoresizingMaskIntoConstraints="NO" id="t4I-uz-ABD">
|
||||
<rect key="frame" x="0.0" y="44" width="414" height="56"/>
|
||||
<textInputTraits key="textInputTraits"/>
|
||||
</searchBar>
|
||||
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" keyboardDismissMode="onDrag" dataMode="none" translatesAutoresizingMaskIntoConstraints="NO" id="kRf-0w-rJc">
|
||||
<rect key="frame" x="0.0" y="120" width="414" height="776"/>
|
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
|
||||
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="0.0" minimumInteritemSpacing="0.0" id="vHO-Ju-ApP">
|
||||
<size key="itemSize" width="50" height="50"/>
|
||||
<size key="headerReferenceSize" width="0.0" height="0.0"/>
|
||||
<size key="footerReferenceSize" width="0.0" height="0.0"/>
|
||||
<inset key="sectionInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
|
||||
</collectionViewFlowLayout>
|
||||
</collectionView>
|
||||
</subviews>
|
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
|
||||
<constraints>
|
||||
<constraint firstAttribute="bottom" secondItem="kRf-0w-rJc" secondAttribute="bottom" id="CBM-WJ-k93"/>
|
||||
<constraint firstItem="t4I-uz-ABD" firstAttribute="leading" secondItem="a1D-Hd-aJU" secondAttribute="leading" id="H5j-5C-2jy"/>
|
||||
<constraint firstAttribute="trailing" secondItem="kRf-0w-rJc" secondAttribute="trailing" id="Hog-jr-nJY"/>
|
||||
<constraint firstItem="t4I-uz-ABD" firstAttribute="top" secondItem="ujY-YI-BSN" secondAttribute="top" id="IkY-zg-lm8"/>
|
||||
<constraint firstItem="kRf-0w-rJc" firstAttribute="top" secondItem="t4I-uz-ABD" secondAttribute="bottom" constant="20" id="aJ9-Wc-4Jd"/>
|
||||
<constraint firstAttribute="trailing" secondItem="t4I-uz-ABD" secondAttribute="trailing" id="d5P-My-ReY"/>
|
||||
<constraint firstItem="kRf-0w-rJc" firstAttribute="leading" secondItem="a1D-Hd-aJU" secondAttribute="leading" id="esv-zM-Pc2"/>
|
||||
</constraints>
|
||||
<viewLayoutGuide key="safeArea" id="ujY-YI-BSN"/>
|
||||
<point key="canvasLocation" x="17" y="173"/>
|
||||
</view>
|
||||
</objects>
|
||||
</document>
|
||||
@@ -0,0 +1,82 @@
|
||||
import UIKit
|
||||
import DiffableDataSources
|
||||
|
||||
final class TopViewController: UIViewController {
|
||||
enum Section {
|
||||
case main
|
||||
}
|
||||
|
||||
enum Item {
|
||||
case mountains
|
||||
case insertionSort
|
||||
}
|
||||
|
||||
@IBOutlet private var tableView: UITableView!
|
||||
|
||||
private lazy var dataSource = TableViewDiffableDataSource<Section, Item>(tableView: tableView) { tableView, indexPath, item in
|
||||
let cell = tableView.dequeueReusableCell(withIdentifier: UITableViewCell.name, for: indexPath)
|
||||
cell.textLabel?.text = item.title
|
||||
cell.accessoryType = .disclosureIndicator
|
||||
return cell
|
||||
}
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
title = "DiffableDataSources"
|
||||
|
||||
tableView.delegate = self
|
||||
tableView.register(UITableViewCell.self, forCellReuseIdentifier: UITableViewCell.name)
|
||||
tableView.rowHeight = 60
|
||||
tableView.contentInset.top = 30
|
||||
|
||||
reset()
|
||||
}
|
||||
|
||||
override func viewWillAppear(_ animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
|
||||
if let indexPath = tableView.indexPathForSelectedRow {
|
||||
tableView.deselectRow(at: indexPath, animated: true)
|
||||
}
|
||||
}
|
||||
|
||||
func reset() {
|
||||
let snapshot = DiffableDataSourceSnapshot<Section, Item>()
|
||||
snapshot.appendSections([.main])
|
||||
snapshot.appendItems([.mountains, .insertionSort])
|
||||
dataSource.apply(snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
extension TopViewController: UITableViewDelegate {
|
||||
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||
guard let item = dataSource.itemIdentifier(for: indexPath) else {
|
||||
return
|
||||
}
|
||||
|
||||
let viewController: UIViewController
|
||||
|
||||
switch item {
|
||||
case .mountains:
|
||||
viewController = MountainsViewController()
|
||||
|
||||
case .insertionSort:
|
||||
viewController = InsertionSortViewController()
|
||||
}
|
||||
|
||||
navigationController?.pushViewController(viewController, animated: true)
|
||||
}
|
||||
}
|
||||
|
||||
private extension TopViewController.Item {
|
||||
var title: String {
|
||||
switch self {
|
||||
case .mountains:
|
||||
return "🗻 Mountains"
|
||||
|
||||
case .insertionSort:
|
||||
return "📱 Insertion Sort"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
|
||||
<device id="retina6_1" orientation="portrait">
|
||||
<adaptation id="fullscreen"/>
|
||||
</device>
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
|
||||
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<objects>
|
||||
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="TopViewController" customModule="Example_iOS" customModuleProvider="target">
|
||||
<connections>
|
||||
<outlet property="tableView" destination="tX9-V1-EJv" id="EHH-CF-6fE"/>
|
||||
<outlet property="view" destination="Ude-8O-7bT" id="VCy-jI-QZK"/>
|
||||
</connections>
|
||||
</placeholder>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
|
||||
<view contentMode="scaleToFill" id="Ude-8O-7bT">
|
||||
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
|
||||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
|
||||
<subviews>
|
||||
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="tX9-V1-EJv">
|
||||
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
|
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
|
||||
</tableView>
|
||||
</subviews>
|
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
|
||||
<constraints>
|
||||
<constraint firstAttribute="bottom" secondItem="tX9-V1-EJv" secondAttribute="bottom" id="0fN-0u-kPN"/>
|
||||
<constraint firstItem="tX9-V1-EJv" firstAttribute="top" secondItem="Ude-8O-7bT" secondAttribute="top" id="NML-7Q-dfc"/>
|
||||
<constraint firstItem="tX9-V1-EJv" firstAttribute="leading" secondItem="Ude-8O-7bT" secondAttribute="leading" id="h66-Ya-k2Y"/>
|
||||
<constraint firstAttribute="trailing" secondItem="tX9-V1-EJv" secondAttribute="trailing" id="phG-hF-KAp"/>
|
||||
</constraints>
|
||||
<viewLayoutGuide key="safeArea" id="iZn-f5-WVt"/>
|
||||
<point key="canvasLocation" x="68" y="141"/>
|
||||
</view>
|
||||
</objects>
|
||||
</document>
|
||||
Reference in New Issue
Block a user