Skip to content

Commit

Permalink
generic checkmark handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ConfusedVorlon committed Feb 7, 2017
1 parent 5a2a2b0 commit 7bb56cf
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 26 deletions.
12 changes: 8 additions & 4 deletions DemoProject/Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,17 @@ class ViewController: UIViewController {
//Row value is linked to the user default 'TestDefault'
self.table.addSection("Linked to default")
row = HSTVRowInfo(title: "Linked to UserDefault 'TestDefault'")
row.linkTo(userDefault:"TestDefault", trueSubtitle: "TestDefault is true", falseSubtitle: "TestDefault is false")
row.handleCheckmark(userDefault:"TestDefault",
checkedSubtitle: "Checked (user default true)",
uncheckedSubtitle: "UnChecked (user default false)")
table += row

//Row value is linked to the user default 'TestDefault', but checkmark shows when value is false
row = HSTVRowInfo(title: "Linked to UserDefault TestOppositeDefault")
row.linkTo(userDefault:"TestOppositeDefault", trueSubtitle: "TestDefault is true", falseSubtitle: "TestDefault is false", checkmarkShowsForFalse: true)
row = HSTVRowInfo(title: "Linked to UserDefault 'TestOppositeDefault'")
row.handleCheckmark(userDefault:"TestOppositeDefault",
checkedSubtitle: "Checked (user default false)",
uncheckedSubtitle: "UnChecked (user default true)",
checkmarkShowsForFalse: true)
table += row

//Various accessory views
Expand Down Expand Up @@ -176,7 +181,6 @@ class ViewController: UIViewController {
}
section.info.reuseIdentifier="orange"


self.table.applyDataUpdate()
}

Expand Down
58 changes: 40 additions & 18 deletions HSTableView/HSTVRowInfoHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,75 @@

import UIKit


extension HSTVRowInfo {


/**
Convenience function that uses the click handler and styleBeforeDisplay handler to link a row to an NSUserDefault
The checkmark shows the value of the default (or the opposite if checkmarkShowsForFalse is true)
Convenience function that uses the click handler and styleBeforeDisplay handler to update a checkmark
The get and set functions let you deal with the result
The true and false subtitles give the user more info. (Note - if you set one of these, you must set both)
*/
public func linkTo(userDefault:String, trueSubtitle: String?, falseSubtitle: String?, checkmarkShowsForFalse: Bool = false){
*/
public func handleCheckmark(checkedSubtitle: String?, uncheckedSubtitle: String?, get:@escaping () -> Bool, set:@escaping (Bool) -> Void){

assert( (trueSubtitle != nil && falseSubtitle != nil) || (trueSubtitle == nil && falseSubtitle == nil),"If you provide a trueSubtitle, or falseSubtitle, you must provide both")
assert( (checkedSubtitle != nil && uncheckedSubtitle != nil) || (checkedSubtitle == nil && uncheckedSubtitle == nil),"If you provide a trueSubtitle, or falseSubtitle, you must provide both")

self.clickHandler = {
(row) in
let currentValue=UserDefaults.standard.bool(forKey: userDefault)
UserDefaults.standard.set(!currentValue, forKey: userDefault)
let currentValue=get()
set(!currentValue)
row.redrawCell(UITableViewRowAnimation.fade)
};

self.styleBeforeDisplayHandler = {
(row,cell) in
let value=UserDefaults.standard.bool(forKey: userDefault)
var checkmarkValue = value
if checkmarkShowsForFalse {
checkmarkValue = !value
}
let value=get()
let checkmarkValue = value

if (value)
{
if (trueSubtitle != nil)
if (checkedSubtitle != nil)
{
cell.detailTextLabel?.text=trueSubtitle
cell.detailTextLabel?.text=checkedSubtitle
}
}
else
{
if (falseSubtitle != nil)
if (uncheckedSubtitle != nil)
{
cell.detailTextLabel?.text=falseSubtitle
cell.detailTextLabel?.text=uncheckedSubtitle
}
}

cell.accessoryType = checkmarkValue ? UITableViewCellAccessoryType.checkmark : UITableViewCellAccessoryType.none

}
}




/**
Convenience function that uses the click handler and styleBeforeDisplay handler to link a row to an NSUserDefault
The checkmark shows the value of the default (or the opposite if checkmarkShowsForFalse is true)
The true and false subtitles give the user more info. (Note - if you set one of these, you must set both)
*/
public func handleCheckmark(userDefault:String, checkedSubtitle: String?, uncheckedSubtitle: String?, checkmarkShowsForFalse: Bool = false){


handleCheckmark(checkedSubtitle: checkedSubtitle,
uncheckedSubtitle: uncheckedSubtitle,
get: { () -> Bool in
let value=UserDefaults.standard.bool(forKey: userDefault)
return checkmarkShowsForFalse ? !value : value
}) { (newValue) in
let value = checkmarkShowsForFalse ? !newValue : newValue
UserDefaults.standard.set(value, forKey: userDefault)
}

}




/// Simple delete handler that just gets rid of the row
/// You can use this as 'row.deleteHandler=row.simpleDeleteHandler'
///
Expand Down
Binary file modified Images/complexDemo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,17 @@ this function completely defines the example table
//Row value is linked to the user default 'TestDefault'
self.table.addSection("Linked to default")
row = HSTVRowInfo(title: "Linked to UserDefault 'TestDefault'")
row.linkTo(userDefault:"TestDefault", trueSubtitle: "TestDefault is true", falseSubtitle: "TestDefault is false")
row.handleCheckmark(userDefault:"TestDefault",
checkedSubtitle: "Checked (user default true)",
uncheckedSubtitle: "UnChecked (user default false)")
table += row

//Row value is linked to the user default 'TestDefault', but checkmark shows when value is false
row = HSTVRowInfo(title: "Linked to UserDefault TestOppositeDefault")
row.linkTo(userDefault:"TestOppositeDefault", trueSubtitle: "TestDefault is true", falseSubtitle: "TestDefault is false", checkmarkShowsForFalse: true)
row = HSTVRowInfo(title: "Linked to UserDefault 'TestOppositeDefault'")
row.handleCheckmark(userDefault:"TestOppositeDefault",
checkedSubtitle: "Checked (user default false)",
uncheckedSubtitle: "UnChecked (user default true)",
checkmarkShowsForFalse: true)
table += row

//Various accessory views
Expand Down Expand Up @@ -217,7 +222,6 @@ this function completely defines the example table
}
section.info.reuseIdentifier="orange"


self.table.applyDataUpdate()
}
```
Expand Down

0 comments on commit 7bb56cf

Please sign in to comment.