Skip to content

Commit

Permalink
Add support for optionally customizing the cell accessory type
Browse files Browse the repository at this point in the history
  • Loading branch information
revolter committed Mar 16, 2023
1 parent 22f5482 commit 544945a
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 15 deletions.
7 changes: 6 additions & 1 deletion Classes/GlobalStateExplorers/Globals/FLEXGlobalsEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,20 @@ typedef void (^FLEXNestedGlobalEntriesHandler)(FLEXUserGlobalEntriesContainer *
@interface FLEXGlobalsEntry : NSObject

@property (nonatomic, readonly, nonnull) FLEXGlobalsEntryNameFuture entryNameFuture;
@property (nonatomic, readonly) UITableViewCellAccessoryType cellAccessoryType;
@property (nonatomic, readonly, nullable) FLEXGlobalsEntryViewControllerFuture viewControllerFuture;
@property (nonatomic, readonly, nullable) FLEXGlobalsEntryRowAction rowAction;

+ (instancetype)entryWithEntry:(Class<FLEXGlobalsEntry>)entry row:(FLEXGlobalsRow)row;
+ (instancetype)entryWithEntry:(Class<FLEXGlobalsEntry>)entry
cellAccessoryType:(UITableViewCellAccessoryType)cellAccessoryType
row:(FLEXGlobalsRow)row;

+ (instancetype)entryWithNameFuture:(FLEXGlobalsEntryNameFuture)nameFuture
cellAccessoryType:(UITableViewCellAccessoryType)cellAccessoryType
viewControllerFuture:(FLEXGlobalsEntryViewControllerFuture)viewControllerFuture;

+ (instancetype)entryWithNameFuture:(FLEXGlobalsEntryNameFuture)nameFuture
cellAccessoryType:(UITableViewCellAccessoryType)cellAccessoryType
action:(FLEXGlobalsEntryRowAction)rowSelectedAction;

@end
Expand Down
14 changes: 12 additions & 2 deletions Classes/GlobalStateExplorers/Globals/FLEXGlobalsEntry.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@

@implementation FLEXGlobalsEntry

+ (instancetype)entryWithEntry:(Class<FLEXGlobalsEntry>)cls row:(FLEXGlobalsRow)row {
+ (instancetype)entryWithEntry:(Class<FLEXGlobalsEntry>)cls
cellAccessoryType:(UITableViewCellAccessoryType)cellAccessoryType
row:(FLEXGlobalsRow)row {
BOOL providesVCs = [cls respondsToSelector:@selector(globalsEntryViewController:)];
BOOL providesActions = [cls respondsToSelector:@selector(globalsEntryRowAction:)];
NSParameterAssert(cls);
NSParameterAssert(providesVCs || providesActions);

FLEXGlobalsEntry *entry = [self new];
entry->_entryNameFuture = ^{ return [cls globalsEntryTitle:row]; };
entry->_cellAccessoryType = cellAccessoryType;

if (providesVCs) {
id action = providesActions ? [cls globalsEntryRowAction:row] : nil;
Expand All @@ -34,24 +37,28 @@ + (instancetype)entryWithEntry:(Class<FLEXGlobalsEntry>)cls row:(FLEXGlobalsRow)
}

+ (instancetype)entryWithNameFuture:(FLEXGlobalsEntryNameFuture)nameFuture
cellAccessoryType:(UITableViewCellAccessoryType)cellAccessoryType
viewControllerFuture:(FLEXGlobalsEntryViewControllerFuture)viewControllerFuture {
NSParameterAssert(nameFuture);
NSParameterAssert(viewControllerFuture);

FLEXGlobalsEntry *entry = [self new];
entry->_entryNameFuture = [nameFuture copy];
entry->_cellAccessoryType = cellAccessoryType;
entry->_viewControllerFuture = [viewControllerFuture copy];

return entry;
}

+ (instancetype)entryWithNameFuture:(FLEXGlobalsEntryNameFuture)nameFuture
cellAccessoryType:(UITableViewCellAccessoryType)cellAccessoryType
action:(FLEXGlobalsEntryRowAction)rowSelectedAction {
NSParameterAssert(nameFuture);
NSParameterAssert(rowSelectedAction);

FLEXGlobalsEntry *entry = [self new];
entry->_entryNameFuture = [nameFuture copy];
entry->_cellAccessoryType = cellAccessoryType;
entry->_rowAction = [rowSelectedAction copy];

return entry;
Expand All @@ -77,7 +84,10 @@ @implementation NSObject (FLEXGlobalsEntry)

+ (FLEXGlobalsEntry *)flex_concreteGlobalsEntry:(FLEXGlobalsRow)row {
if ([self conformsToProtocol:@protocol(FLEXGlobalsEntry)]) {
return [FLEXGlobalsEntry entryWithEntry:self row:row];
return [FLEXGlobalsEntry entryWithEntry:self
cellAccessoryType:UITableViewCellAccessoryDisclosureIndicator
row:row
];
}

return nil;
Expand Down
2 changes: 1 addition & 1 deletion Classes/GlobalStateExplorers/Globals/FLEXGlobalsSection.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ - (UIViewController *)viewControllerToPushForRow:(NSInteger)row {
}

- (void)configureCell:(__kindof UITableViewCell *)cell forRow:(NSInteger)row {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.accessoryType = self.rows[row].cellAccessoryType;
cell.textLabel.font = UIFont.flex_defaultTableCellFont;
cell.textLabel.text = self.rows[row].entryNameFuture();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ NS_ASSUME_NONNULL_BEGIN
/// you may want to use __weak references.
- (void)registerGlobalEntryWithName:(NSString *)entryName objectFutureBlock:(id (^)(void))objectFutureBlock;

/// Adds an entry at the top of the list of Global State items.
/// Call this method before this view controller is displayed.
/// @param entryName The string to be displayed in the cell.
/// @param cellAccessoryType The accessory type to be used for the cell.
/// @param objectFutureBlock When you tap on the row, information about the object returned
/// by this block will be displayed. Passing a block that returns an object allows you to display
/// information about an object whose actual pointer may change at runtime (e.g. +currentUser)
/// @note This method must be called from the main thread.
/// The objectFutureBlock will be invoked from the main thread and may return nil.
/// @note The passed block will be copied and retain for the duration of the application,
/// you may want to use __weak references.
- (void)registerGlobalEntryWithName:(NSString *)entryName
cellAccessoryType:(UITableViewCellAccessoryType)cellAccessoryType
objectFutureBlock:(id (^)(void))objectFutureBlock;

/// Adds an entry at the top of the list of Global State items.
/// Call this method before this view controller is displayed.
/// @param entryName The string to be displayed in the cell.
Expand All @@ -38,6 +53,20 @@ NS_ASSUME_NONNULL_BEGIN
- (void)registerGlobalEntryWithName:(NSString *)entryName
viewControllerFutureBlock:(UIViewController * (^)(void))viewControllerFutureBlock;

/// Adds an entry at the top of the list of Global State items.
/// Call this method before this view controller is displayed.
/// @param entryName The string to be displayed in the cell.
/// @param cellAccessoryType The accessory type to be used for the cell.
/// @param viewControllerFutureBlock When you tap on the row, view controller returned
/// by this block will be pushed on the navigation controller stack.
/// @note This method must be called from the main thread.
/// The viewControllerFutureBlock will be invoked from the main thread and may not return nil.
/// @note The passed block will be copied and retain for the duration of the application,
/// you may want to use __weak references as needed.
- (void)registerGlobalEntryWithName:(NSString *)entryName
cellAccessoryType:(UITableViewCellAccessoryType)cellAccessoryType
viewControllerFutureBlock:(UIViewController * (^)(void))viewControllerFutureBlock;

/// Adds an entry at the top of the list of Global State items.
/// @param entryName The string to be displayed in the cell.
/// @param rowSelectedAction When you tap on the row, this block will be invoked
Expand All @@ -48,6 +77,19 @@ NS_ASSUME_NONNULL_BEGIN
/// you may want to use __weak references as needed.
- (void)registerGlobalEntryWithName:(NSString *)entryName action:(FLEXGlobalsEntryRowAction)rowSelectedAction;

/// Adds an entry at the top of the list of Global State items.
/// @param entryName The string to be displayed in the cell.
/// @param cellAccessoryType The accessory type to be used for the cell.
/// @param rowSelectedAction When you tap on the row, this block will be invoked
/// with the host table view view controller. Use it to deselect the row or present an alert.
/// @note This method must be called from the main thread.
/// The rowSelectedAction will be invoked from the main thread.
/// @note The passed block will be copied and retained for the duration of the application,
/// you may want to use __weak references as needed.
- (void)registerGlobalEntryWithName:(NSString *)entryName
cellAccessoryType:(UITableViewCellAccessoryType)cellAccessoryType
action:(FLEXGlobalsEntryRowAction)rowSelectedAction;

/// Adds an entry at the top of the list of Global State items.
/// @param entryName The string to be displayed in the cell.
/// @param nestedEntriesHandler When you tap on the row, this block will be invoked
Expand All @@ -58,6 +100,19 @@ NS_ASSUME_NONNULL_BEGIN
/// you may want to use __weak references as needed.
- (void)registerNestedGlobalEntryWithName:(NSString *)entryName handler:(FLEXNestedGlobalEntriesHandler)nestedEntriesHandler;

/// Adds an entry at the top of the list of Global State items.
/// @param entryName The string to be displayed in the cell.
/// @param cellAccessoryType The accessory type to be used for the cell.
/// @param nestedEntriesHandler When you tap on the row, this block will be invoked
/// with the container object. Use it to register nested entries.
/// @note This method must be called from the main thread.
/// The nestedEntriesHandler will be invoked from the main thread.
/// @note The passed block will be copied and retained for the duration of the application,
/// you may want to use __weak references as needed.
- (void)registerNestedGlobalEntryWithName:(NSString *)entryName
cellAccessoryType:(UITableViewCellAccessoryType)cellAccessoryType
handler:(FLEXNestedGlobalEntriesHandler)nestedEntriesHandler;

/// Removes all registered global entries.
- (void)clearGlobalEntries;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,45 @@ - (instancetype)init {
}

- (void)registerGlobalEntryWithName:(NSString *)entryName objectFutureBlock:(id (^)(void))objectFutureBlock {
[self registerGlobalEntryWithName:entryName
cellAccessoryType:UITableViewCellAccessoryDisclosureIndicator
objectFutureBlock:objectFutureBlock
];
}

- (void)registerGlobalEntryWithName:(NSString *)entryName cellAccessoryType:(UITableViewCellAccessoryType)cellAccessoryType objectFutureBlock:(id (^)(void))objectFutureBlock {
NSParameterAssert(entryName);
NSParameterAssert(objectFutureBlock);
NSAssert(NSThread.isMainThread, @"This method must be called from the main thread.");

entryName = entryName.copy;
FLEXGlobalsEntry *entry = [FLEXGlobalsEntry entryWithNameFuture:^NSString *{
return entryName;
} viewControllerFuture:^UIViewController *{
} cellAccessoryType:cellAccessoryType viewControllerFuture:^UIViewController *{
return [FLEXObjectExplorerFactory explorerViewControllerForObject:objectFutureBlock()];
}];

[self.entries addObject:entry];
}

- (void)registerGlobalEntryWithName:(NSString *)entryName viewControllerFutureBlock:(UIViewController * (^)(void))viewControllerFutureBlock {
[self registerGlobalEntryWithName:entryName
cellAccessoryType:UITableViewCellAccessoryDisclosureIndicator
viewControllerFutureBlock:viewControllerFutureBlock
];
}

- (void)registerGlobalEntryWithName:(NSString *)entryName
cellAccessoryType:(UITableViewCellAccessoryType)cellAccessoryType
viewControllerFutureBlock:(UIViewController * (^)(void))viewControllerFutureBlock {
NSParameterAssert(entryName);
NSParameterAssert(viewControllerFutureBlock);
NSAssert(NSThread.isMainThread, @"This method must be called from the main thread.");

entryName = entryName.copy;
FLEXGlobalsEntry *entry = [FLEXGlobalsEntry entryWithNameFuture:^NSString *{
return entryName;
} viewControllerFuture:^UIViewController *{
} cellAccessoryType:cellAccessoryType viewControllerFuture:^UIViewController *{
UIViewController *viewController = viewControllerFutureBlock();
NSCAssert(viewController, @"'%@' entry returned nil viewController. viewControllerFutureBlock should never return nil.", entryName);
return viewController;
Expand All @@ -60,27 +76,45 @@ - (void)registerGlobalEntryWithName:(NSString *)entryName viewControllerFutureBl
}

- (void)registerGlobalEntryWithName:(NSString *)entryName action:(FLEXGlobalsEntryRowAction)rowSelectedAction {
[self registerGlobalEntryWithName:entryName
cellAccessoryType:UITableViewCellAccessoryDisclosureIndicator
action:rowSelectedAction
];
}

- (void)registerGlobalEntryWithName:(NSString *)entryName
cellAccessoryType:(UITableViewCellAccessoryType)cellAccessoryType
action:(FLEXGlobalsEntryRowAction)rowSelectedAction {
NSParameterAssert(entryName);
NSParameterAssert(rowSelectedAction);
NSAssert(NSThread.isMainThread, @"This method must be called from the main thread.");

entryName = entryName.copy;
FLEXGlobalsEntry *entry = [FLEXGlobalsEntry entryWithNameFuture:^NSString * _Nonnull{
return entryName;
} action:rowSelectedAction];
} cellAccessoryType:cellAccessoryType action:rowSelectedAction];

[self.entries addObject:entry];
}

- (void)registerNestedGlobalEntryWithName:(NSString *)entryName handler:(FLEXNestedGlobalEntriesHandler)nestedEntriesHandler {
[self registerNestedGlobalEntryWithName:entryName
cellAccessoryType:UITableViewCellAccessoryDisclosureIndicator
handler:nestedEntriesHandler
];
}

- (void)registerNestedGlobalEntryWithName:(NSString *)entryName
cellAccessoryType:(UITableViewCellAccessoryType)cellAccessoryType
handler:(FLEXNestedGlobalEntriesHandler)nestedEntriesHandler {
NSParameterAssert(entryName);
NSParameterAssert(nestedEntriesHandler);
NSAssert(NSThread.isMainThread, @"This method must be called from the main thread.");

entryName = entryName.copy;
FLEXGlobalsEntry *entry = [FLEXGlobalsEntry entryWithNameFuture:^NSString * _Nonnull{
return entryName;
} viewControllerFuture:^UIViewController * _Nullable{
} cellAccessoryType:cellAccessoryType viewControllerFuture:^UIViewController * _Nullable{
FLEXUserGlobalEntriesContainer *container = [FLEXUserGlobalEntriesContainer new];
nestedEntriesHandler(container);

Expand Down
14 changes: 7 additions & 7 deletions Example/FLEXample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

// To show off the global entries, register one of each type

FLEXManager.shared.globalEntriesContainer.registerGlobalEntry(withName: "Level 1 - Object") {
FLEXManager.shared.globalEntriesContainer.registerGlobalEntry(withName: "Level 1 - Object", cellAccessoryType: .none) {
return "Level 1 - Object"
}

FLEXManager.shared.globalEntriesContainer.registerGlobalEntry(withName: "Level 1 - View controller") {
FLEXManager.shared.globalEntriesContainer.registerGlobalEntry(withName: "Level 1 - View controller", cellAccessoryType: .none) {
let label = UILabel()
label.text = "Level 1 - View controller"
label.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -48,16 +48,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return controller
}

FLEXManager.shared.globalEntriesContainer.registerGlobalEntry(withName: "Level 1 - Action") { host in
FLEXManager.shared.globalEntriesContainer.registerGlobalEntry(withName: "Level 1 - Action", cellAccessoryType: .none) { host in
FLEXAlert.showQuickAlert("Level 1 - Action", from: host)
}

FLEXManager.shared.globalEntriesContainer.registerNestedGlobalEntry(withName: "Level 1 - Nested") { container in
container.registerGlobalEntry(withName: "Level 2 - Object") {
container.registerGlobalEntry(withName: "Level 2 - Object", cellAccessoryType: .none) {
return "Level 2 - Object"
}

container.registerGlobalEntry(withName: "Level 2 - View controller") {
container.registerGlobalEntry(withName: "Level 2 - View controller", cellAccessoryType: .none) {
let label = UILabel()
label.text = "Level 2 - View controller"
label.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -73,12 +73,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return controller
}

container.registerGlobalEntry(withName: "Level 2 - Action") { host in
container.registerGlobalEntry(withName: "Level 2 - Action", cellAccessoryType: .none) { host in
FLEXAlert.showQuickAlert("Level 2 - Action", from: host)
}

container.registerNestedGlobalEntry(withName: "Level 2 - Nested") { level2Container in
level2Container.registerGlobalEntry(withName: "Level 3 - Action") { host in
level2Container.registerGlobalEntry(withName: "Level 3 - Action", cellAccessoryType: .none) { host in
FLEXAlert.showQuickAlert("Level 3 - Action", from: host)
}
}
Expand Down

0 comments on commit 544945a

Please sign in to comment.