Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: chrome/browser/ui/autofill/autofill_dialog_models.cc

Issue 2918133002: [Merge M60][Payments] Refactor the year and month combobox models and preselect. (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/autofill/autofill_dialog_models.h" 5 #include "chrome/browser/ui/autofill/autofill_dialog_models.h"
6 6
7 #include "base/bind.h" 7 #include "base/strings/string16.h"
8 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "base/time/time.h" 11 #include "base/time/time.h"
12 #include "chrome/grit/generated_resources.h" 12 #include "chrome/grit/generated_resources.h"
13 #include "components/autofill/core/browser/autofill_country.h" 13 #include "components/autofill/core/common/autofill_clock.h"
14 #include "components/prefs/pref_service.h"
15 #include "ui/base/l10n/l10n_util.h" 14 #include "ui/base/l10n/l10n_util.h"
16 15
17 namespace autofill { 16 namespace autofill {
18 17
18 namespace {
19
20 // Number of years to be shown in the year combobox, including the current year.
21 // YearComboboxModel has the option of passing an additional year if not
22 // contained within the initial range.
23 const int kNumberOfExpirationYears = 10;
24
25 // Returns the items that are in the expiration year dropdown. If
26 // |additional_year| is not 0 and not within the normal range, it will be added
27 // accordingly.
28 std::vector<base::string16> GetExpirationYearItems(int additional_year) {
29 std::vector<base::string16> years;
30 // Add the "Year" placeholder item.
31 years.push_back(
32 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR));
33
34 base::Time::Exploded now_exploded;
35 AutofillClock::Now().LocalExplode(&now_exploded);
36
37 if (additional_year != 0 && additional_year < now_exploded.year)
38 years.push_back(base::UTF8ToUTF16(std::to_string(additional_year)));
39
40 for (int i = 0; i < kNumberOfExpirationYears; i++)
41 years.push_back(base::UTF8ToUTF16(std::to_string(now_exploded.year + i)));
42
43 if (additional_year != 0 &&
44 additional_year >= now_exploded.year + kNumberOfExpirationYears) {
45 years.push_back(base::UTF8ToUTF16(std::to_string(additional_year)));
46 }
47
48 return years;
49 }
50
51 // Formats a month, zero-padded (e.g. "02").
52 base::string16 FormatMonth(int month) {
53 return base::ASCIIToUTF16(base::StringPrintf("%.2d", month));
54 }
55
56 } // namespace
57
19 SuggestionsMenuModelDelegate::~SuggestionsMenuModelDelegate() {} 58 SuggestionsMenuModelDelegate::~SuggestionsMenuModelDelegate() {}
20 59
21 // SuggestionsMenuModel ---------------------------------------------------- 60 // SuggestionsMenuModel ----------------------------------------------------
22 61
23 SuggestionsMenuModel::SuggestionsMenuModel( 62 SuggestionsMenuModel::SuggestionsMenuModel(
24 SuggestionsMenuModelDelegate* delegate) 63 SuggestionsMenuModelDelegate* delegate)
25 : ui::SimpleMenuModel(this), 64 : ui::SimpleMenuModel(this),
26 delegate_(delegate), 65 delegate_(delegate),
27 checked_item_(0) {} 66 checked_item_(0) {}
28 67
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 167
129 MonthComboboxModel::MonthComboboxModel() {} 168 MonthComboboxModel::MonthComboboxModel() {}
130 169
131 MonthComboboxModel::~MonthComboboxModel() {} 170 MonthComboboxModel::~MonthComboboxModel() {}
132 171
133 int MonthComboboxModel::GetItemCount() const { 172 int MonthComboboxModel::GetItemCount() const {
134 // 12 months plus the empty entry. 173 // 12 months plus the empty entry.
135 return 13; 174 return 13;
136 } 175 }
137 176
138 // static
139 base::string16 MonthComboboxModel::FormatMonth(int index) {
140 return base::ASCIIToUTF16(base::StringPrintf("%.2d", index));
141 }
142
143 base::string16 MonthComboboxModel::GetItemAt(int index) { 177 base::string16 MonthComboboxModel::GetItemAt(int index) {
144 return index == 0 ? 178 return index == 0 ?
145 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH) : 179 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH) :
146 FormatMonth(index); 180 FormatMonth(index);
147 } 181 }
148 182
149 // YearComboboxModel ----------------------------------------------------------- 183 // YearComboboxModel -----------------------------------------------------------
150 184
151 YearComboboxModel::YearComboboxModel() : this_year_(0) { 185 YearComboboxModel::YearComboboxModel(int additional_year)
152 base::Time time = base::Time::Now(); 186 : ui::SimpleComboboxModel(GetExpirationYearItems(additional_year)) {}
153 base::Time::Exploded exploded;
154 time.LocalExplode(&exploded);
155 this_year_ = exploded.year;
156 }
157 187
158 YearComboboxModel::~YearComboboxModel() {} 188 YearComboboxModel::~YearComboboxModel() {}
159 189
160 int YearComboboxModel::GetItemCount() const {
161 // 10 years plus the empty entry.
162 return 11;
163 }
164
165 base::string16 YearComboboxModel::GetItemAt(int index) {
166 if (index == 0) {
167 return l10n_util::GetStringUTF16(
168 IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR);
169 }
170
171 return base::IntToString16(this_year_ + index - 1);
172 }
173
174 } // namespace autofill 190 } // namespace autofill
OLDNEW
« no previous file with comments | « chrome/browser/ui/autofill/autofill_dialog_models.h ('k') | chrome/browser/ui/autofill/autofill_dialog_models_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698