From df88bd33e2799b04c08d1476e55ef03183f91e4b Mon Sep 17 00:00:00 2001 From: Gabriel Luong Date: Wed, 17 Mar 2021 19:36:28 -0400 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/18246 - [Add card] Populate the Credit card expiry month drop down --- .../creditcards/CreditCardEditorFragment.kt | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/settings/creditcards/CreditCardEditorFragment.kt b/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/settings/creditcards/CreditCardEditorFragment.kt index c2693ce01efa..b8b34bec2896 100644 --- a/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/settings/creditcards/CreditCardEditorFragment.kt +++ b/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/settings/creditcards/CreditCardEditorFragment.kt @@ -6,11 +6,15 @@ package org.mozilla.fenix.settings.creditcards import android.os.Bundle import android.view.View +import android.widget.ArrayAdapter import androidx.fragment.app.Fragment import androidx.navigation.fragment.findNavController import kotlinx.android.synthetic.main.fragment_credit_card_editor.view.* import org.mozilla.fenix.R import org.mozilla.fenix.ext.showToolbar +import java.text.SimpleDateFormat +import java.util.Calendar +import java.util.Locale /** * Display a credit card editor for adding and editing a credit card. @@ -23,6 +27,7 @@ class CreditCardEditorFragment : Fragment(R.layout.fragment_credit_card_editor) showToolbar(getString(R.string.credit_cards_add_card)) setupButtonClickListeners(view) + setupExpiryMonthDropDown(view) } /** @@ -33,4 +38,29 @@ class CreditCardEditorFragment : Fragment(R.layout.fragment_credit_card_editor) findNavController().popBackStack() } } + + /** + * Setup the expiry month dropdown by formatting and populating it with the months in a calendar + * year. + */ + private fun setupExpiryMonthDropDown(view: View) { + val adapter = + ArrayAdapter(view.context, android.R.layout.simple_spinner_dropdown_item) + val dateFormat = SimpleDateFormat("MMMM (MM)", Locale.getDefault()) + + val calendar = Calendar.getInstance() + calendar.set(Calendar.DAY_OF_MONTH, 1) + + for (month in 0..NUMBER_OF_MONTHS) { + calendar.set(Calendar.MONTH, month) + adapter.add(dateFormat.format(calendar.time)) + } + + view.expiry_month_drop_down.adapter = adapter + } + + companion object { + // Number of months in a year (0-indexed). + private const val NUMBER_OF_MONTHS = 11 + } } -- 2.11.4.GIT