Skip to content

Commit

Permalink
CalendarSetUp
Browse files Browse the repository at this point in the history
  • Loading branch information
ArjunGupta08 committed Apr 4, 2024
1 parent 67b8659 commit ada42e8
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.arjun.horizontalcalendardate

import android.content.Context
import android.widget.AdapterView.OnItemClickListener
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearSnapHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SnapHelper
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale

class CalendarSetUp() {

private val sdf = SimpleDateFormat("MMMM yyyy", Locale.ENGLISH)
private val cal = Calendar.getInstance(Locale.ENGLISH)
private val currentDate = Calendar.getInstance(Locale.ENGLISH)
private val dates = ArrayList<Date>()
private lateinit var adapter: CalendarAdapter
private val calendarList2 = ArrayList<CalendarDateModel>()

/*
* Set up click listener
*/
fun setUpCalendarClickListener(ivCalendarNext: ImageView, ivCalendarPrevious: ImageView, listener: CalendarAdapter.onItemClickListener) {
ivCalendarNext.setOnClickListener {
cal.add(Calendar.MONTH, 1)
setUpCalendar(listener)
}
ivCalendarPrevious.setOnClickListener {
cal.add(Calendar.MONTH, -1)
if (cal == currentDate)
setUpCalendar(listener)
else
setUpCalendar(listener)
}
}


/*
* Setting up adapter for recyclerview
*/
fun setUpCalendarAdapter(recyclerView: RecyclerView, listener : CalendarAdapter.onItemClickListener) {
val snapHelper: SnapHelper = LinearSnapHelper()
snapHelper.attachToRecyclerView(recyclerView)
adapter = CalendarAdapter { calendarDateModel: CalendarDateModel, position: Int ->
calendarList2.forEachIndexed { index, calendarModel ->
calendarModel.isSelected = index == position
}
adapter.setData(calendarList2)
adapter.setOnItemClickListener(listener)
}
recyclerView.adapter = adapter

setUpCalendar(listener)
}

/*
* Function to setup calendar for every month
*/
private fun setUpCalendar(listener: CalendarAdapter.onItemClickListener) {
val calendarList = ArrayList<CalendarDateModel>()
// tvDateMonth.text = sdf.format(cal.time)
val monthCalendar = cal.clone() as Calendar
val maxDaysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH)
dates.clear()
monthCalendar.set(Calendar.DAY_OF_MONTH, 1)
while (dates.size < maxDaysInMonth) {
dates.add(monthCalendar.time)
calendarList.add(CalendarDateModel(monthCalendar.time))
monthCalendar.add(Calendar.DAY_OF_MONTH, 1)
}
calendarList2.clear()
calendarList2.addAll(calendarList)
adapter.setOnItemClickListener(listener)
adapter.setData(calendarList)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ class MainActivity : AppCompatActivity(), CalendarAdapter.onItemClickListener {
recyclerView = findViewById(R.id.recyclerView)
ivCalendarNext = findViewById(R.id.iv_calendar_next)
ivCalendarPrevious = findViewById(R.id.iv_calendar_previous)
setUpAdapter()
setUpClickListener()
setUpCalendar()
val calendarSetUp = CalendarSetUp()

calendarSetUp.setUpCalendarAdapter(recyclerView, this@MainActivity)
calendarSetUp.setUpCalendarClickListener(ivCalendarNext, ivCalendarPrevious, this@MainActivity)

}
override fun onItemClick(text: String, date: String, day: String) {
findViewById<TextView>(R.id.selectedDate).text = "Selected date: $text"
Expand Down

0 comments on commit ada42e8

Please sign in to comment.