[fenix] For https://github.com/mozilla-mobile/fenix/issues/18246 - [Add card] Populat...
[gecko.git] / mobile / android / fenix / app / src / main / java / org / mozilla / fenix / settings / creditcards / CreditCardEditorFragment.kt
blobb8b34bec28963e553b5bff210b73c79b8440f76a
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.fenix.settings.creditcards
7 import android.os.Bundle
8 import android.view.View
9 import android.widget.ArrayAdapter
10 import androidx.fragment.app.Fragment
11 import androidx.navigation.fragment.findNavController
12 import kotlinx.android.synthetic.main.fragment_credit_card_editor.view.*
13 import org.mozilla.fenix.R
14 import org.mozilla.fenix.ext.showToolbar
15 import java.text.SimpleDateFormat
16 import java.util.Calendar
17 import java.util.Locale
19 /**
20  * Display a credit card editor for adding and editing a credit card.
21  */
22 class CreditCardEditorFragment : Fragment(R.layout.fragment_credit_card_editor) {
24     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
25         super.onViewCreated(view, savedInstanceState)
27         showToolbar(getString(R.string.credit_cards_add_card))
29         setupButtonClickListeners(view)
30         setupExpiryMonthDropDown(view)
31     }
33     /**
34      * Setup the all button click listeners in the credit card editor.
35      */
36     private fun setupButtonClickListeners(view: View) {
37         view.cancel_button.setOnClickListener {
38             findNavController().popBackStack()
39         }
40     }
42     /**
43      * Setup the expiry month dropdown by formatting and populating it with the months in a calendar
44      * year.
45      */
46     private fun setupExpiryMonthDropDown(view: View) {
47         val adapter =
48             ArrayAdapter<String>(view.context, android.R.layout.simple_spinner_dropdown_item)
49         val dateFormat = SimpleDateFormat("MMMM (MM)", Locale.getDefault())
51         val calendar = Calendar.getInstance()
52         calendar.set(Calendar.DAY_OF_MONTH, 1)
54         for (month in 0..NUMBER_OF_MONTHS) {
55             calendar.set(Calendar.MONTH, month)
56             adapter.add(dateFormat.format(calendar.time))
57         }
59         view.expiry_month_drop_down.adapter = adapter
60     }
62     companion object {
63         // Number of months in a year (0-indexed).
64         private const val NUMBER_OF_MONTHS = 11
65     }