From b51f1b82f5439450a18b8fa3f67bd1efa8e7d1ac Mon Sep 17 00:00:00 2001 From: "Khan, Arshad" Date: Sat, 14 Sep 2019 04:55:40 +0530 Subject: [PATCH] adding action block, which should only call after user interaction, but not at the time of initial loading. Fixing apple crash, where even after deletion, apple not updating number of rows, which is leading to index out of bounds crash. Adding remove list item method which has specific molecule inside. --- MVMCoreUI/Atoms/Views/MVMCoreUISwitch.h | 1 + MVMCoreUI/Atoms/Views/MVMCoreUISwitch.m | 12 ++++++++++++ MVMCoreUI/Templates/MoleculeListTemplate.swift | 17 +++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.h b/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.h index 32e98f2a..256aabfa 100644 --- a/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.h +++ b/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.h @@ -24,6 +24,7 @@ typedef void(^ValueChangeBlock)(void); @property (nonatomic) BOOL shouldTouchToSwitch; @property (nullable, copy, nonatomic) ValueChangeBlock valueChangedBlock; +@property (nullable, copy, nonatomic) ValueChangeBlock actionBlock; + (nonnull instancetype)mvmSwitchDefault; + (nonnull instancetype)mvmSwitchDefaultWithValueChangeBlock:(nullable ValueChangeBlock)block; diff --git a/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.m b/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.m index f994b320..a3bbea81 100644 --- a/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.m +++ b/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.m @@ -169,6 +169,18 @@ const CGFloat SwitchShakeIntensity = 2; } [self setState:[json boolForKey:@"state"] animated:false]; + + self.delegate = delegateObject; + NSDictionary *actionMap = [json dict:@"actionMap"]; + if (actionMap) { + [self addTarget:self action:@selector(addCustomAction) forControlEvents:UIControlEventTouchUpInside]; + } +} + +- (void)addCustomAction { + if (self.actionBlock) { + self.actionBlock(); + } } + (CGFloat)estimatedHeightForRow:(NSDictionary *)json delegateObject:(MVMCoreUIDelegateObject *)delegateObject { diff --git a/MVMCoreUI/Templates/MoleculeListTemplate.swift b/MVMCoreUI/Templates/MoleculeListTemplate.swift index 0270cb63..4fb10ed0 100644 --- a/MVMCoreUI/Templates/MoleculeListTemplate.swift +++ b/MVMCoreUI/Templates/MoleculeListTemplate.swift @@ -149,6 +149,23 @@ open class MoleculeListTemplate: ThreeLayerTableViewController { } } self.tableView?.deleteRows(at: indexPaths, with: animation) + // crash fix + self.tableView?.reloadData() + self.updateViewConstraints() + self.view.layoutIfNeeded() + } + + public func removeListItemWhichHas(_ molecule: [AnyHashable : Any], animation: UITableView.RowAnimation) { + var indexPaths: [IndexPath] = [] + if let removeIndex = moleculesInfo?.firstIndex(where: { (moleculeInfo) -> Bool in + return NSDictionary(dictionary: molecule).isEqual(to: moleculeInfo.molecule["molecule"] as? [AnyHashable : Any] ?? [:]) + }) { + moleculesInfo?.remove(at: removeIndex) + indexPaths.append(IndexPath(row: removeIndex + indexPaths.count, section: 0)) + } + self.tableView?.deleteRows(at: indexPaths, with: animation) + // crash fix + self.tableView?.reloadData() self.updateViewConstraints() self.view.layoutIfNeeded() }