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: bc120aee3c0eed2e4b654785733066108473f90f (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#import "MWMTTSSettingsViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "LinkCell.h"
#import "MWMTextToSpeech.h"
#import "SelectableCell.h"
#import "Statistics.h"
#import "UIColor+MapsMeColor.h"
#import "WebViewController.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;
  }

  NSString * nsSavedLanguage = [MWMTextToSpeech savedLanguage];
  if (nsSavedLanguage.length)
  {
    string const savedLanguage = nsSavedLanguage.UTF8String;
    if (savedLanguage != currentBcp47Str && savedLanguage != standart.first &&
        !savedLanguage.empty())
      _languages.emplace_back(
          make_pair(savedLanguage, translatedTwine(bcp47ToTwineLanguage(nsSavedLanguage))));
  }
}

- (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() + 2;
  else
    return 1;
}

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (indexPath.section == 0)
  {
    if (indexPath.row == 0)
    {
      [Statistics logEvent:kStatEventName(kStatSettings, kStatTTS)
            withParameters:@{kStatValue : kStatOff}];
      [MWMTextToSpeech setTTSEnabled:NO];
      [tableView reloadSections:[NSIndexSet indexSetWithIndex:0]
               withRowAnimation:UITableViewRowAnimationFade];
    }
    else
    {
      [Statistics logEvent:kStatEventName(kStatSettings, kStatTTS)
            withParameters:@{kStatValue : kStatOn}];
      [MWMTextToSpeech setTTSEnabled:YES];
      NSInteger const row = indexPath.row - 1;
      if (row == _languages.size())
      {
        [Statistics logEvent:kStatEventName(kStatTTSSettings, kStatChangeLanguage)
              withParameters:@{kStatValue : kStatOther}];
        [self performSegueWithIdentifier:kSelectTTSLanguageSegueName sender:nil];
      }
      else
      {
        [[MWMTextToSpeech tts] setNotificationsLocale:@(_languages[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