Advertisement
Guest User

Untitled

a guest
Apr 6th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.20 KB | None | 0 0
  1. //
  2. // Created by Sergey Kovalenko on 9/8/16.
  3. // Copyright (c) 2016 Techery. All rights reserved.
  4. //
  5.  
  6. #import "SCSmartCardSettingsViewModel.h"
  7. #import "SCSwitcherRowController.h"
  8. #import "SCFirmwareUpdateRowController.h"
  9. #import "SCAlertCommandRowController.h"
  10. #import "SCSettingsDataSource.h"
  11. #import "SCApplicationDomainProtocol.h"
  12. #import "SCSmartCardsStore.h"
  13. #import "SCTimeIntervalSetting.h"
  14. #import "SCSmartCardUserSetting.h"
  15. #import "SCOperationEvent.h"
  16. #import "SCSettingsSectionInfo.h"
  17. #import "SCSmartCardCharacteristicsServiceProtocol.h"
  18. #import "SCSmartCardSecurityServiceProtocol.h"
  19. #import "SCSmartCardConnectionServiceProtocol.h"
  20. #import "SCSmartCardUserServiceProtocol.h"
  21. #import "SCFirmwareServiceProtocol.h"
  22. #import "SCViewModelAlertFactory.h"
  23. #import <Core/DTSignal.h>
  24. #import <SmartCardSDKIsolation/SmartCardSDKIsolation.h>
  25. #import "RACSignal+SCOperationEvent.h"
  26. #import "SCWalletSettingsAnalyticsHelper.h"
  27.  
  28. NSErrorDomain const SCSettingsViewModelErrorDomain = @"SCSettingsViewModelErrorDomain";
  29.  
  30. @interface SCSmartCardSettingsViewModel ()
  31.  
  32. @property (nonatomic, strong) id<SCSmartCardCharacteristicsServiceProtocol> ioc_smartcardCharacteristicsService;
  33. @property (nonatomic, strong) id<SCSmartCardSecurityServiceProtocol> ioc_smartCardSecurityService;
  34. @property (nonatomic, strong) id<SCSmartCardConnectionServiceProtocol> ioc_smartCardConnectionService;
  35. @property (nonatomic, strong) id<SCFirmwareServiceProtocol> ioc_firmwareService;
  36. @property (nonatomic, strong) id<SCSmartCardUserServiceProtocol> ioc_smartCardUserService;
  37. @property (nonatomic, strong) id<SCWalletSettingsAnalyticsHelperProtocol> ioc_analyticsHelper;
  38.  
  39. @property (nonatomic, copy, readwrite) NSString *firmwareVersionTitle;
  40. @property (nonatomic, copy, readwrite) NSString *batteryLevelTitle;
  41.  
  42. @property (nonatomic, strong) SCSmartCardUserSetting *settings;
  43. @property (nonatomic, strong) RACSubject *operationEventsSubject;
  44. @property (nonatomic, strong) RACSubject *alertsSubject;
  45. @property (nonatomic, assign) BOOL smartCardConnected;
  46.  
  47. @property (nonatomic, strong) SCSettingsRouter *router;
  48. @property (nonatomic, strong, readwrite) SCSettingsViewModel *settingsViewModel;
  49.  
  50. @end
  51.  
  52. @implementation SCSmartCardSettingsViewModel
  53.  
  54. - (instancetype)initWithRouter:(SCSettingsRouter *)router {
  55. DTParameterAssertNotNil(router);
  56. self = [super init];
  57. if (self) {
  58. self.router = router;
  59. self.settingsViewModel = [[SCSettingsViewModel alloc] init];
  60. self.settingsViewModel.title = DTBundleLocalizedString(@"settings.list.title");
  61. self.operationEventsSubject = [RACReplaySubject replaySubjectWithCapacity:1];
  62. self.alertsSubject = [RACReplaySubject replaySubjectWithCapacity:1];
  63. [self synchronizeSettings];
  64. [self setupSettingsDataSource];
  65.  
  66. RAC(self, smartCardConnected) = [[self.ioc_smartCardConnectionService smartCardIsConnectedState] deliverOnMainThread];
  67. @weakify(self);
  68. RAC(self, firmwareVersionTitle) = [[[self.ioc_firmwareService currentFirmwareVersion] deliverOnMainThread] map:^id(SCFirmwareVersionsBundle *version) {
  69. @strongify(self);
  70. return [NSString stringWithFormat:@"%@ %@", DTBundleLocalizedString(@"settings.card_profile.fimware_version"), version.appNordicVersion];
  71. }];
  72. RAC(self, batteryLevelTitle) = [[[self.ioc_smartcardCharacteristicsService batteryLevelStatus] deliverOnMainThread] map:^id(NSNumber *value) {
  73. @strongify(self);
  74. CGFloat percentage = value.doubleValue * 100;
  75. return [NSString stringWithFormat:@"%@ %.0f\%%",DTBundleLocalizedString(@"settings.card_profile.battery_level"),percentage];
  76. }];
  77. }
  78. return self;
  79. }
  80.  
  81. - (void)synchronizeSettings {
  82. @weakify(self);
  83. RACSignal *settingsSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
  84. @strongify(self);
  85. NSMutableArray *settingsSignals = [NSMutableArray array];
  86. [settingsSignals addObject:[self.ioc_smartcardCharacteristicsService lockDeviceStatus]];
  87. [settingsSignals addObject:[self.ioc_smartcardCharacteristicsService resetDefaultCardPeriod]];
  88. [settingsSignals addObject:[self.ioc_smartcardCharacteristicsService clearSmartCardPeriod]];
  89. [settingsSignals addObject:[self.ioc_smartcardCharacteristicsService stealthModeStatusSubscribe:YES]];
  90.  
  91. return [[[RACSignal combineLatest:settingsSignals] map:^id(RACTuple *settingsTuple) {
  92. RACTupleUnpack(NSNumber *locked, NSNumber *resetDefaultCardInterval, NSNumber *resetClearSmartCardPeriod, NSNumber *stealthModeEnabled) = settingsTuple;
  93. SCSmartCardUserSetting *settings = [[SCSmartCardUserSetting alloc] init];
  94. settings.stealthModeEnabled = stealthModeEnabled.boolValue;
  95. settings.locked = locked.boolValue;
  96. settings.disableDefaultPaymentInterval = [SCTimeIntervalSetting settingWithTimeInterval:resetDefaultCardInterval.doubleValue];
  97. settings.clearPaymentCardsInterval = [SCTimeIntervalSetting settingWithTimeInterval:resetClearSmartCardPeriod.doubleValue];
  98. return settings;
  99. }] subscribe:subscriber];
  100. }];
  101.  
  102. RACSignal *settingsWhileConnectedSignal = [[[[[self.ioc_smartCardConnectionService smartCardIsConnectedState] filter:^BOOL(NSNumber *status) {
  103. return status.boolValue;
  104. }] deliverOnMainThread] flattenMap:^RACStream *(id value) {
  105. @strongify(self);
  106. [self.operationEventsSubject sendNext:[SCOperationEvent eventWithLoadingMessage:nil]];
  107. return [[[settingsSignal deliverOnMainThread] doNext:^(id x) {
  108. @strongify(self);
  109. [self.operationEventsSubject sendNext:[SCOperationEvent eventWithCompleteMessage:@""]];
  110. }] doError:^(NSError *error) {
  111. @strongify(self);
  112. [self.operationEventsSubject sendNext:[SCOperationEvent eventWithError:error]];
  113. }];
  114. }] takeUntil:self.rac_willDeallocSignal];
  115.  
  116. RAC(self, settings) = [[settingsWhileConnectedSignal catchTo:[RACSignal empty]] deliverOnMainThread];
  117. }
  118.  
  119. - (void)setupSettingsDataSource {
  120. [self createSmartCardProfileSection];
  121. [self createGeneralSection];
  122. [self createSecuritySection];
  123. [self createFirmwareUpdateSection];
  124. [self createSetupSection];
  125. [self createRestartDeviceSection];
  126. }
  127.  
  128. - (void)createSmartCardProfileSection {
  129. SCCommandRowController *smartCardProfileRowController = [self smartCardProfileRowController];
  130. SCSettingsSectionInfo *section = [[SCSettingsSectionInfo alloc] initWithRowControllers:@[smartCardProfileRowController]];
  131. section.heightForHeader = 0.0f;
  132. [self.settingsViewModel.dataSource addSection:section];
  133. }
  134.  
  135. - (void)createGeneralSection {
  136. NSArray *settings = @[
  137. [self about],
  138. [self offlineMode],
  139. [self locateSmartcard]
  140. ];
  141. SCSettingsSectionInfo *section = [[SCSettingsSectionInfo alloc] initWithRowControllers:settings];
  142. section.headerTitle = DTBundleLocalizedString(@"settings.general");
  143. section.heightForHeader = 38.0f;
  144. section.headerTitleFontSize = 13.0;
  145.  
  146. [self.settingsViewModel.dataSource addSection:section];
  147. }
  148.  
  149. - (void)createSecuritySection {
  150. NSMutableArray *settings = [NSMutableArray array];
  151. [settings addObject:[self lock]];
  152. [settings addObject:[self disableDefaultPayment]];
  153. [settings addObject:[self resetPin]];
  154. [settings addObject:[self autoClearPaymentInfo]];
  155. [settings addObject:[self stealthMode]];
  156.  
  157. if(settings.count > 0) {
  158. SCSettingsSectionInfo *section = [[SCSettingsSectionInfo alloc] initWithRowControllers:settings];
  159. section.headerTitle = DTBundleLocalizedString(@"settings.section.security");
  160. section.heightForHeader = 38.0f;
  161. section.headerTitleFontSize = 13.0;
  162.  
  163. [self.settingsViewModel.dataSource addSection:section];
  164. }
  165. }
  166.  
  167. - (void)createFirmwareUpdateSection {
  168. SCFirmwareUpdateRowController *firmwareUpdateSetting = [[SCFirmwareUpdateRowController alloc] initWithRouter:self.router.firmwareUpdateRouter];
  169. firmwareUpdateSetting.subtitleText = DTBundleLocalizedString(@"settings.firmware_update.detail_text");
  170. SCSettingsSectionInfo *section = [[SCSettingsSectionInfo alloc] initWithRowControllers:@[firmwareUpdateSetting]];
  171. section.headerTitle = DTBundleLocalizedString(@"settings.section.update");
  172. section.heightForHeader = 38.0f;
  173. section.headerTitleFontSize = 13.0;
  174.  
  175. [self.settingsViewModel.dataSource addSection:section];
  176. }
  177.  
  178. - (void)createSetupSection {
  179. NSMutableArray *settings = [NSMutableArray array];
  180.  
  181. [settings addObject:[self setupNewSmartCard]];
  182. [settings addObject:[self factoryReset]];
  183.  
  184. if(settings.count > 0) {
  185. SCSettingsSectionInfo *section = [[SCSettingsSectionInfo alloc] initWithRowControllers:settings];
  186. section.headerTitle = DTBundleLocalizedString(@"settings.section.setup");
  187. section.heightForHeader = 38.0f;
  188. section.headerTitleFontSize = 13.0;
  189. [self.settingsViewModel.dataSource addSection:section];
  190. }
  191. }
  192.  
  193. #pragma mark - Row controllers - SmartCard profile
  194.  
  195. - (SCCommandRowController *)smartCardProfileRowController {
  196. @weakify(self);
  197. SCCommandRowController *smartCardProfileRowController = [[SCCommandRowController alloc] init];
  198. smartCardProfileRowController.title = DTBundleLocalizedString(@"settings.smartcard_profile");
  199. smartCardProfileRowController.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  200. smartCardProfileRowController.executeCommandOnSelection = YES;
  201. smartCardProfileRowController.command = [DTCommand withSignalBlock:^RACSignal *(id input) {
  202. @strongify(self);
  203. return [self showConnectionLostAlertOrExecuteBlockSignal:^{
  204. @strongify(self);
  205. [self.router showSmartCardProfile];
  206. }];
  207. }];
  208. return smartCardProfileRowController;
  209. }
  210.  
  211. #pragma mark - Row controllers - Performance
  212.  
  213. - (void)createRestartDeviceSection {
  214. SCSettingsSectionInfo *section = [[SCSettingsSectionInfo alloc] initWithRowControllers:@[[self restartDevice]]];
  215. [self.settingsViewModel.dataSource addSection:section];
  216. }
  217.  
  218. #pragma mark - Row controllers - General
  219.  
  220. - (SCCommandRowController *)about {
  221. SCCommandRowController *aboutSetting = [[SCCommandRowController alloc] initWithStyle:UITableViewCellStyleDefault];
  222. aboutSetting.title = DTBundleLocalizedString(@"settings.about");
  223. aboutSetting.executeCommandOnSelection = YES;
  224. aboutSetting.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  225. @weakify(self);
  226. aboutSetting.command = [DTCommand withBlock:^(id input) {
  227. @strongify(self);
  228. [self.router showAboutScreen];
  229. }];
  230. return aboutSetting;
  231. }
  232.  
  233. - (SCCommandRowController *)offlineMode {
  234. SCCommandRowController *offlineModeSetting = [[SCCommandRowController alloc] initWithStyle:UITableViewCellStyleDefault];
  235. offlineModeSetting.title = DTBundleLocalizedString(@"offline_mode.section.title");
  236. offlineModeSetting.subtitleText = DTBundleLocalizedString(@"offline_mode.section.subtitle");
  237. offlineModeSetting.executeCommandOnSelection = YES;
  238. offlineModeSetting.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  239. @weakify(self);
  240. offlineModeSetting.command = [DTCommand withBlock:^(id input) {
  241. @strongify(self);
  242. [self.router showOfflineModeScreen];
  243. }];
  244. return offlineModeSetting;
  245. }
  246.  
  247. - (SCCommandRowController *)locateSmartcard {
  248. SCCommandRowController *locateSmartcardSetting = [[SCCommandRowController alloc] init];
  249. locateSmartcardSetting.title = DTBundleLocalizedString(@"settings.locate_smartcard");
  250. locateSmartcardSetting.subtitleText = DTBundleLocalizedString(@"settings.locate_smartcard.detail_text");
  251. locateSmartcardSetting.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  252.  
  253. @weakify(self);
  254. locateSmartcardSetting.command = [DTCommand withBlock:^(id input) {
  255. @strongify(self);
  256. [self.router showLocateSmartCard];
  257. }];
  258. locateSmartcardSetting.executeCommandOnSelection = YES;
  259.  
  260. return locateSmartcardSetting;
  261. }
  262.  
  263. #pragma mark - Row controllers - Security
  264.  
  265. - (SCCommandRowController *)lock {
  266. SCSwitcherRowController *lockSetting = [[SCSwitcherRowController alloc] init];
  267. lockSetting.title = DTBundleLocalizedString(@"settings.lock.card");
  268. lockSetting.subtitleText = DTBundleLocalizedString(@"settings.lock.card.detailText");
  269. RAC(lockSetting, switcher.on, @NO) = RACObserve(self, settings.locked);
  270. @weakify(self);
  271. lockSetting.command = [DTCommand withSignalBlock:^RACSignal *(NSNumber *locked) {
  272. @strongify(self);
  273. DTParameterAssertClass(locked, NSNumber);
  274. [self.ioc_analyticsHelper trackWalletSettingsLockStatusChanging:locked.boolValue];
  275. if (locked.boolValue) {
  276. return [self.ioc_smartCardSecurityService lockDevice];
  277. } else {
  278. return [self.ioc_smartCardSecurityService unlockDevice];
  279. }
  280. }];
  281. [[lockSetting.command.errors map:^id(NSError *error) {
  282. return [SCOperationEvent eventWithError:error];
  283. }] subscribe:self.operationEventsSubject];
  284. return lockSetting;
  285. }
  286.  
  287. - (SCCommandRowController *)disableDefaultPayment {
  288. SCCommandRowController *disableDefaultPaymentSetting = [[SCCommandRowController alloc] initWithStyle:UITableViewCellStyleValue1];
  289. disableDefaultPaymentSetting.title = DTBundleLocalizedString(@"settings.disable.default.card.title");
  290. disableDefaultPaymentSetting.executeCommandOnSelection = YES;
  291. disableDefaultPaymentSetting.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  292.  
  293. RAC(disableDefaultPaymentSetting, detailText) = RACObserve(self, settings.disableDefaultPaymentInterval.intervalDescription);
  294. @weakify(self);
  295. disableDefaultPaymentSetting.command = [DTCommand withBlock:^(id input) {
  296. @strongify(self);
  297. [self.router showResetDefaultCardSettingsWithSettings:self.settings];
  298. }];
  299. return disableDefaultPaymentSetting;
  300. }
  301.  
  302. - (SCCommandRowController *)resetPin {
  303. SCCommandRowController *resetPinSetting = [[SCCommandRowController alloc] init];
  304. resetPinSetting.title = DTBundleLocalizedString(@"settings.reset.pin");
  305. resetPinSetting.executeCommandOnSelection = YES;
  306. resetPinSetting.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  307. @weakify(self);
  308. resetPinSetting.command = [DTCommand withSignalBlock:^RACSignal *(id input) {
  309. @strongify(self);
  310. return [self showConnectionLostAlertOrExecuteBlockSignal:^{
  311. @strongify(self);
  312. SCSmartCard *smartCard = [self.ioc_smartCardConnectionService smartCard];
  313. [self.router showSetTouchPin:smartCard];
  314. }];
  315. }];
  316. return resetPinSetting;
  317. }
  318.  
  319. - (SCCommandRowController *)autoClearPaymentInfo {
  320. SCCommandRowController *clearPaymentInfoSetting = [[SCCommandRowController alloc] initWithStyle:UITableViewCellStyleValue1];
  321. clearPaymentInfoSetting.title = DTBundleLocalizedString(@"settings.clear.smart.card.title");
  322. clearPaymentInfoSetting.subtitleText = DTBundleLocalizedString(@"settings.clear.smartcard.detailText");
  323. clearPaymentInfoSetting.executeCommandOnSelection = YES;
  324. clearPaymentInfoSetting.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  325. RAC(clearPaymentInfoSetting, detailText) = RACObserve(self, settings.clearPaymentCardsInterval.intervalDescription);
  326. @weakify(self);
  327. clearPaymentInfoSetting.command = [DTCommand withBlock:^(id input) {
  328. @strongify(self);
  329. [self.router showClearCardSettingsWithSettings:self.settings];
  330. }];
  331. return clearPaymentInfoSetting;
  332. }
  333.  
  334. - (SCCommandRowController *)stealthMode {
  335. @weakify(self);
  336. SCSwitcherRowController *stealthModeSetting = [[SCSwitcherRowController alloc] init];
  337. stealthModeSetting.title = DTBundleLocalizedString(@"settings.stealth.mode");
  338. stealthModeSetting.subtitleText = DTBundleLocalizedString(@"settings.slealth.mode.detailText");
  339. stealthModeSetting.switcher.enabled = YES;
  340. RAC(stealthModeSetting, switcher.on, @NO) = RACObserve(self, settings.stealthModeEnabled);
  341. stealthModeSetting.command = [DTCommand withSignalBlock:^RACSignal *(NSNumber *input) {
  342. @strongify(self);
  343. DTParameterAssertClass(input, NSNumber);
  344. return [[self.ioc_smartcardCharacteristicsService setStealthModeEnabled:input.boolValue] doNext:^(id x) {
  345. @strongify(self);
  346. [self.ioc_analyticsHelper trackWalletSettingsPrivacyChanging:input.boolValue];
  347. self.settings.stealthModeEnabled = input.boolValue;
  348. }];
  349. }];
  350. [[stealthModeSetting.command.errors map:^id(NSError *error) {
  351. return [SCOperationEvent eventWithError:error];
  352. }] subscribe:self.operationEventsSubject];
  353. return stealthModeSetting;
  354. }
  355.  
  356. - (NSError *)unableToUnlockError {
  357. return [NSError errorWithDomain:SCSettingsViewModelErrorDomain
  358. code:SCRootSettingsErrorUnlockDenied
  359. userInfo:@{NSLocalizedDescriptionKey: DTBundleLocalizedString(@"settings.list.unlock_smart_card_denied")}];
  360. }
  361.  
  362. - (NSError *)timeoutError {
  363. return [NSError errorWithDomain:SCSettingsViewModelErrorDomain
  364. code:SCRootSettingsErrorTimeout
  365. userInfo:@{NSLocalizedDescriptionKey: DTBundleLocalizedString(@"connection.smartcard.error")}];
  366. }
  367.  
  368. - (NSError *)smartCardDisconnectedError {
  369. return [NSError errorWithDomain:SCSettingsViewModelErrorDomain
  370. code:SCRootSettingsSmartCardDisconnected
  371. userInfo:@{NSLocalizedDescriptionKey: DTBundleLocalizedString(@"connection.describing.disconnected")}];
  372. }
  373.  
  374. #pragma mark - Row controllers - Setup
  375.  
  376. - (SCCommandRowController *)factoryReset {
  377. SCCommandRowController *factoryResetSetting = [[SCCommandRowController alloc] initWithStyle:UITableViewCellStyleDefault];
  378. factoryResetSetting.title = DTBundleLocalizedString(@"settings.factory_reset.title");
  379. factoryResetSetting.executeCommandOnSelection = YES;
  380. factoryResetSetting.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  381. factoryResetSetting.subtitleText = DTBundleLocalizedString(@"settings.factory_reset.detail_title");
  382. @weakify(self);
  383. factoryResetSetting.command = [DTCommand withSignalBlock:^RACSignal *(id input) {
  384. @strongify(self);
  385. return [self showConnectionLostAlertOrExecuteBlockSignal:^{
  386. @strongify(self);
  387. [self sendConfirmResetSettingSmartCard];
  388. }];
  389. }];
  390. return factoryResetSetting;
  391. }
  392.  
  393. - (SCCommandRowController *)setupNewSmartCard {
  394. SCCommandRowController *setupNewSmartCardSetting = [[SCCommandRowController alloc] initWithStyle:UITableViewCellStyleDefault];
  395. setupNewSmartCardSetting.title = DTBundleLocalizedString(@"settings.factory_reset.setup_new_smart_card");
  396. setupNewSmartCardSetting.executeCommandOnSelection = YES;
  397. setupNewSmartCardSetting.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  398.  
  399. @weakify(self);
  400. setupNewSmartCardSetting.command = [DTCommand withBlock:^(id x) {
  401. @strongify(self);
  402. [self.router showSetupNewSmartCardWithConnectedState:self.smartCardConnected];
  403. }];
  404.  
  405. return setupNewSmartCardSetting;
  406. }
  407.  
  408. #pragma mark - Row controllers - Restart Device
  409.  
  410. - (SCCommandRowController *)restartDevice {
  411. SCCommandRowController *restartDeviceSetting = [[SCCommandRowController alloc] initWithStyle:UITableViewCellStyleDefault];
  412. restartDeviceSetting.title = DTBundleLocalizedString(@"settings.reset_device.title");
  413. restartDeviceSetting.executeCommandOnSelection = YES;
  414. @weakify(self);
  415. restartDeviceSetting.command = [DTCommand withSignalBlock:^RACSignal *(id input) {
  416. @strongify(self);
  417. return [self showConnectionLostAlertOrExecuteBlockSignal:^{
  418. @strongify(self);
  419. [self sendConfirmRestartingSmartCard];
  420. }];
  421. }];
  422. return restartDeviceSetting;
  423. }
  424.  
  425. - (void)restartSmartCard {
  426. [[[self.ioc_smartCardSecurityService restartDevice] sc_operationEventSignal] subscribe:self.operationEventsSubject];
  427. }
  428.  
  429. - (SCAlertCommandRowController *)unpairSmartCard {
  430. SCAlertCommandRowController *restartSmartCard = [[SCAlertCommandRowController alloc] init];
  431. restartSmartCard.cellTitleColor = [UIColor redColor];
  432. restartSmartCard.title = DTBundleLocalizedString(@"settings.unpair_smartcard");
  433. return restartSmartCard;
  434. }
  435.  
  436. - (RACSignal *)smartCardConnectedConditionSignal {
  437. return [[self.ioc_smartCardConnectionService smartCardIsConnectedState] take:1];
  438. }
  439.  
  440. - (RACSignal *)showConnectionLostAlertOrExecuteBlockSignal:(void (^)())block {
  441. return [RACSignal if:[self smartCardConnectedConditionSignal] then:[DTSignal createSignalWithBlock:block] else:[self sendConnectionLostAlertSignal]];
  442. }
  443.  
  444. #pragma mark - Alerts
  445.  
  446. - (RACSignal *)sendConnectionLostAlertSignal {
  447. @weakify(self);
  448. return [DTSignal createSignalWithBlock:^{
  449. @strongify(self);
  450. [self sendConnectionLostAlert];
  451. }];
  452. }
  453.  
  454. - (void)sendConnectionLostAlert {
  455. [self.alertsSubject sendNext:[SCViewModelAlertFactory noSmartCardConnectionAlert]];
  456. }
  457.  
  458. - (void)sendConfirmRestartingSmartCard {
  459. SCViewModelAlert *alert = [[SCViewModelAlert alloc] initWithTitle:DTBundleLocalizedString(@"settings.reset_device.title") message:DTBundleLocalizedString(@"settings.reset_device.alert.message")];
  460. alert.style = SCViewModelAlertStyleActionSheet;
  461.  
  462. @weakify(self);
  463. [alert addAction:[SCViewModelAlertAction okActionWithCallback:^{
  464. @strongify(self);
  465. [self.ioc_analyticsHelper trackWalletSettingsRestartSmartcard];
  466. [self restartSmartCard];
  467. }]];
  468.  
  469. SCViewModelAlertAction *cancelAction = [SCViewModelAlertAction cancelActionWithCallback:nil];
  470. cancelAction.style = SCViewModelAlertActionStyleCancel;
  471. [alert addAction:cancelAction];
  472.  
  473. [self.alertsSubject sendNext:alert];
  474. }
  475.  
  476. - (void)sendConfirmResetSettingSmartCard {
  477. SCViewModelAlert *alert = [[SCViewModelAlert alloc] initWithTitle:nil message:DTBundleLocalizedString(@"settings.reset_smartcard.alert.message")];
  478.  
  479. [alert addAction:[SCViewModelAlertAction cancelActionWithCallback:nil]];
  480.  
  481. @weakify(self);
  482. [alert addAction:[SCViewModelAlertAction continueActionWithCallback:^{
  483. @strongify(self);
  484. [self.router showFactoryResetScreen];
  485. }]];
  486.  
  487. [self.alertsSubject sendNext:alert];
  488. }
  489.  
  490. #pragma mark - SCOperationEventProducer
  491.  
  492. - (RACSignal *)operationEventsSignal {
  493. return [self.operationEventsSubject map:^SCOperationEvent *(SCOperationEvent *operationEvent) {
  494. if ([operationEvent.error.domain isEqualToString:SCSmartCardFrameworkErrorDomain] &&
  495. operationEvent.error.code == SCSmartCardFrameworkErrorNoActiveConnection) {
  496. return [SCOperationEvent eventWithCompleteMessage:nil];
  497. }
  498. return operationEvent;
  499. }];
  500. }
  501.  
  502. #pragma mark - SCViewModelAlertProducer
  503.  
  504. - (RACSignal *)viewModelAlertsStream {
  505. return [self.alertsSubject deliverOnMainThread];
  506. }
  507.  
  508. #pragma mark - SCAnalyticsTrackingProtocol
  509.  
  510. - (void)trackScreenOpened {
  511. [self.ioc_analyticsHelper trackWalletSettingsScreen];
  512. }
  513.  
  514.  
  515. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement