Day78 of #100DaysOfCode
Hii folks 🙌
Today I will be continuing the same pathway in which we will update pickup and summary fragments to viewModel.
Unit 3: Navigation
Pathway 3: Advanced navigation app Examples
https://developer.android.com/courses/android-basics-kotlin/course
Use the Create pickup options list
- Date formatter
The Android framework provides a class called
SimpleDateFormat, which is a class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date → text) and parsing (text → date) of dates.
We can create an instance of SimpleDateFormat by passing in a pattern string and a locale:
SimpleDateFormat("E MMM d", Locale.getDefault())
A pattern string like "E MMM d" is a representation of Date and Time formats.
A
Localeobject represents a specific geographical, political, or cultural region. It represents a language/country/variant combination. Locales are used to alter the presentation of information such as numbers or dates to suit the conventions in the region.
Locale in Android is a combination of language and country code. The language codes are two-letter lowercase ISO language codes, such as “en” for english. The country codes are two-letter uppercase ISO country codes, such as “US” for the United States.
Now we can use SimpleDateFormat and Locale to determine the available pickup dates for the Cupcake app.
- In
OrderViewModelclass, we will add the following function calledgetPickupOptions()to create and return the list of pickup dates. Within the method, create avalvariable calledoptionsand initialize it tomutableListOf<String>().
private fun getPickupOptions(): List<String> {
val options = mutableListOf<String>()
}- Create a formatter string using
SimpleDateFormatpassing pattern string"E MMM d", and the locale. In the pattern string,Estands for day name in week and it parses to "Tue Dec 10".
val formatter = SimpleDateFormat("E MMM d", Locale.getDefault())Import java.text.SimpleDateFormat and java.util.Locale, when prompted by Android Studio.
- Get a
Calendarinstance and assign it to a new variable. Make it aval. This variable will contain the current date and time. Also, importjava.util.Calendar.
val calendar = Calendar.getInstance()- Build up a list of dates starting with the current date and the following three dates. Because we’ll need 4 date options, repeat this block of code 4 times. This
repeatblock will format a date, add it to the list of date options, and then increment the calendar by 1 day.
repeat(4) {
options.add(formatter.format(calendar.time))
calendar.add(Calendar.DATE, 1)
}- Return the updated
optionsat the end of the method. Here is our completed method:
private fun getPickupOptions(): List<String> {
val options = mutableListOf<String>()
val formatter = SimpleDateFormat("E MMM d", Locale.getDefault())
val calendar = Calendar.getInstance()
// Create a list of dates starting with the current date and the following 3 dates
repeat(4) {
options.add(formatter.format(calendar.time))
calendar.add(Calendar.DATE, 1)
}
return options
}- In
OrderViewModelclass, add a class property calleddateOptionsthat's aval.We will initialize it using thegetPickupOptions()method we just created.
val dateOptions = getPickupOptions()That is all for Day78 ✅
Thanks for reading, See you tomorrow!
