feat: Show the calendar for current month

This commit is contained in:
Peter Vacho 2024-12-24 16:48:57 +01:00
parent bcebaede91
commit 40ca0eaa13
Signed by: school
GPG key ID: 8CFC3837052871B4
5 changed files with 106 additions and 15 deletions

View file

@ -50,4 +50,5 @@ dependencies {
implementation("com.squareup.retrofit2:retrofit:2.11.0")
implementation("com.squareup.retrofit2:converter-gson:2.11.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.1")
implementation("com.jakewharton.threetenabp:threetenabp:1.4.4")
}

View file

@ -4,6 +4,7 @@ import android.app.Application
import com.p_vacho.neat_calendar.api.RetrofitClient
import com.p_vacho.neat_calendar.util.auth.AuthRepository
import com.p_vacho.neat_calendar.util.auth.TokenManager
import com.jakewharton.threetenabp.AndroidThreeTen
class MyApplication : Application() {
lateinit var tokenManager: TokenManager
@ -15,6 +16,9 @@ class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
// Initialize the timezone information
AndroidThreeTen.init(this)
// Initialize RetrofitClient (mutating the global object)
RetrofitClient.initialize(this)

View file

@ -1,13 +1,27 @@
package com.p_vacho.neat_calendar.activities
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.p_vacho.neat_calendar.R
import org.threeten.bp.LocalDate
import org.threeten.bp.YearMonth
import org.threeten.bp.format.TextStyle
import java.time.temporal.WeekFields
import java.util.Locale
class CalendarActivity : AppCompatActivity() {
private lateinit var tvMonthYear: TextView
private lateinit var rvCalendar: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
@ -18,10 +32,60 @@ class CalendarActivity : AppCompatActivity() {
insets
}
initializeCalendar()
tvMonthYear = findViewById(R.id.tvMonthYear)
rvCalendar = findViewById(R.id.rvCalendar)
val currentMonth = YearMonth.now()
setCalendarForMonth(currentMonth)
}
private fun initializeCalendar() {
// TODO("Not yet implemented")
private fun setCalendarForMonth(yearMonth: YearMonth) {
val firstDayOfMonth = yearMonth.atDay(1)
val daysInMonth = yearMonth.lengthOfMonth()
// Get localized month name and year
val monthYearText = "${yearMonth.month.getDisplayName(TextStyle.FULL, Locale.getDefault())} ${yearMonth.year}"
tvMonthYear.text = monthYearText
// Calculate the day of the week the month starts on (Monday as first day)
val dayOfWeek = firstDayOfMonth.dayOfWeek.value // 1 (Monday) to 7 (Sunday)
val startOffset = (dayOfWeek - 1 + 7) % 7 // Shift Sunday (7) to the correct position for Monday start
// Generate calendar days
val days = mutableListOf<String>()
// Add padding for days of the previous month
for (i in 1..startOffset) {
days.add("") // Empty strings represent padding days
}
// Add the actual days of the current month
for (i in 1..daysInMonth) {
days.add(i.toString())
}
// Set up RecyclerView
rvCalendar.layoutManager = GridLayoutManager(this, 7) // 7 columns for days of the week
rvCalendar.adapter = CalendarAdapter(days)
}
}
}
class CalendarAdapter(private val days: List<String>) :
RecyclerView.Adapter<CalendarAdapter.DayViewHolder>() {
class DayViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tvDay: TextView = itemView.findViewById(android.R.id.text1)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DayViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(android.R.layout.simple_list_item_1, parent, false)
return DayViewHolder(view)
}
override fun onBindViewHolder(holder: DayViewHolder, position: Int) {
holder.tvDay.text = days[position]
}
override fun getItemCount(): Int = days.size
}

View file

@ -8,12 +8,28 @@
tools:context=".activities.CalendarActivity">
<TextView
android:layout_width="wrap_content"
android:id="@+id/tvMonthYear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:gravity="center"
android:padding="16dp"
android:text=""
tools:text="January 2024"
android:textAppearance="?attr/textAppearanceHeadline6"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintEnd_toEndOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvCalendar"
android:layout_width="0dp"
android:layout_height="0dp"
android:padding="8dp"
app:layout_constraintTop_toBottomOf="@id/tvMonthYear"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:scrollbars="none"
tools:listitem="@layout/item_calendar_day"
tools:itemCount="6"/>
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tvDay"
android:layout_width="40dp"
android:layout_height="40dp"
android:gravity="center"
android:textColor="?attr/colorOnBackground"
android:background="?attr/selectableItemBackground"
tools:text="1"
android:textAppearance="?attr/textAppearanceBody1" />