Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MWMTTSSettingsViewController.mm « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ffc965b1d6a3c8e70a47ab13fe64a2e67d72c593 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#import "LinkCell.h"
#import "MWMTextToSpeech.h"
#import "MWMTTSSettingsViewController.h"
#import "SelectableCell.h"
#import "Statistics.h"
#import "UIColor+MapsMeColor.h"
#import "WebViewController.h"
#import <AVFoundation/AVFoundation.h>

#include "LocaleTranslator.h"

static NSString * kSelectTTSLanguageSegueName = @"TTSLanguage";

using namespace locale_translator;

@interface MWMTTSSettingsViewController ()
{
  pair<string, string> _additionalTTSLanguage;
  vector<pair<string, string>> _languages;
}

@property (nonatomic) BOOL isLocaleLanguageAbsent;

@end

@implementation MWMTTSSettingsViewController

- (void)viewDidLoad
{
  [super viewDidLoad];
  self.title = L(@"pref_tts_language_title");
  self.tableView.separatorColor = [UIColor blackDividers];
  MWMTextToSpeech * tts = [MWMTextToSpeech tts];

  _languages.reserve(3);
  auto const & v = tts.availableLanguages;
  NSAssert(!v.empty(), @"Vector can't be empty!");
  pair<string, string> const standart = v.front();
  _languages.push_back(standart);

  using namespace tts;
  NSString * currentBcp47 = [AVSpeechSynthesisVoice currentLanguageCode];
  string const currentBcp47Str = [currentBcp47 UTF8String];
  string const currentTwineStr = bcp47ToTwineLanguage(currentBcp47);
  if (currentBcp47Str != standart.first && !currentBcp47Str.empty())
  {
    string const translated = translatedTwine(currentTwineStr);
    pair<string, string> const cur {currentBcp47Str, translated};
    if (translated.empty() || find(v.begin(), v.end(), cur) != v.end())
      _languages.push_back(cur);
    else
      self.isLocaleLanguageAbsent = YES;
  }
  string const savedLanguage = tts.savedLanguage.UTF8String;
  if (savedLanguage != currentBcp47Str && savedLanguage != standart.first && !savedLanguage.empty())
    _languages.emplace_back(make_pair(savedLanguage, translatedTwine(bcp47ToTwineLanguage(tts.savedLanguage))));
}

- (IBAction)unwind:(id)sender
{
  size_t const size = _languages.size();
  if (find(_languages.begin(), _languages.end(), _additionalTTSLanguage) != _languages.end())
  {
    [self.tableView reloadData];
    return;
  }
  switch (size)
  {
    case 1:
      _languages.push_back(_additionalTTSLanguage);
      break;
    case 2:
      if (self.isLocaleLanguageAbsent)
        _languages[size - 1] = _additionalTTSLanguage;
      else
        _languages.push_back(_additionalTTSLanguage);
      break;
    case 3:
      _languages[size - 1] = _additionalTTSLanguage;
      break;
    default:
      NSAssert(false, @"Incorrect language's count");
      break;
  }
  [self.tableView reloadData];
}

- (void)setAdditionalTTSLanguage:(pair<string, string> const &)l
{
  [[MWMTextToSpeech tts] setNotificationsLocale:@(l.first.c_str())];
  _additionalTTSLanguage = l;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  if (section == 0)
    return _languages.size() + 1;
  else
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (indexPath.section == 0 && indexPath.row != _languages.size())
  {
    SelectableCell * cell = (SelectableCell *)[tableView dequeueReusableCellWithIdentifier:[SelectableCell className]];
    pair<string, string> const p = _languages[indexPath.row];
    cell.titleLabel.text = @(p.second.c_str());
    BOOL const isSelected = [@(p.first.c_str()) isEqualToString:[[MWMTextToSpeech tts] savedLanguage]];
    cell.accessoryType = isSelected ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    return cell;
  }
  else
  {
    LinkCell * cell = (LinkCell *)[tableView dequeueReusableCellWithIdentifier:[LinkCell className]];
    cell.titleLabel.text = indexPath.section == 0 ? L(@"pref_tts_other_section_title") : L(@"pref_tts_how_to_set_up_voice");
    return cell;
  }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (indexPath.section == 0)
  {
    if (indexPath.row == _languages.size())
    {
      [Statistics logEvent:kStatEventName(kStatTTSSettings, kStatChangeLanguage)
                       withParameters:@{kStatValue : kStatOther}];
      [self performSegueWithIdentifier:kSelectTTSLanguageSegueName sender:nil];
    }
    else
    {
      [[MWMTextToSpeech tts] setNotificationsLocale:@(_languages[indexPath.row].first.c_str())];
      [tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
    }
  }
  else if (indexPath.section == 1)
  {
    [Statistics logEvent:kStatEventName(kStatTTSSettings, kStatHelp)];
    NSString * path = [[NSBundle mainBundle] pathForResource:@"tts-how-to-set-up-voice" ofType:@"html"];
    NSString * html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSURL * baseURL = [NSURL fileURLWithPath:path];
    WebViewController * vc = [[WebViewController alloc] initWithHtml:html baseUrl:baseURL andTitleOrNil:L(@"pref_tts_how_to_set_up_voice")];
    [self.navigationController pushViewController:vc animated:YES];
  }
}

@end