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

Side by Side Diff: chrome/browser/ui/views/payments/credit_card_editor_view_controller.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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/views/payments/credit_card_editor_view_controller.h" 5 #include "chrome/browser/ui/views/payments/credit_card_editor_view_controller.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/strings/string16.h" 15 #include "base/strings/string16.h"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "base/time/time.h" 18 #include "base/time/time.h"
19 #include "chrome/browser/ui/autofill/autofill_dialog_models.h"
19 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" 20 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h"
20 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" 21 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h"
21 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" 22 #include "chrome/browser/ui/views/payments/payment_request_views_util.h"
22 #include "chrome/browser/ui/views/payments/preselected_combobox_model.h"
23 #include "chrome/browser/ui/views/payments/validating_combobox.h" 23 #include "chrome/browser/ui/views/payments/validating_combobox.h"
24 #include "chrome/browser/ui/views/payments/validating_textfield.h" 24 #include "chrome/browser/ui/views/payments/validating_textfield.h"
25 #include "chrome/grit/generated_resources.h" 25 #include "chrome/grit/generated_resources.h"
26 #include "components/autofill/core/browser/address_combobox_model.h" 26 #include "components/autofill/core/browser/address_combobox_model.h"
27 #include "components/autofill/core/browser/autofill_data_util.h" 27 #include "components/autofill/core/browser/autofill_data_util.h"
28 #include "components/autofill/core/browser/autofill_type.h" 28 #include "components/autofill/core/browser/autofill_type.h"
29 #include "components/autofill/core/browser/credit_card.h" 29 #include "components/autofill/core/browser/credit_card.h"
30 #include "components/autofill/core/browser/field_types.h" 30 #include "components/autofill/core/browser/field_types.h"
31 #include "components/autofill/core/browser/personal_data_manager.h" 31 #include "components/autofill/core/browser/personal_data_manager.h"
32 #include "components/autofill/core/browser/validation.h" 32 #include "components/autofill/core/browser/validation.h"
(...skipping 10 matching lines...) Expand all
43 #include "ui/views/controls/textfield/textfield.h" 43 #include "ui/views/controls/textfield/textfield.h"
44 #include "ui/views/layout/box_layout.h" 44 #include "ui/views/layout/box_layout.h"
45 #include "ui/views/layout/fill_layout.h" 45 #include "ui/views/layout/fill_layout.h"
46 #include "ui/views/layout/grid_layout.h" 46 #include "ui/views/layout/grid_layout.h"
47 #include "ui/views/view.h" 47 #include "ui/views/view.h"
48 48
49 namespace payments { 49 namespace payments {
50 50
51 namespace { 51 namespace {
52 52
53 // Number of years to be shown in the expiration year dropdown. If the card's
54 // year is outside of the range of
55 // [currentYear, currentYear+kNumberOfExpirationYears], then the UI shows an
56 // additional entry for the card's expiration year.
57 const int kNumberOfExpirationYears = 10;
58
59 // Opacity of card network icons when they are not selected and are displayed 53 // Opacity of card network icons when they are not selected and are displayed
60 // alongside a selected icon. 54 // alongside a selected icon.
61 const float kDimmedCardIconOpacity = 0.33f; 55 const float kDimmedCardIconOpacity = 0.33f;
62 56
63 // This is not quite right but is the closest server type that wasn't already 57 // This is not quite right but is the closest server type that wasn't already
64 // used. 58 // used.
65 const auto kBillingAddressType = autofill::ADDRESS_BILLING_LINE1; 59 const auto kBillingAddressType = autofill::ADDRESS_BILLING_LINE1;
66 60
67 // Returns the items that are in the expiration month dropdown. Will return the
68 // months in order starting at "01" until "12". Uses a clock so that the
69 // |default_index| is set to the current month.
70 std::vector<base::string16> GetExpirationMonthItems(int* default_index) {
71 std::vector<base::string16> months;
72 months.reserve(12);
73 for (int i = 1; i <= 12; i++)
74 months.push_back(base::UTF8ToUTF16(base::StringPrintf("%02d", i)));
75
76 base::Time::Exploded now_exploded;
77 autofill::AutofillClock::Now().LocalExplode(&now_exploded);
78 *default_index = now_exploded.month - 1;
79
80 return months;
81 }
82
83 // Returns the items that are in the expiration year dropdown.
84 std::vector<base::string16> GetExpirationYearItems(
85 const autofill::CreditCard* card_to_edit) {
86 std::vector<base::string16> years;
87 years.reserve(kNumberOfExpirationYears + 1);
88
89 base::Time::Exploded now_exploded;
90 autofill::AutofillClock::Now().LocalExplode(&now_exploded);
91
92 if (card_to_edit && card_to_edit->expiration_year() < now_exploded.year) {
93 years.push_back(
94 base::UTF8ToUTF16(std::to_string(card_to_edit->expiration_year())));
95 }
96
97 for (int i = 0; i < kNumberOfExpirationYears; i++) {
98 years.push_back(base::UTF8ToUTF16(std::to_string(now_exploded.year + i)));
99 }
100
101 if (card_to_edit && card_to_edit->expiration_year() >=
102 now_exploded.year + kNumberOfExpirationYears) {
103 years.push_back(
104 base::UTF8ToUTF16(std::to_string(card_to_edit->expiration_year())));
105 }
106
107 return years;
108 }
109
110 bool IsCardExpired(const base::string16& month, 61 bool IsCardExpired(const base::string16& month,
111 const base::string16& year, 62 const base::string16& year,
112 const std::string& app_locale) { 63 const std::string& app_locale) {
113 autofill::CreditCard card; 64 autofill::CreditCard card;
114 card.SetExpirationMonthFromString(month, app_locale); 65 card.SetExpirationMonthFromString(month, app_locale);
115 card.SetExpirationYearFromString(year); 66 card.SetExpirationYearFromString(year);
116 return card.IsExpired(autofill::AutofillClock::Now()); 67 return card.IsExpired(autofill::AutofillClock::Now());
117 } 68 }
118 69
119 // Validates the two comboboxes used for expiration date. 70 // Validates the two comboboxes used for expiration date.
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
526 return base::MakeUnique< 477 return base::MakeUnique<
527 CreditCardEditorViewController::CreditCardValidationDelegate>(field, 478 CreditCardEditorViewController::CreditCardValidationDelegate>(field,
528 this); 479 this);
529 } 480 }
530 481
531 std::unique_ptr<ui::ComboboxModel> 482 std::unique_ptr<ui::ComboboxModel>
532 CreditCardEditorViewController::GetComboboxModelForType( 483 CreditCardEditorViewController::GetComboboxModelForType(
533 const autofill::ServerFieldType& type) { 484 const autofill::ServerFieldType& type) {
534 switch (type) { 485 switch (type) {
535 case autofill::CREDIT_CARD_EXP_MONTH: { 486 case autofill::CREDIT_CARD_EXP_MONTH: {
536 int default_index = 0; 487 return base::MakeUnique<autofill::MonthComboboxModel>();
537 std::vector<base::string16> months =
538 GetExpirationMonthItems(&default_index);
539 return base::MakeUnique<PreselectedComboboxModel>(months, default_index);
540 } 488 }
541 case autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR: 489 case autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR:
542 return base::MakeUnique<ui::SimpleComboboxModel>( 490 return base::MakeUnique<autofill::YearComboboxModel>(
543 GetExpirationYearItems(credit_card_to_edit_)); 491 credit_card_to_edit_ ? credit_card_to_edit_->expiration_year() : 0);
544 case kBillingAddressType: 492 case kBillingAddressType:
545 // The combobox filled with potential billing addresses. It's fine to pass 493 // The combobox filled with potential billing addresses. It's fine to pass
546 // empty string as the default selected guid if there are no cards being 494 // empty string as the default selected guid if there are no cards being
547 // edited. 495 // edited.
548 return base::MakeUnique<autofill::AddressComboboxModel>( 496 return base::MakeUnique<autofill::AddressComboboxModel>(
549 *state()->GetPersonalDataManager(), state()->GetApplicationLocale(), 497 *state()->GetPersonalDataManager(), state()->GetApplicationLocale(),
550 credit_card_to_edit_ ? credit_card_to_edit_->billing_address_id() 498 credit_card_to_edit_ ? credit_card_to_edit_->billing_address_id()
551 : ""); 499 : "");
552 default: 500 default:
553 NOTREACHED(); 501 NOTREACHED();
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
766 return ValidateValue(combobox->GetTextForRow(combobox->selected_index()), 714 return ValidateValue(combobox->GetTextForRow(combobox->selected_index()),
767 error_message); 715 error_message);
768 } 716 }
769 717
770 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) { 718 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) {
771 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET; 719 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET;
772 return true; 720 return true;
773 } 721 }
774 722
775 } // namespace payments 723 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698