From e4e635ff925bae40bfd7a632d93a73c9ce8f142c Mon Sep 17 00:00:00 2001 From: Gabriel Wong Date: Thu, 14 Sep 2017 15:49:26 +0700 Subject: [PATCH] Migrate LanguageDetailViewController to Swift --- .../KeymanEngine.xcodeproj/project.pbxproj | 18 +- .../LanguageDetailViewController.h | 22 -- .../LanguageDetailViewController.m | 287 ------------------ .../LanguageDetailViewController.swift | 173 +++++++++++ .../LanguagePicker/LanguageViewController.m | 4 +- 5 files changed, 179 insertions(+), 325 deletions(-) delete mode 100644 ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageDetailViewController.h delete mode 100644 ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageDetailViewController.m create mode 100644 ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageDetailViewController.swift diff --git a/ios/engine/KMEI/KeymanEngine.xcodeproj/project.pbxproj b/ios/engine/KMEI/KeymanEngine.xcodeproj/project.pbxproj index 268d8381f7..f853dedb94 100644 --- a/ios/engine/KMEI/KeymanEngine.xcodeproj/project.pbxproj +++ b/ios/engine/KMEI/KeymanEngine.xcodeproj/project.pbxproj @@ -68,17 +68,12 @@ 988B36B91ADF728A0008752C /* inuktitut_latin-1.0.js in Resources */ = {isa = PBXBuildFile; fileRef = 988B36B71ADF728A0008752C /* inuktitut_latin-1.0.js */; }; 988B36BA1ADF728A0008752C /* inuktitut_naqittaut-1.0.js in Resources */ = {isa = PBXBuildFile; fileRef = 988B36B81ADF728A0008752C /* inuktitut_naqittaut-1.0.js */; }; 989FDF92193C1C7100EECAF9 /* KMKeyPreviewView.m in Sources */ = {isa = PBXBuildFile; fileRef = 989FDF91193C1C7100EECAF9 /* KMKeyPreviewView.m */; }; - 98ACCE0917A9EF0700B7C780 /* LanguageDetailViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 98C0B11617A204BF007ECAFC /* LanguageDetailViewController.m */; }; 98ACCE0A17A9EF1D00B7C780 /* LanguageViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 98C0B11317A20491007ECAFC /* LanguageViewController.m */; }; - 98ACCE0D17A9EF8C00B7C780 /* LanguageDetailViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = 98C0B11517A204BF007ECAFC /* LanguageDetailViewController.h */; }; 98ACCE0E17A9EF9700B7C780 /* LanguageViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = 98C0B11217A20490007ECAFC /* LanguageViewController.h */; }; - 98ACCE1117A9EFCE00B7C780 /* LanguageDetailViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 98C0B11617A204BF007ECAFC /* LanguageDetailViewController.m */; }; 98ACCE1217A9EFE100B7C780 /* LanguageViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 98C0B11317A20491007ECAFC /* LanguageViewController.m */; }; - 98ACCE1517A9F01300B7C780 /* LanguageDetailViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = 98C0B11517A204BF007ECAFC /* LanguageDetailViewController.h */; }; 98ACCE1617A9F02300B7C780 /* LanguageViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = 98C0B11217A20490007ECAFC /* LanguageViewController.h */; }; 98B34AB917F3B82000DF079E /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 98B34AB817F3B82000DF079E /* AudioToolbox.framework */; }; 98C0B11417A20493007ECAFC /* LanguageViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 98C0B11317A20491007ECAFC /* LanguageViewController.m */; }; - 98C0B11717A204BF007ECAFC /* LanguageDetailViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 98C0B11617A204BF007ECAFC /* LanguageDetailViewController.m */; }; 98D4190C17695E58008D2FF3 /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 98D4190B17695E58008D2FF3 /* Default-568h@2x.png */; }; 98EF3BA619A5B03800A1365F /* KMSubKeysView.m in Sources */ = {isa = PBXBuildFile; fileRef = 9841130A199D9B84005BD609 /* KMSubKeysView.m */; }; 98EF3BA719A5B05000A1365F /* KMSubKeysView.h in Headers */ = {isa = PBXBuildFile; fileRef = 98411309199D9B84005BD609 /* KMSubKeysView.h */; }; @@ -106,6 +101,7 @@ C08C62131F67C31100268D03 /* KeyboardNameTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C08C62121F67C31100268D03 /* KeyboardNameTableViewCell.swift */; }; C08C62151F67C8D500268D03 /* KeyboardInfoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C08C62141F67C8D500268D03 /* KeyboardInfoViewController.swift */; }; C08C62171F67CFB800268D03 /* KeyboardPickerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C08C62161F67CFB800268D03 /* KeyboardPickerViewController.swift */; }; + C092D8391F6A70C8005C5485 /* LanguageDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C092D8381F6A70C8005C5485 /* LanguageDetailViewController.swift */; }; C0A5FF371F6682EB00BE740C /* PopoverView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0A5FF361F6682EB00BE740C /* PopoverView.swift */; }; CE25CCBD1F1DA7BB005AA2BC /* KMHTTPDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = CE25CCBC1F1DA7BB005AA2BC /* KMHTTPDownloader.m */; }; CE25CCBE1F1DA7BB005AA2BC /* KMHTTPDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = CE25CCBC1F1DA7BB005AA2BC /* KMHTTPDownloader.m */; }; @@ -229,8 +225,6 @@ 98B34AB817F3B82000DF079E /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = System/Library/Frameworks/AudioToolbox.framework; sourceTree = SDKROOT; }; 98C0B11217A20490007ECAFC /* LanguageViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = LanguageViewController.h; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; 98C0B11317A20491007ECAFC /* LanguageViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = LanguageViewController.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; - 98C0B11517A204BF007ECAFC /* LanguageDetailViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = LanguageDetailViewController.h; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; - 98C0B11617A204BF007ECAFC /* LanguageDetailViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = LanguageDetailViewController.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; 98D4190B17695E58008D2FF3 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = ""; }; 98F9D6971954112F0087AA43 /* Tavultesoft.KMEI.SystemKeyboard.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = Tavultesoft.KMEI.SystemKeyboard.appex; sourceTree = BUILT_PRODUCTS_DIR; }; 98F9D69A1954112F0087AA43 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -245,6 +239,7 @@ C08C62121F67C31100268D03 /* KeyboardNameTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyboardNameTableViewCell.swift; sourceTree = ""; }; C08C62141F67C8D500268D03 /* KeyboardInfoViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyboardInfoViewController.swift; sourceTree = ""; }; C08C62161F67CFB800268D03 /* KeyboardPickerViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyboardPickerViewController.swift; sourceTree = ""; }; + C092D8381F6A70C8005C5485 /* LanguageDetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LanguageDetailViewController.swift; sourceTree = ""; }; C0A5FF351F6682EA00BE740C /* KMEI-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "KMEI-Bridging-Header.h"; sourceTree = ""; }; C0A5FF361F6682EB00BE740C /* PopoverView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PopoverView.swift; sourceTree = ""; }; CE25CCBC1F1DA7BB005AA2BC /* KMHTTPDownloader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KMHTTPDownloader.m; sourceTree = ""; }; @@ -587,8 +582,7 @@ children = ( C08C62141F67C8D500268D03 /* KeyboardInfoViewController.swift */, C08C62161F67CFB800268D03 /* KeyboardPickerViewController.swift */, - 98C0B11517A204BF007ECAFC /* LanguageDetailViewController.h */, - 98C0B11617A204BF007ECAFC /* LanguageDetailViewController.m */, + C092D8381F6A70C8005C5485 /* LanguageDetailViewController.swift */, 98C0B11217A20490007ECAFC /* LanguageViewController.h */, 98C0B11317A20491007ECAFC /* LanguageViewController.m */, 98C0B11817A22DB5007ECAFC /* PickerButtons */, @@ -729,7 +723,6 @@ 980CC8E519D522060089BB57 /* KMURLProtocol.h in Headers */, 987F3EA31941ABF600209D4F /* KMKeyPreviewView.h in Headers */, 98ACCE0E17A9EF9700B7C780 /* LanguageViewController.h in Headers */, - 98ACCE0D17A9EF8C00B7C780 /* LanguageDetailViewController.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -752,7 +745,6 @@ 987F3EA51941AC2700209D4F /* KMKeyPreviewView.h in Headers */, 98ACCE1617A9F02300B7C780 /* LanguageViewController.h in Headers */, 980CC8E819D5223C0089BB57 /* KMURLProtocol.h in Headers */, - 98ACCE1517A9F01300B7C780 /* LanguageDetailViewController.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -981,6 +973,7 @@ F243888F14BBD43100A3E055 /* main.m in Sources */, 6C0A1407151EA8B3007FA4AD /* KMTextView.m in Sources */, 6C0A140C151EA925007FA4AD /* AppDelegate.m in Sources */, + C092D8391F6A70C8005C5485 /* LanguageDetailViewController.swift in Sources */, 6C0A140D151EA925007FA4AD /* MainViewController.m in Sources */, 6CBD0DE2151EC30300D70779 /* KMTextViewDelegateProxy.m in Sources */, C08C620C1F6792A200268D03 /* KeyboardPickerBarButtonItem.swift in Sources */, @@ -1004,7 +997,6 @@ C08C62171F67CFB800268D03 /* KeyboardPickerViewController.swift in Sources */, C08C62101F67BD4500268D03 /* KeyboardPickerButton.swift in Sources */, 98C0B11417A20493007ECAFC /* LanguageViewController.m in Sources */, - 98C0B11717A204BF007ECAFC /* LanguageDetailViewController.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1019,7 +1011,6 @@ 987F3E9F1941AAB700209D4F /* KMKeyPreviewView.m in Sources */, 98F9D692195402F90087AA43 /* KMTextView.m in Sources */, 98ACCE0A17A9EF1D00B7C780 /* LanguageViewController.m in Sources */, - 98ACCE0917A9EF0700B7C780 /* LanguageDetailViewController.m in Sources */, F27FCA88157FB13B00FBBA20 /* KMManager.m in Sources */, F27FCA8C157FB13B00FBBA20 /* KMTextViewDelegateProxy.m in Sources */, F27FCA8E157FB13B00FBBA20 /* KMTextField.m in Sources */, @@ -1043,7 +1034,6 @@ 9878F7751B8BF01F00360B8D /* NSString+Helpers.m in Sources */, 987F3EA11941AAE900209D4F /* KMKeyPreviewView.m in Sources */, 98ACCE1217A9EFE100B7C780 /* LanguageViewController.m in Sources */, - 98ACCE1117A9EFCE00B7C780 /* LanguageDetailViewController.m in Sources */, F27FCAC9157FD15900FBBA20 /* KMManager.m in Sources */, F27FCACA157FD15900FBBA20 /* KMTextView.m in Sources */, F27FCACB157FD15900FBBA20 /* KMTextViewDelegateProxy.m in Sources */, diff --git a/ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageDetailViewController.h b/ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageDetailViewController.h deleted file mode 100644 index d6c51998d8..0000000000 --- a/ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageDetailViewController.h +++ /dev/null @@ -1,22 +0,0 @@ -// -// LanguageDetailViewController.h -// Keyman Engine -// -// Copyright (c) 2017 SIL International. All rights reserved. -// - -#import - -@interface LanguageDetailViewController : UITableViewController { - NSInteger languageIndex; - NSString *languageName; - NSString *languageID; - NSArray *keyboards; -} - -@property (nonatomic, assign) NSInteger languageIndex; -@property (nonatomic, strong) NSString *languageName; -@property (nonatomic, strong) NSString *languageID; -@property (nonatomic, strong) NSArray *keyboards; - -@end diff --git a/ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageDetailViewController.m b/ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageDetailViewController.m deleted file mode 100644 index 83160fecaf..0000000000 --- a/ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageDetailViewController.m +++ /dev/null @@ -1,287 +0,0 @@ -// -// LanguageDetailViewController.m -// Keyman Engine -// -// Created by Serkan Kurt on 26/07/13. -// Copyright (c) 2017 SIL International. All rights reserved. -// - -#import "LanguageDetailViewController.h" -#import "KMManager+Internal.h" -#import - -static const NSInteger kmToolbarButtonTag = 100; -static const NSInteger kmToolbarLabelTag = 101; -static const NSInteger kmToolbarActivityIndicatorTag = 102; - -@interface LanguageDetailViewController () - @property (nonatomic, strong) NSMutableDictionary *userKeyboards; - @property (assign) BOOL isUpdate; -@end - -@implementation LanguageDetailViewController - -@synthesize userKeyboards, languageIndex, languageName, languageID, keyboards; - -- (id)initWithStyle:(UITableViewStyle)style -{ - self = [super initWithStyle:style]; - if (self) { - // Custom initialization - } - return self; -} - -- (void)loadView { - [super loadView]; - [self loadUserKeyboards]; -} - -- (void)viewDidLoad -{ - [super viewDidLoad]; - - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDownloadStarted:) name:kKeymanKeyboardDownloadStartedNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDownloadFailed:) name:kKeymanKeyboardDownloadFailedNotification object:nil]; - - // Uncomment the following line to preserve selection between presentations. - // self.clearsSelectionOnViewWillAppear = NO; - - // Uncomment the following line to display an Edit button in the navigation bar for this view controller. - // self.navigationItem.rightBarButtonItem = self.editButtonItem; -} - -- (void)dealloc { - [[NSNotificationCenter defaultCenter] removeObserver:self]; -} - -- (void)didReceiveMemoryWarning -{ - [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. -} - -#pragma mark - Table view data source - -- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView -{ - return [keyboards count]; -} - -- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section -{ - return 1; -} - -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath -{ - static NSString *cellIdentifier = @"Cell"; - UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; - if (!cell) { - cell = [[KeyboardNameTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; - UIView *selectionColor = [[UIView alloc] init]; - [selectionColor setBackgroundColor:[UIColor colorWithRed:204.0/255.0 green:136.0/255.0 blue:34.0/255.0 alpha:1.0]]; - [cell setSelectedBackgroundView:selectionColor]; - } - - return cell; -} - -- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { - //[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; - [(KeyboardNameTableViewCell *)cell setIndexPath:indexPath]; - - cell.textLabel.text = [[keyboards objectAtIndex:indexPath.section] objectForKey:kKeymanNameKey]; - NSString *keyboardID = [[keyboards objectAtIndex:indexPath.section] objectForKey:kKeymanIdKey]; - if ([self isAdded:languageID keyboardID:keyboardID]) { - [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; - cell.userInteractionEnabled = NO; - cell.textLabel.enabled = NO; - cell.detailTextLabel.enabled = NO; - } - else { - [cell setAccessoryType:UITableViewCellAccessoryNone]; - cell.userInteractionEnabled = YES; - cell.textLabel.enabled = YES; - cell.detailTextLabel.enabled = YES; - } - - eKMKeyboardState kbState = [[KMManager sharedInstance] stateForKeyboardWithID:keyboardID]; - [(KeyboardNameTableViewCell *)cell setKeyboardState:kbState selected:NO defaultAccessoryType:cell.accessoryType]; -} - -/* - // Override to support conditional editing of the table view. - - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath - { - // Return NO if you do not want the specified item to be editable. - return YES; - } - */ - -/* - // Override to support editing the table view. - - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath - { - if (editingStyle == UITableViewCellEditingStyleDelete) { - // Delete the row from the data source - [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; - } - else if (editingStyle == UITableViewCellEditingStyleInsert) { - // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view - } - } - */ - -/* - // Override to support rearranging the table view. - - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath - { - } - */ - -/* - // Override to support conditional rearranging of the table view. - - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath - { - // Return NO if you do not want the item to be re-orderable. - return YES; - } - */ - -#pragma mark - Table view delegate - -- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath -{ - [tableView cellForRowAtIndexPath:indexPath].selected = NO; - NSString *kbID = [[keyboards objectAtIndex:indexPath.section] objectForKey:kKeymanIdKey]; - NSString *kbName = [[keyboards objectAtIndex:indexPath.section] objectForKey:kKeymanNameKey]; - //NSString *isRTL = [[keyboards objectAtIndex:indexPath.section] objectForKey:kKeymanKeyboardRTLKey]; - //isRTL = (isRTL == nil)?@"N":@"Y"; - //NSString *font = [[KMManager sharedInstance] jsFontFromFontDictionary:[[keyboards objectAtIndex:indexPath.section] objectForKey:kKeymanFontKey]]; - //NSString *oskFont = [[KMManager sharedInstance] jsFontFromFontDictionary:[[keyboards objectAtIndex:indexPath.section] objectForKey:kKeymanOskFontKey]]; - - eKMKeyboardState state = [[KMManager sharedInstance] stateForKeyboardWithID:kbID]; - if (state != kKMKeyboardStateDownloading) { - if (state == kKMKeyboardStateNeedsDownload) - self.isUpdate = NO; - else - self.isUpdate = YES; - - UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@: %@", languageName, kbName] - message:@"Would you like to download this keyboard?" - delegate:self - cancelButtonTitle:@"Cancel" - otherButtonTitles:@"Download", nil]; - alert.tag = indexPath.section; - [alert show]; - } - /* - if (state == kKMKeyboardStateNeedsDownload) { - UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@: %@", languageName, kbName] - message:@"Would you like to download this keyboard?" - delegate:self - cancelButtonTitle:@"Cancel" - otherButtonTitles:@"Download", nil]; - alert.tag = indexPath.section; - [alert show]; - } - else if (state == kKMKeyboardStateDownloading || state == kKMKeyboardStateNone) { - // Do nothing. - } - else { - // Add keyboard. - [[KMManager sharedInstance] addKeyboardWithID:kbID languageID:languageID keyboardName:kbName languageName:languageName isRTL:[isRTL isEqualToString:@"Y"]?YES:NO isCustom:NO font:font oskFont:oskFont]; - [[KMManager sharedInstance] setKeyboardWithID:kbID languageID:languageID keyboardName:kbName languageName:languageName font:font oskFont:oskFont]; - [self.navigationController popToRootViewControllerAnimated:YES]; - }*/ -} - -#pragma mark - Alert view delegate - -- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { - // Keyboard download confirmation alert (tag is used for keyboard index). - if (buttonIndex != alertView.cancelButtonIndex) { - [[KMManager sharedInstance] downloadKeyboardForLanguageIndex:languageIndex keyboardIndex:alertView.tag isUpdate:self.isUpdate]; - } -} - -#pragma mark - Keyman notifications - -- (void)keyboardDownloadStarted:(NSNotification *)notification { - [self.view setUserInteractionEnabled:NO]; - [self.navigationItem setHidesBackButton:YES animated:YES]; - - CGRect frame = [self.navigationController.toolbar frame]; - CGFloat width = frame.size.width; - CGFloat height = frame.size.height; - frame.size.width = width*0.95; - frame.size.height = height*0.7; - UILabel *label = [[UILabel alloc] initWithFrame:frame]; - [label setBackgroundColor:[UIColor clearColor]]; - [label setTextColor:[UIColor whiteColor]]; - [label setTextAlignment:NSTextAlignmentCenter]; - [label setCenter:CGPointMake(width*0.5, height*0.5)]; - [label setText:@"Downloading\u2026"]; - [label setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; - [label setTag:kmToolbarLabelTag]; - - UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; - frame = [indicatorView frame]; - [indicatorView setCenter:CGPointMake(width - frame.size.width, height*0.5)]; - [indicatorView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; - [indicatorView setTag:kmToolbarActivityIndicatorTag]; - [indicatorView startAnimating]; - - [[self.navigationController.toolbar viewWithTag:kmToolbarButtonTag] removeFromSuperview]; - [[self.navigationController.toolbar viewWithTag:kmToolbarLabelTag] removeFromSuperview]; - [[self.navigationController.toolbar viewWithTag:kmToolbarActivityIndicatorTag] removeFromSuperview]; - [self.navigationController.toolbar addSubview:label]; - [self.navigationController.toolbar addSubview:indicatorView]; - [self.navigationController setToolbarHidden:NO animated:YES]; -} - -- (void)keyboardDownloadFailed:(NSNotification *)notification { - [self.view setUserInteractionEnabled:YES]; - [self.navigationItem setHidesBackButton:NO animated:YES]; -} - -#pragma mark - Private methods - -- (void)loadUserKeyboards { - NSUserDefaults *userData = [[KMManager sharedInstance] activeUserDefaults]; - NSArray *userKbList = [[NSArray alloc] initWithArray:[userData arrayForKey:kKeymanUserKeyboardsListKey]]; - NSUInteger len = [userKbList count]; - - if (len == 0) { - userKeyboards = nil; - return; - } - - NSString *langID = nil; - NSString *kbID = nil; - NSString *dictKey = nil; - userKeyboards = [[NSMutableDictionary alloc] initWithCapacity:len]; - for (NSDictionary *kb in userKbList) { - langID = [kb objectForKey:kKeymanLanguageIdKey]; - kbID = [kb objectForKey:kKeymanKeyboardIdKey]; - dictKey = [NSString stringWithFormat:@"%@_%@", langID, kbID]; - [self.userKeyboards setObject:kb forKey:dictKey]; - } -} - -- (BOOL)isAdded:(NSString *)langID keyboardID:(NSString *)kbID { - if (userKeyboards != nil) { - if([userKeyboards objectForKey:[NSString stringWithFormat:@"%@_%@", langID, kbID]]) { - return YES; - } - else { - return NO; - } - } - else { - return NO; - } -} - -@end diff --git a/ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageDetailViewController.swift b/ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageDetailViewController.swift new file mode 100644 index 0000000000..a5f68b5051 --- /dev/null +++ b/ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageDetailViewController.swift @@ -0,0 +1,173 @@ +// +// LanguageDetailViewController.swift +// KeymanEngine +// +// Created by Gabriel Wong on 2017-09-14. +// Copyright © 2017 SIL International. All rights reserved. +// + +import UIKit + +private let toolbarButtonTag = 100 +private let toolbarLabelTag: Int = 101 +private let toolbarActivityIndicatorTag: Int = 102 + +class LanguageDetailViewController: UITableViewController, UIAlertViewDelegate { + var languageIndex = 0 + var languageName = "" + var languageID = "" + var keyboards: [[String: Any]] = [] + var userKeyboards: [String: [String: Any]] = [:] + var isUpdate = false + + override func loadView() { + super.loadView() + loadUserKeyboards() + } + + override func viewDidLoad() { + super.viewDidLoad() + NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDownloadStarted), + name: NSNotification.Name.keymanKeyboardDownloadStarted, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDownloadFailed), + name: NSNotification.Name.keymanKeyboardDownloadFailed, object: nil) + } + + deinit { + NotificationCenter.default.removeObserver(self) + } + + override func numberOfSections(in tableView: UITableView) -> Int { + return keyboards.count + } + + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return 1 + } + + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cellIdentifier = "Cell" + if let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) { + return cell + } + + let cell = KeyboardNameTableViewCell(style: .subtitle, reuseIdentifier: cellIdentifier) + let selectionColor = UIView() + selectionColor.backgroundColor = UIColor(red: 204.0 / 255.0, green: 136.0 / 255.0, blue: 34.0 / 255.0, alpha: 1.0) + cell.selectedBackgroundView = selectionColor + return cell + } + + override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { + let cell = cell as! KeyboardNameTableViewCell + cell.indexPath = indexPath + cell.textLabel?.text = (keyboards[indexPath.section][kKeymanNameKey] as! String) + let keyboardID = keyboards[indexPath.section][kKeymanIdKey] as! String + if isAdded(languageID: languageID, keyboardID: keyboardID) { + cell.accessoryType = .checkmark + cell.isUserInteractionEnabled = false + cell.textLabel?.isEnabled = false + cell.detailTextLabel?.isEnabled = false + } else { + cell.accessoryType = .none + cell.isUserInteractionEnabled = true + cell.textLabel?.isEnabled = true + cell.detailTextLabel?.isEnabled = true + } + + let kbState = KMManager.sharedInstance().stateForKeyboard(withID: keyboardID) + cell.setKeyboardState(kbState, selected: false, defaultAccessoryType: cell.accessoryType) + } + + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + tableView.cellForRow(at: indexPath)?.isSelected = false + let kbID = keyboards[indexPath.section][kKeymanIdKey] as! String + let kbName = keyboards[indexPath.section][kKeymanNameKey] + + let state = KMManager.sharedInstance().stateForKeyboard(withID: kbID) + if state != kKMKeyboardStateDownloading { + if state == kKMKeyboardStateNeedsDownload { + isUpdate = false + } else { + isUpdate = true + } + + let alert = UIAlertView(title: "\(languageName): \(kbName!)", + message: "Would you like to download this keyboard?", + delegate: self, + cancelButtonTitle: "Cancel", + otherButtonTitles: "Download") + alert.tag = indexPath.section + alert.show() + } + } + + func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) { + // Keyboard download confirmation alert (tag is used for keyboard index). + if buttonIndex != alertView.cancelButtonIndex { + KMManager.sharedInstance().downloadKeyboard(forLanguageIndex: languageIndex, + keyboardIndex: alertView.tag, isUpdate: isUpdate) + } + } + + func keyboardDownloadStarted(_ notification: Notification) { + view.isUserInteractionEnabled = false + navigationItem.setHidesBackButton(true, animated: true) + + let toolbarFrame = navigationController!.toolbar.frame + let labelFrame = CGRect(origin: toolbarFrame.origin, + size: CGSize(width: toolbarFrame.width * 0.95, height: toolbarFrame.height * 0.7)) + let label = UILabel(frame: labelFrame) + label.backgroundColor = UIColor.clear + label.textColor = UIColor.white + label.textAlignment = .center + label.center = CGPoint(x: toolbarFrame.width * 0.5, y: toolbarFrame.height * 0.5) + label.text = "Downloading\u{2026}" + label.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, + .flexibleBottomMargin, .flexibleWidth, .flexibleHeight] + label.tag = toolbarLabelTag + + let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray) + indicatorView.center = CGPoint(x: toolbarFrame.width - indicatorView.frame.width, + y: toolbarFrame.height * 0.5) + indicatorView.autoresizingMask = [.flexibleLeftMargin, .flexibleTopMargin, .flexibleBottomMargin] + indicatorView.tag = toolbarActivityIndicatorTag + indicatorView.startAnimating() + + navigationController?.toolbar.viewWithTag(toolbarButtonTag)?.removeFromSuperview() + navigationController?.toolbar.viewWithTag(toolbarLabelTag)?.removeFromSuperview() + navigationController?.toolbar.viewWithTag(toolbarActivityIndicatorTag)?.removeFromSuperview() + navigationController?.toolbar.addSubview(label) + navigationController?.toolbar.addSubview(indicatorView) + navigationController?.setToolbarHidden(false, animated: true) + } + + func keyboardDownloadFailed(_ notification: Notification) { + view.isUserInteractionEnabled = true + navigationItem.setHidesBackButton(false, animated: true) + } + + func loadUserKeyboards() { + let userData = KMManager.sharedInstance().activeUserDefaults() + guard let userKbList = userData?.array(forKey: kKeymanUserKeyboardsListKey) as? [[String: String]], + !userKbList.isEmpty else { + userKeyboards = [:] + return + } + + userKeyboards = [:] + for kb in userKbList { + let langID = kb[kKeymanLanguageIdKey] + let kbID = kb[kKeymanKeyboardIdKey] + let dictKey = "\(langID!)_\(kbID!)" + userKeyboards[dictKey] = kb + } + } + + func isAdded(languageID: String?, keyboardID: String?) -> Bool { + guard let languageID = languageID, let keyboardID = keyboardID else { + return false + } + return userKeyboards["\(languageID)_\(keyboardID)"] != nil + } +} diff --git a/ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageViewController.m b/ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageViewController.m index 5be4100ffe..2e531597c0 100644 --- a/ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageViewController.m +++ b/ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/LanguageViewController.m @@ -7,7 +7,6 @@ // #import "LanguageViewController.h" -#import "LanguageDetailViewController.h" #import "KMManager+Internal.h" #import "QuartzCore/QuartzCore.h" #import @@ -298,8 +297,9 @@ static const NSInteger kmToolbarActivityIndicatorTag = 102; langDetailView.languageIndex = langIndex; langDetailView.languageName = [[languages objectAtIndex:langIndex] objectForKey:kKeymanNameKey]; langDetailView.languageID = [[languages objectAtIndex:langIndex] objectForKey:kKeymanIdKey]; - langDetailView.keyboards = [[NSArray alloc] initWithArray:[[KMManager sharedInstance] keyboardsForIndex:langIndex]]; + NSArray *keyboards = [[NSArray alloc] initWithArray:[[KMManager sharedInstance] keyboardsForIndex:langIndex]]; + langDetailView.keyboards = keyboards; [self.navigationController pushViewController:langDetailView animated:YES]; }