Skip to content

Daily calendar #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/.name

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

17 changes: 17 additions & 0 deletions .idea/deploymentTargetDropDown.xml

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

3 changes: 1 addition & 2 deletions .idea/gradle.xml

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

11 changes: 11 additions & 0 deletions .idea/misc.xml

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

5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CalendarAppExample">
<activity
android:name=".DailyCalendarActivity"
android:exported="true" />
<activity android:name=".EventEditActivity" />
<activity android:name=".WeekViewActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package codewithcal.au.calendarappexample;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.time.LocalDate;
import java.util.ArrayList;

class CalendarAdapter extends RecyclerView.Adapter<CalendarViewHolder>
{
private final ArrayList<String> daysOfMonth;
private final ArrayList<LocalDate> days;
private final OnItemListener onItemListener;

public CalendarAdapter(ArrayList<String> daysOfMonth, OnItemListener onItemListener)
public CalendarAdapter(ArrayList<LocalDate> days, OnItemListener onItemListener)
{
this.daysOfMonth = daysOfMonth;
this.days = days;
this.onItemListener = onItemListener;
}

Expand All @@ -27,24 +29,38 @@ public CalendarViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int view
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.calendar_cell, parent, false);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.height = (int) (parent.getHeight() * 0.166666666);
return new CalendarViewHolder(view, onItemListener);
if(days.size() > 15) //month view
layoutParams.height = (int) (parent.getHeight() * 0.166666666);
else // week view
layoutParams.height = (int) parent.getHeight();

return new CalendarViewHolder(view, onItemListener, days);
}

@Override
public void onBindViewHolder(@NonNull CalendarViewHolder holder, int position)
{
holder.dayOfMonth.setText(daysOfMonth.get(position));
final LocalDate date = days.get(position);

holder.dayOfMonth.setText(String.valueOf(date.getDayOfMonth()));

if(date.equals(CalendarUtils.selectedDate))
holder.parentView.setBackgroundColor(Color.LTGRAY);

if(date.getMonth().equals(CalendarUtils.selectedDate.getMonth()))
holder.dayOfMonth.setTextColor(Color.BLACK);
else
holder.dayOfMonth.setTextColor(Color.LTGRAY);
}

@Override
public int getItemCount()
{
return daysOfMonth.size();
return days.size();
}

public interface OnItemListener
{
void onItemClick(int position, String dayText);
void onItemClick(int position, LocalDate date);
}
}
102 changes: 102 additions & 0 deletions app/src/main/java/codewithcal/au/calendarappexample/CalendarUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package codewithcal.au.calendarappexample;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

public class CalendarUtils
{
public static LocalDate selectedDate;

public static String formattedDate(LocalDate date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMMM yyyy");
return date.format(formatter);
}

public static String formattedTime(LocalTime time)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
return time.format(formatter);
}

public static String formattedShortTime(LocalTime time)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
return time.format(formatter);
}

public static String monthYearFromDate(LocalDate date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM yyyy");
return date.format(formatter);
}

public static String monthDayFromDate(LocalDate date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d");
return date.format(formatter);
}

public static ArrayList<LocalDate> daysInMonthArray()
{
ArrayList<LocalDate> daysInMonthArray = new ArrayList<>();

YearMonth yearMonth = YearMonth.from(selectedDate);
int daysInMonth = yearMonth.lengthOfMonth();

LocalDate prevMonth = selectedDate.minusMonths(1);
LocalDate nextMonth = selectedDate.plusMonths(1);

YearMonth prevYearMonth = YearMonth.from(prevMonth);
int prevDaysInMonth = prevYearMonth.lengthOfMonth();

LocalDate firstOfMonth = CalendarUtils.selectedDate.withDayOfMonth(1);
int dayOfWeek = firstOfMonth.getDayOfWeek().getValue();

for(int i = 1; i <= 42; i++)
{
if(i <= dayOfWeek)
daysInMonthArray.add(LocalDate.of(prevMonth.getYear(),prevMonth.getMonth(),prevDaysInMonth + i - dayOfWeek));
else if(i > daysInMonth + dayOfWeek)
daysInMonthArray.add(LocalDate.of(nextMonth.getYear(),nextMonth.getMonth(),i - dayOfWeek - daysInMonth));
else
daysInMonthArray.add(LocalDate.of(selectedDate.getYear(),selectedDate.getMonth(),i - dayOfWeek));
}
return daysInMonthArray;
}

public static ArrayList<LocalDate> daysInWeekArray(LocalDate selectedDate)
{
ArrayList<LocalDate> days = new ArrayList<>();
LocalDate current = sundayForDate(selectedDate);
LocalDate endDate = current.plusWeeks(1);

while (current.isBefore(endDate))
{
days.add(current);
current = current.plusDays(1);
}
return days;
}

private static LocalDate sundayForDate(LocalDate current)
{
LocalDate oneWeekAgo = current.minusWeeks(1);

while (current.isAfter(oneWeekAgo))
{
if(current.getDayOfWeek() == DayOfWeek.SUNDAY)
return current;

current = current.minusDays(1);
}

return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,28 @@
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.time.LocalDate;
import java.util.ArrayList;

public class CalendarViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
private final ArrayList<LocalDate> days;
public final View parentView;
public final TextView dayOfMonth;
private final CalendarAdapter.OnItemListener onItemListener;
public CalendarViewHolder(@NonNull View itemView, CalendarAdapter.OnItemListener onItemListener)
public CalendarViewHolder(@NonNull View itemView, CalendarAdapter.OnItemListener onItemListener, ArrayList<LocalDate> days)
{
super(itemView);
parentView = itemView.findViewById(R.id.parentView);
dayOfMonth = itemView.findViewById(R.id.cellDayText);
this.onItemListener = onItemListener;
itemView.setOnClickListener(this);
this.days = days;
}

@Override
public void onClick(View view)
{
onItemListener.onItemClick(getAdapterPosition(), (String) dayOfMonth.getText());
onItemListener.onItemClick(getAdapterPosition(), days.get(getAdapterPosition()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package codewithcal.au.calendarappexample;

import static codewithcal.au.calendarappexample.CalendarUtils.selectedDate;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import java.time.LocalTime;
import java.time.format.TextStyle;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class DailyCalendarActivity extends AppCompatActivity
{

private TextView monthDayText;
private TextView dayOfWeekTV;
private ListView hourListView;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daily_calendar);
initWidgets();
}

private void initWidgets()
{
monthDayText = findViewById(R.id.monthDayText);
dayOfWeekTV = findViewById(R.id.dayOfWeekTV);
hourListView = findViewById(R.id.hourListView);
}

@Override
protected void onResume()
{
super.onResume();
setDayView();
}

private void setDayView()
{
monthDayText.setText(CalendarUtils.monthDayFromDate(selectedDate));
String dayOfWeek = selectedDate.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.getDefault());
dayOfWeekTV.setText(dayOfWeek);
setHourAdapter();
}

private void setHourAdapter()
{
HourAdapter hourAdapter = new HourAdapter(getApplicationContext(), hourEventList());
hourListView.setAdapter(hourAdapter);
}

private ArrayList<HourEvent> hourEventList()
{
ArrayList<HourEvent> list = new ArrayList<>();

for(int hour = 0; hour < 24; hour++)
{
LocalTime time = LocalTime.of(hour, 0);
ArrayList<Event> events = Event.eventsForDateAndTime(selectedDate, time);
HourEvent hourEvent = new HourEvent(time, events);
list.add(hourEvent);
}

return list;
}

public void previousDayAction(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.minusDays(1);
setDayView();
}

public void nextDayAction(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.plusDays(1);
setDayView();
}

public void newEventAction(View view)
{
startActivity(new Intent(this, EventEditActivity.class));
}
}
Loading