Skip to content

Commit

Permalink
Merge branch 'release/1.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackjacx committed Jul 23, 2020
2 parents d974394 + a4302b7 commit 3c37d6b
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased]

## [1.6.0] - 2020-07-23
* [#28](https://github.com/dbdrive/beiwagen/pull/28): Add ModelCollection.insert() and Section.insert() - [@stherold](https://github.com/stherold).

## [1.5.0] - 2020-06-25
* [#27](https://github.com/dbdrive/beiwagen/pull/27): Add support for Accessory Type - [@stherold](https://github.com/stherold).

Expand Down
4 changes: 2 additions & 2 deletions Example/Source/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16097" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="FJQ-W8-nq8">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16097.2" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="FJQ-W8-nq8">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
Expand Down Expand Up @@ -50,7 +50,7 @@
<rect key="frame" x="0.0" y="0.0" width="348" height="60"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Single Section w. Disclosure Indicator" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1vv-ZX-hB5">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Single Section" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1vv-ZX-hB5">
<rect key="frame" x="16" y="0.0" width="332" height="60"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
Expand Down
5 changes: 4 additions & 1 deletion Example/Source/CustomModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ struct CustomModel: ViewModel {

extension CustomModel {

init(title: String, didTap: IndexPathClosure? = nil, didDelete: IndexPathClosure? = nil) {
init(title: String,
didTap: IndexPathClosure? = nil,
didDelete: IndexPathClosure? = nil) {

self.title = title
self.didTap = didTap
self.didDelete = didDelete
Expand Down
26 changes: 18 additions & 8 deletions Example/Source/SingleSectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Source

final class SingleSectionViewController: UIViewController {

private static let data: [String] = ["Settings", "Imprint", "Recommendation", "Help", "Logout"]

let table = UITableView()
let dataSource = Source()

Expand All @@ -25,6 +27,19 @@ final class SingleSectionViewController: UIViewController {
table.addMaximizedTo(view)

setupDataSource()

let addButton = UIBarButtonItem(barButtonSystemItem: .add,
target: self,
action: #selector(onAdd(sender:)))
navigationItem.rightBarButtonItem = addButton
}

@objc func onAdd(sender: UIBarButtonItem) {

let title = Self.data.randomElement()!
let model = CustomModel(title: title, didTap: { (sender) in print(title) })
let indexPath = IndexPath(row: dataSource.collection[0].count, section: 0)
dataSource.collection.insertRow(item: model, at: indexPath)
}

private func setupDataSource() {
Expand All @@ -35,15 +50,10 @@ final class SingleSectionViewController: UIViewController {
self.table.reloadData()
}

let accessory: UITableViewCell.AccessoryType = .none
var models = Self.data.map { title in
CustomModel(title: title, accessory: .none, didTap: { (sender) in print(title) })
}

var models = [
CustomModel(title: "Einstellungen", accessory: accessory, didTap: { (sender) in print("Einstellungen") }),
CustomModel(title: "Impressung", accessory: accessory, didTap: { (sender) in print("Impressum") }),
CustomModel(title: "Empfehlen", accessory: accessory, didTap: { (sender) in print("Empfehlen") }),
CustomModel(title: "Hilfe", accessory: accessory, didTap: { (sender) in print("Hilfe") }),
CustomModel(title: "Logout", accessory: accessory, didTap: { (sender) in print("Logout") })
]
// Changing the connected cell class for all models. Alternatively you
// can just create a new model and set another default cell type.
for index in 0..<models.count {
Expand Down
2 changes: 1 addition & 1 deletion Source.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Source'
s.version = '1.5.0'
s.version = '1.6.0'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.summary = 'The generic data source implementation for all your view controllers.'
s.description = <<-DESC
Expand Down
8 changes: 4 additions & 4 deletions Source.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,12 @@
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 20;
CURRENT_PROJECT_VERSION = 21;
DEBUG_INFORMATION_FORMAT = dwarf;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = "";
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 20;
DYLIB_CURRENT_VERSION = 21;
DYLIB_INSTALL_NAME_BASE = "@rpath";
GCC_C_LANGUAGE_STANDARD = gnu11;
INFOPLIST_FILE = Source/Info.plist;
Expand Down Expand Up @@ -472,11 +472,11 @@
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 20;
CURRENT_PROJECT_VERSION = 21;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = "";
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 20;
DYLIB_CURRENT_VERSION = 21;
DYLIB_INSTALL_NAME_BASE = "@rpath";
GCC_C_LANGUAGE_STANDARD = gnu11;
INFOPLIST_FILE = Source/Info.plist;
Expand Down
28 changes: 24 additions & 4 deletions Source/Classes/ModelCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ public struct ModelCollection {

public func isInBounds(_ indexPath: IndexPath) -> Bool {

// Checking for `isEmpty` is necessary since `.section` is an alias for
// the internal array _indexes[0] which will crash if the indexPath is
// empty.
// See https://stackoverflow.com/a/17182239/971329
// Checking for `isEmpty` is necessary since `.section` is an alias for the internal array _indexes[0] which
// will crash if the indexPath is empty. See https://stackoverflow.com/a/17182239/971329
guard !indexPath.isEmpty,
0 <= indexPath.section && indexPath.section < sections.count,
0 <= indexPath.row && indexPath.row < rowsForSection(indexPath.section) else {
Expand All @@ -58,6 +56,23 @@ public struct ModelCollection {
return true
}

/// The difference to `isInBounds` is that `indexPath` can actually match sections.count and section.rows.count
/// since iOS can insert in array at this position without crashing.
/// - parameter indexPath: The indexPath for to check. Permitted values are
/// ([0...sections.count], [0...sections[indexPath.section].count]).
public func isInBoundsForInsertion(_ indexPath: IndexPath) -> Bool {

// Checking for `isEmpty` is necessary since `.section` is an alias for the internal array _indexes[0] which
// will crash if the indexPath is empty. See https://stackoverflow.com/a/17182239/971329
guard !indexPath.isEmpty,
0 <= indexPath.section && indexPath.section <= sections.count,
0 <= indexPath.row && indexPath.row <= rowsForSection(indexPath.section) else {

return false
}
return true
}

public func indexPath(for model: ViewModel) -> IndexPath? {
for (sectionIndex, section) in sections.enumerated() {
for (rowIndex, dataSourceModel) in section.models.enumerated() where model == dataSourceModel {
Expand All @@ -75,6 +90,11 @@ public struct ModelCollection {
sections.remove(at: index)
}

public mutating func insertRow(item: ViewModel, at indexPath: IndexPath) {
guard isInBoundsForInsertion(indexPath) else { return }
sections[indexPath.section].insert(item: item, at: indexPath.row)
}

public mutating func removeRow(at indexPath: IndexPath) {
guard isInBounds(indexPath) else { return }
sections[indexPath.section].remove(at: indexPath.row)
Expand Down
5 changes: 5 additions & 0 deletions Source/Classes/Section.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public extension Section {

subscript(index: Int) -> ViewModel { models[index] }

mutating func insert(item: ViewModel, at index: Int) {
guard index <= models.count else { return } // Use <= since we can insert at models.count without crash
models.insert(item, at: index)
}

mutating func remove(at index: Int) {
guard index < models.count else { return }
models.remove(at: index)
Expand Down
4 changes: 2 additions & 2 deletions Source/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.5.0</string>
<string>1.6.0</string>
<key>CFBundleVersion</key>
<string>20</string>
<string>21</string>
</dict>
</plist>
4 changes: 2 additions & 2 deletions SourceTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.5.0</string>
<string>1.6.0</string>
<key>CFBundleVersion</key>
<string>20</string>
<string>21</string>
</dict>
</plist>

0 comments on commit 3c37d6b

Please sign in to comment.