Skip to content

Commit

Permalink
CalendarAdapter.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
ArjunGupta08 committed Apr 5, 2024
1 parent ada42e8 commit 37faeea
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 202 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

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

6 changes: 4 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ plugins {

android {
namespace 'com.arjun.horizontalcalendardate'
compileSdk 33
compileSdk 34

defaultConfig {
applicationId "com.arjun.horizontalcalendardate"
minSdk 24
targetSdk 33
targetSdk 34
versionCode 1
versionName "1.0"

Expand Down Expand Up @@ -41,4 +41,6 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

implementation project(path:':horizontal_calendar_date')
}

This file was deleted.

This file was deleted.

80 changes: 6 additions & 74 deletions app/src/main/java/com/arjun/horizontalcalendardate/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,102 +4,34 @@ import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
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.*
import kotlin.collections.ArrayList
import com.arjungupta08.horizontal_calendar_date.HorizontalCalendarAdapter
import com.arjungupta08.horizontal_calendar_date.HorizontalCalendarSetUp

class MainActivity : AppCompatActivity(), CalendarAdapter.onItemClickListener {
class MainActivity : AppCompatActivity(), HorizontalCalendarAdapter.OnItemClickListener {

private lateinit var recyclerView: RecyclerView
private lateinit var tvDateMonth: TextView
private lateinit var ivCalendarNext: ImageView
private lateinit var ivCalendarPrevious: ImageView

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>()


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)


tvDateMonth = findViewById(R.id.text_date_month)
recyclerView = findViewById(R.id.recyclerView)
ivCalendarNext = findViewById(R.id.iv_calendar_next)
ivCalendarPrevious = findViewById(R.id.iv_calendar_previous)
val calendarSetUp = CalendarSetUp()

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

calendarSetUp.setUpCalendarPrevNextClickListener(ivCalendarNext, ivCalendarPrevious, this@MainActivity)
}

override fun onItemClick(text: String, date: String, day: String) {
findViewById<TextView>(R.id.selectedDate).text = "Selected date: $text"
findViewById<TextView>(R.id.selectedDD).text = "Selected DD: $date"
findViewById<TextView>(R.id.selectedDay).text = "Selected day: $day"
}
/**
* Set up click listener
*/

private fun setUpClickListener() {
ivCalendarNext.setOnClickListener {
cal.add(Calendar.MONTH, 1)
setUpCalendar()
}
ivCalendarPrevious.setOnClickListener {
cal.add(Calendar.MONTH, -1)
if (cal == currentDate)
setUpCalendar()
else
setUpCalendar()
}
}


/**
* Setting up adapter for recyclerview
*/
private fun setUpAdapter() {
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(this@MainActivity)
}
recyclerView.adapter = adapter
}

/**
* Function to setup calendar for every month
*/
private fun setUpCalendar() {
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(this@MainActivity)
adapter.setData(calendarList)
}

}
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.4.0' apply false
id 'com.android.library' version '7.4.0' apply false
id 'org.jetbrains.kotlin.android' version '1.7.21' apply false
id("com.android.application") version "8.2.2" apply false
id("org.jetbrains.kotlin.android") version "1.9.22" apply false
}
6 changes: 3 additions & 3 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Mar 09 10:11:29 IST 2023
#Fri Apr 05 00:04:11 IST 2024
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.arjungupta08.horizontal_calendar_date
import java.text.SimpleDateFormat
import java.util.*

data class CalendarDateModel(var data: Date, var isSelected: Boolean = true) {
data class CalendarDateModel(var data: Date, var isSelected: Boolean = false) {

val calendarDay: String
get() = SimpleDateFormat("EE", Locale.getDefault()).format(data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import kotlin.collections.ArrayList

class CalendarAdapter(private val listener: (calendarDateModel: CalendarDateModel, position: Int) -> Unit):
RecyclerView.Adapter<CalendarAdapter.CalendarViewHolder>(){
class HorizontalCalendarAdapter(private val listener: (calendarDateModel: CalendarDateModel, position: Int) -> Unit):
RecyclerView.Adapter<HorizontalCalendarAdapter.CalendarViewHolder>(){

private var list = ArrayList<CalendarDateModel>()
private var adapterPosition = -1
Expand Down Expand Up @@ -49,8 +49,10 @@ class CalendarAdapter(private val listener: (calendarDateModel: CalendarDateMode
val date = itemList.calendarDate
val day = itemList.calendarDay
mListener?.onItemClick(text,date,day)
listener.invoke(itemList, position)
}
if (position == adapterPosition){

if (itemList.isSelected){
holder.calendarDay.setTextColor(ContextCompat.getColor(holder.itemView.context, R.color.white))
holder.calendarDate.setTextColor(ContextCompat.getColor(holder.itemView.context, R.color.white))
holder.linear.setBackgroundResource(R.drawable.rectangle_fill)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.arjun.horizontalcalendardate
package com.arjungupta08.horizontal_calendar_date

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
Expand All @@ -12,19 +9,19 @@ import java.util.Calendar
import java.util.Date
import java.util.Locale

class CalendarSetUp() {
class HorizontalCalendarSetUp() {

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 lateinit var adapter: HorizontalCalendarAdapter
private val calendarList2 = ArrayList<CalendarDateModel>()

/*
* Set up click listener
*/
fun setUpCalendarClickListener(ivCalendarNext: ImageView, ivCalendarPrevious: ImageView, listener: CalendarAdapter.onItemClickListener) {
fun setUpCalendarPrevNextClickListener(ivCalendarNext: ImageView, ivCalendarPrevious: ImageView, listener: HorizontalCalendarAdapter.OnItemClickListener) {
ivCalendarNext.setOnClickListener {
cal.add(Calendar.MONTH, 1)
setUpCalendar(listener)
Expand All @@ -42,16 +39,17 @@ class CalendarSetUp() {
/*
* Setting up adapter for recyclerview
*/
fun setUpCalendarAdapter(recyclerView: RecyclerView, listener : CalendarAdapter.onItemClickListener) {
fun setUpCalendarAdapter(recyclerView: RecyclerView, listener : HorizontalCalendarAdapter.OnItemClickListener) {
val snapHelper: SnapHelper = LinearSnapHelper()
snapHelper.attachToRecyclerView(recyclerView)
adapter = CalendarAdapter { calendarDateModel: CalendarDateModel, position: Int ->

adapter = HorizontalCalendarAdapter() { calendarDateModel: CalendarDateModel, position: Int ->
calendarList2.forEachIndexed { index, calendarModel ->
calendarModel.isSelected = index == position
}
adapter.setData(calendarList2)
adapter.setOnItemClickListener(listener)
}
adapter.setData(calendarList2)
adapter.setOnItemClickListener(listener)
recyclerView.adapter = adapter

setUpCalendar(listener)
Expand All @@ -60,9 +58,8 @@ class CalendarSetUp() {
/*
* Function to setup calendar for every month
*/
private fun setUpCalendar(listener: CalendarAdapter.onItemClickListener) {
private fun setUpCalendar(listener: HorizontalCalendarAdapter.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()
Expand All @@ -76,6 +73,7 @@ class CalendarSetUp() {
calendarList2.addAll(calendarList)
adapter.setOnItemClickListener(listener)
adapter.setData(calendarList)
// tvDateMonth.text = sdf.format(cal.time)
}

}

0 comments on commit 37faeea

Please sign in to comment.