Skip to content
This repository has been archived by the owner on Aug 24, 2020. It is now read-only.

Commit

Permalink
Add dynamic options support
Browse files Browse the repository at this point in the history
  • Loading branch information
davdroman committed Sep 25, 2015
1 parent 9f8136c commit 5a0a17b
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Bohr.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Bohr"
s.version = "3.0.0-rc.1"
s.version = "3.0.0-rc.2"
s.summary = "Settings screen composing framework"
s.homepage = "https://github.com/DavdRoman/Bohr"
s.author = { "David Román" => "[email protected]" }
Expand Down
7 changes: 6 additions & 1 deletion Bohr.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@
75C7ADC91B1AB2F80050C8AA /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0640;
LastUpgradeCheck = 0700;
TargetAttributes = {
75C7ADD21B1AB3010050C8AA = {
CreatedOnToolsVersion = 6.3.2;
Expand Down Expand Up @@ -435,6 +435,7 @@
75C7ADCD1B1AB2F80050C8AA /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ENABLE_TESTABILITY = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
ONLY_ACTIVE_ARCH = YES;
};
Expand Down Expand Up @@ -490,6 +491,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "me.davidroman.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SDKROOT = iphoneos;
Expand Down Expand Up @@ -533,6 +535,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = "me.davidroman.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SDKROOT = iphoneos;
Expand Down Expand Up @@ -589,6 +592,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "me.davidroman.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SDKROOT = iphoneos;
Expand Down Expand Up @@ -640,6 +644,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = "me.davidroman.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SDKROOT = iphoneos;
Expand Down
13 changes: 8 additions & 5 deletions Bohr.xcodeproj/xcshareddata/xcschemes/Bohr.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0640"
LastUpgradeVersion = "0700"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -23,21 +23,24 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
Expand All @@ -52,10 +55,10 @@
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
Expand Down
6 changes: 6 additions & 0 deletions Bohr/BOTableViewCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
/// An optional destination view controller to push when the cell is pressed.
@property (nonatomic) UIViewController *destinationViewController;

/// An optional key in order to determine whether the cell should be visible or not depending on its value.
@property (nonatomic) NSString *visibilityKey;

/// A block determining whether the cell should be visible or not.
@property (nonatomic, copy) BOOL (^visibilityBlock)(id settingValue);

/** Initializes a new BOTableViewCell object.
*
* @param title The cell title.
Expand Down
27 changes: 27 additions & 0 deletions Bohr/BOTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

@interface BOTableViewController ()

@property (nonatomic, copy) NSArray *lastSections;
@property (nonatomic) NSArray *sections;
@property (nonatomic) NSArray *footerViews;

Expand Down Expand Up @@ -150,6 +151,32 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
}

- (void)reloadTableView {

#pragma mark Dynamic options

NSMutableIndexSet *affectedIndexes = [NSMutableIndexSet new];

for (NSInteger s = 0; s < self.tableView.numberOfSections; s++) {
NSInteger numberOfRows = [self.tableView numberOfRowsInSection:s];

if (numberOfRows != [self.sections[s] cells].count) {
[affectedIndexes addIndex:s];
} else {
for (NSInteger r = 0; r < numberOfRows; r++) {
UITableViewCell *lastCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]];
if (![[self.sections[s] cells] containsObject:lastCell]) {
[affectedIndexes addIndex:s];
}
}
}
}

if (affectedIndexes.count > 0) {
[self.tableView beginUpdates];
[self.tableView reloadSections:affectedIndexes withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}

[UIView performWithoutAnimation:^{
CGPoint previousContentOffset = self.tableView.contentOffset;
[self.tableView beginUpdates];
Expand Down
2 changes: 1 addition & 1 deletion Bohr/BOTableViewSection.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/// The footer title color of the section.
@property (nonatomic) UIFont *footerTitleFont MZ_APPEARANCE_SELECTOR;

/// The cells for the section.
/// The visible cells in the section.
@property (nonatomic, readonly) NSArray *cells;

/** Initializes a new BOTableViewSection object.
Expand Down
17 changes: 14 additions & 3 deletions Bohr/BOTableViewSection.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

@interface BOTableViewSection ()

@property (nonatomic) NSArray *rawCells;
@property (nonatomic) NSArray *cells;

@end
Expand All @@ -22,9 +23,9 @@ + (instancetype)appearance {

- (instancetype)init {
if (self = [super init]) {
self.cells = [NSArray new];
self.rawCells = [NSArray new];
// Workaround to UITableViewHeaderFooterView's default 0-sized font driving iOS 9 nuts (yeah, WTF Apple).
self.footerTitleFont = [UIFont fontWithName:@"Helvetica" size:13];
self.footerTitleFont = [UIFont systemFontOfSize:13];
// I mean, really, if you...
// NSLog(@"%@", [UITableViewHeaderFooterView new].textLabel.font);
// you get "font-size: 0.00pt".
Expand All @@ -50,7 +51,17 @@ + (instancetype)sectionWithHeaderTitle:(NSString *)headerTitle handler:(void (^)
}

- (void)addCell:(BOTableViewCell *)cell {
self.cells = [self.cells arrayByAddingObject:cell];
self.rawCells = [self.rawCells arrayByAddingObject:cell];
}

- (NSArray *)cells {
return [self.rawCells filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(BOTableViewCell *cell, NSDictionary<NSString *,id> *bindings) {
if (cell.visibilityKey.length > 0) {
return cell.visibilityBlock([[NSUserDefaults standardUserDefaults] objectForKey:cell.visibilityKey]);
}

return YES;
}]];
}

@end
2 changes: 1 addition & 1 deletion Bohr/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>me.davidroman.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
2 changes: 1 addition & 1 deletion BohrDemo/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>me.davidroman.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
6 changes: 5 additions & 1 deletion BohrDemo/TableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ - (void)setup {

[section addCell:[BOSwitchTableViewCell cellWithTitle:@"Switch 1" key:@"bool_1" handler:nil]];

[section addCell:[BOSwitchTableViewCell cellWithTitle:@"Switch 2" key:@"bool_2" handler:^(BOSwitchTableViewCell *cell) {
[section addCell:[BOSwitchTableViewCell cellWithTitle:@"Switch 2 (dependant on switch 1)" key:@"bool_2" handler:^(BOSwitchTableViewCell *cell) {
cell.visibilityKey = @"bool_1";
cell.visibilityBlock = ^BOOL(id settingValue) {
return [settingValue boolValue];
};
cell.onFooterTitle = @"Switch setting 2 is on";
cell.offFooterTitle = @"Switch setting 2 is off";
}]];
Expand Down

0 comments on commit 5a0a17b

Please sign in to comment.