Skip to content

Commit

Permalink
Project import generated by Copybara.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 343096754
Change-Id: I30649bfac49258401a34a1f0d59bee7c2981fcdf
  • Loading branch information
Google ML Kit authored and Dong Chen committed Nov 18, 2020
1 parent d01e076 commit 0f08221
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
2 changes: 2 additions & 0 deletions ios/quickstarts/translate/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use_frameworks!

platform :ios, '10.0'

project 'TranslateExample.xcodeproj'

pod 'GoogleMLKit/Translate'

target 'TranslateExample' do
Expand Down
16 changes: 13 additions & 3 deletions ios/quickstarts/translate/TranslateExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ class ViewController: UIViewController, UITextViewDelegate, UIPickerViewDataSour

func handleDownloadDelete(picker: UIPickerView, button: UIButton) {
let language = allLanguages[picker.selectedRow(inComponent: 0)]
if language == .english {
return
}
button.setTitle("working...", for: .normal)
let model = self.model(forLanguage: language)
let modelManager = ModelManager.modelManager()
Expand Down Expand Up @@ -159,17 +162,22 @@ class ViewController: UIViewController, UITextViewDelegate, UIPickerViewDataSour
else {
return
}
weak var weakSelf = self
DispatchQueue.main.async {
guard let strongSelf = weakSelf else {
print("Self is nil!")
return
}
let languageName = Locale.current.localizedString(
forLanguageCode: remoteModel.language.rawValue)!
if notification.name == .mlkitModelDownloadDidSucceed {
self.statusTextView.text =
strongSelf.statusTextView.text =
"Download succeeded for \(languageName)"
} else {
self.statusTextView.text =
strongSelf.statusTextView.text =
"Download failed for \(languageName)"
}
self.setDownloadDeleteButtonLabels()
strongSelf.setDownloadDeleteButtonLabels()
}
}

Expand All @@ -181,11 +189,13 @@ class ViewController: UIViewController, UITextViewDelegate, UIPickerViewDataSour
} else {
self.sourceDownloadDeleteButton.setTitle("Download model", for: .normal)
}
self.sourceDownloadDeleteButton.isHidden = inputLanguage == .english
if self.isLanguageDownloaded(outputLanguage) {
self.targetDownloadDeleteButton.setTitle("Delete model", for: .normal)
} else {
self.targetDownloadDeleteButton.setTitle("Download model", for: .normal)
}
self.targetDownloadDeleteButton.isHidden = inputLanguage == .english
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
Expand Down
64 changes: 41 additions & 23 deletions ios/quickstarts/translate/TranslateExampleObjC/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
- (nullable NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
return [NSLocale.currentLocale localizedStringForLanguageCode:self.allLanguages[row]];
}

Expand All @@ -104,13 +106,17 @@ - (BOOL)textView:(UITextView *)textView

- (IBAction)didTapSwap {
NSInteger inputSelectedRow = [self.inputPicker selectedRowInComponent:0];
[self.inputPicker selectRow:[self.outputPicker selectedRowInComponent:0] inComponent:0 animated:NO];
[self.inputPicker selectRow:[self.outputPicker selectedRowInComponent:0]
inComponent:0
animated:NO];
[self.outputPicker selectRow:inputSelectedRow inComponent:0 animated:NO];
self.inputTextView.text = self.outputTextView.text;
[self pickerView:self.inputPicker didSelectRow:0 inComponent:0];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
MLKTranslatorOptions *options = [[MLKTranslatorOptions alloc]
initWithSourceLanguage:self.allLanguages[[self.inputPicker selectedRowInComponent:0]]
targetLanguage:self.allLanguages[[self.outputPicker selectedRowInComponent:0]]];
Expand All @@ -121,40 +127,47 @@ - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComp

- (void)translate {
MLKTranslator *translatorForDownload = self.translator;
__weak typeof(self) weakSelf = self;
[self.translator downloadModelIfNeededWithCompletion:^(NSError *_Nullable error) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if (error != nil) {
self.outputTextView.text =
strongSelf.outputTextView.text =
[NSString stringWithFormat:@"Failed to ensure model downloaded with error %@",
error.localizedDescription];
return;
}
[self updateDownloadDeleteButtonLabels];
NSString *text = self.inputTextView.text;
[strongSelf updateDownloadDeleteButtonLabels];
NSString *text = strongSelf.inputTextView.text;
if (text == nil) {
text = @"";
}
self.outputTextView.text = @"";
strongSelf.outputTextView.text = @"";
if (translatorForDownload != self.translator) {
return;
}
[self.translator translateText:text
completion:^(NSString *_Nullable result, NSError *_Nullable error) {
if (error != nil) {
self.outputTextView.text = [NSString
stringWithFormat:@"Failed to ensure model downloaded with error %@",
error.localizedDescription];
return;
}
if (translatorForDownload != self.translator) {
return;
}
self.outputTextView.text = result;
}];
[strongSelf.translator
translateText:text
completion:^(NSString *_Nullable result, NSError *_Nullable error) {
__strong typeof(weakSelf) strongSelf2 = weakSelf;
if (error != nil) {
strongSelf2.outputTextView.text =
[NSString stringWithFormat:@"Failed to ensure model downloaded with error %@",
error.localizedDescription];
return;
}
if (translatorForDownload != strongSelf2.translator) {
return;
}
strongSelf2.outputTextView.text = result;
}];
}];
}

- (void)handleDownloadDeleteWithPicker:(UIPickerView *)picker button:(UIButton *)button {
MLKTranslateLanguage language = self.allLanguages[[picker selectedRowInComponent:0]];
if (language == MLKTranslateLanguageEnglish) {
return;
}
NSString *languageName = [NSLocale.currentLocale localizedStringForLanguageCode:language];

[button setTitle:@"Working..." forState:UIControlStateNormal];
Expand Down Expand Up @@ -189,11 +202,13 @@ - (void)updateDownloadDeleteButtonLabels {
} else {
[self.sourceDownloadDeleteButton setTitle:@"Download Model" forState:UIControlStateNormal];
}
self.sourceDownloadDeleteButton.hidden = inputLanguage == MLKTranslateLanguageEnglish;
if ([self isLanguageDownloaded:outputLanguage]) {
[self.targetDownloadDeleteButton setTitle:@"Delete Model" forState:UIControlStateNormal];
} else {
[self.targetDownloadDeleteButton setTitle:@"Download Model" forState:UIControlStateNormal];
}
self.targetDownloadDeleteButton.hidden = inputLanguage == MLKTranslateLanguageEnglish;
}

- (BOOL)isLanguageDownloaded:(MLKTranslateLanguage)language {
Expand Down Expand Up @@ -235,14 +250,17 @@ - (void)modelDownloadDidCompleteWithNotification:(NSNotification *)notification
}
NSString *languageName = [NSLocale.currentLocale localizedStringForLanguageCode:model.language];

__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if (notification.name == MLKModelDownloadDidSucceedNotification) {
self.statusTextView.text =
strongSelf.statusTextView.text =
[NSString stringWithFormat:@"Download succeeded for %@", languageName];
} else {
self.statusTextView.text = [NSString stringWithFormat:@"Download failed for%@", languageName];
strongSelf.statusTextView.text =
[NSString stringWithFormat:@"Download failed for%@", languageName];
}
[self updateDownloadDeleteButtonLabels];
[strongSelf updateDownloadDeleteButtonLabels];
});
}

Expand Down

0 comments on commit 0f08221

Please sign in to comment.