forked from ra1028/DifferenceKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
365 additions
and
454 deletions.
There are no files selected for viewing
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
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,26 @@ | ||
import UIKit | ||
|
||
final class EmojiCell: UICollectionViewCell, NibReusable { | ||
@IBOutlet weak var label: UILabel! | ||
|
||
override func awakeFromNib() { | ||
super.awakeFromNib() | ||
|
||
contentView.backgroundColor = UIColor(white: 0.95, alpha: 1) | ||
contentView.layer.cornerRadius = 8 | ||
contentView.layer.shadowOffset = CGSize(width: 0, height: 7) | ||
} | ||
|
||
override var isHighlighted: Bool { | ||
didSet { alpha = isHidden ? 0.2 : 1 } | ||
} | ||
|
||
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) { | ||
coordinator.addCoordinatedAnimations({ [weak self] in | ||
guard let self = self else { return } | ||
self.contentView.layer.shadowOpacity = self.isFocused ? 0.3 : 0 | ||
self.contentView.layer.transform = self.isFocused ? CATransform3DMakeScale(1.1, 1.1, 1) : CATransform3DIdentity | ||
self.layer.zPosition = self.isFocused ? 1 : 0 | ||
}) | ||
} | ||
} |
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,45 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<document type="com.apple.InterfaceBuilder.AppleTV.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="AppleTV" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES"> | ||
<device id="appleTV" orientation="landscape"> | ||
<adaptation id="light"/> | ||
</device> | ||
<dependencies> | ||
<deployment identifier="tvOS"/> | ||
<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" multipleTouchEnabled="YES" contentMode="center" id="Zg6-US-FcK" customClass="EmojiCell" customModule="Example_tvOS" customModuleProvider="target"> | ||
<rect key="frame" x="0.0" y="0.0" width="150" height="90"/> | ||
<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="150" height="90"/> | ||
<autoresizingMask key="autoresizingMask"/> | ||
<subviews> | ||
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="😁" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Xh8-3R-aih"> | ||
<rect key="frame" x="0.0" y="0.0" width="150" height="90"/> | ||
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/> | ||
<fontDescription key="fontDescription" style="UICTFontTextStyleHeadline"/> | ||
<nil key="textColor"/> | ||
<nil key="highlightedColor"/> | ||
</label> | ||
</subviews> | ||
</view> | ||
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/> | ||
<constraints> | ||
<constraint firstItem="Xh8-3R-aih" firstAttribute="leading" secondItem="Zg6-US-FcK" secondAttribute="leading" id="Lf3-dv-Js4"/> | ||
<constraint firstAttribute="bottom" secondItem="Xh8-3R-aih" secondAttribute="bottom" id="Q6r-1B-mOJ"/> | ||
<constraint firstItem="Xh8-3R-aih" firstAttribute="top" secondItem="Zg6-US-FcK" secondAttribute="top" id="Qew-Zz-RZi"/> | ||
<constraint firstAttribute="trailing" secondItem="Xh8-3R-aih" secondAttribute="trailing" id="nxP-jf-QMm"/> | ||
</constraints> | ||
<viewLayoutGuide key="safeArea" id="1K2-3x-ulA"/> | ||
<connections> | ||
<outlet property="label" destination="Xh8-3R-aih" id="ert-4A-vyJ"/> | ||
</connections> | ||
<point key="canvasLocation" x="67" y="-8"/> | ||
</collectionViewCell> | ||
</objects> | ||
</document> |
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,89 @@ | ||
import UIKit | ||
import DifferenceKit | ||
|
||
final class EmojiViewController: UIViewController { | ||
enum SectionID: Differentiable, CaseIterable { | ||
case first, second, third | ||
} | ||
|
||
typealias Section = ArraySection<SectionID, String> | ||
|
||
@IBOutlet private weak var collectionView: UICollectionView! | ||
|
||
private var data = [Section]() | ||
private var dataInput: [Section] { | ||
get { return data } | ||
set { | ||
let changeset = StagedChangeset(source: data, target: newValue) | ||
collectionView.reload(using: changeset) { data in | ||
self.data = data | ||
} | ||
} | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
title = "Emoji" | ||
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(refresh)) | ||
|
||
collectionView.delegate = self | ||
collectionView.dataSource = self | ||
collectionView.register(cellType: EmojiCell.self) | ||
|
||
refresh() | ||
} | ||
|
||
@IBAction func refresh() { | ||
let ids = SectionID.allCases | ||
let Emojis = (0x1F600...0x1F647).compactMap { UnicodeScalar($0).map(String.init) } | ||
let splitedCount = Int((Double(Emojis.count) / Double(ids.count)).rounded(.up)) | ||
|
||
dataInput = ids.enumerated().map { offset, model in | ||
let start = offset * splitedCount | ||
let end = min(start + splitedCount, Emojis.endIndex) | ||
let Emojis = Emojis[start..<end] | ||
return Section(model: model, elements: Emojis) | ||
} | ||
} | ||
|
||
@IBAction func shuffleAllEmojis() { | ||
var flattenEmojis = ArraySlice(dataInput.flatMap { $0.elements }) | ||
flattenEmojis.shuffle() | ||
|
||
dataInput = dataInput.map { section in | ||
var section = section | ||
section.elements = Array(flattenEmojis.prefix(section.elements.count)) | ||
flattenEmojis.removeFirst(section.elements.count) | ||
return section | ||
} | ||
} | ||
|
||
@IBAction func shuffleSections() { | ||
dataInput.shuffle() | ||
} | ||
|
||
func remove(at indexPath: IndexPath) { | ||
dataInput[indexPath.section].elements.remove(at: indexPath.item) | ||
} | ||
} | ||
|
||
extension EmojiViewController: UICollectionViewDataSource, UICollectionViewDelegate { | ||
func numberOfSections(in collectionView: UICollectionView) -> Int { | ||
return data.count | ||
} | ||
|
||
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | ||
return data[section].elements.count | ||
} | ||
|
||
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | ||
let cell: EmojiCell = collectionView.dequeueReusableCell(for: indexPath) | ||
cell.label.text = data[indexPath.section].elements[indexPath.item] | ||
return cell | ||
} | ||
|
||
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | ||
remove(at: indexPath) | ||
} | ||
} |
Oops, something went wrong.