diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2bcd16f..27fc4b4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,6 +9,8 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.CalendarAppExample"> + + diff --git a/app/src/main/java/codewithcal/au/calendarappexample/CalendarAdapter.java b/app/src/main/java/codewithcal/au/calendarappexample/CalendarAdapter.java index 92b7ec5..eafa94f 100644 --- a/app/src/main/java/codewithcal/au/calendarappexample/CalendarAdapter.java +++ b/app/src/main/java/codewithcal/au/calendarappexample/CalendarAdapter.java @@ -1,5 +1,6 @@ package codewithcal.au.calendarappexample; +import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -7,16 +8,17 @@ import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; +import java.time.LocalDate; import java.util.ArrayList; class CalendarAdapter extends RecyclerView.Adapter { - private final ArrayList daysOfMonth; + private final ArrayList days; private final OnItemListener onItemListener; - public CalendarAdapter(ArrayList daysOfMonth, OnItemListener onItemListener) + public CalendarAdapter(ArrayList days, OnItemListener onItemListener) { - this.daysOfMonth = daysOfMonth; + this.days = days; this.onItemListener = onItemListener; } @@ -27,24 +29,36 @@ 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); + if(date == null) + holder.dayOfMonth.setText(""); + else + { + holder.dayOfMonth.setText(String.valueOf(date.getDayOfMonth())); + if(date.equals(CalendarUtils.selectedDate)) + holder.parentView.setBackgroundColor(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); } } diff --git a/app/src/main/java/codewithcal/au/calendarappexample/CalendarUtils.java b/app/src/main/java/codewithcal/au/calendarappexample/CalendarUtils.java new file mode 100644 index 0000000..bb40ee7 --- /dev/null +++ b/app/src/main/java/codewithcal/au/calendarappexample/CalendarUtils.java @@ -0,0 +1,82 @@ +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 monthYearFromDate(LocalDate date) + { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM yyyy"); + return date.format(formatter); + } + + public static ArrayList daysInMonthArray(LocalDate date) + { + ArrayList daysInMonthArray = new ArrayList<>(); + YearMonth yearMonth = YearMonth.from(date); + + int daysInMonth = yearMonth.lengthOfMonth(); + + LocalDate firstOfMonth = CalendarUtils.selectedDate.withDayOfMonth(1); + int dayOfWeek = firstOfMonth.getDayOfWeek().getValue(); + + for(int i = 1; i <= 42; i++) + { + if(i <= dayOfWeek || i > daysInMonth + dayOfWeek) + daysInMonthArray.add(null); + else + daysInMonthArray.add(LocalDate.of(selectedDate.getYear(),selectedDate.getMonth(),i - dayOfWeek)); + } + return daysInMonthArray; + } + + public static ArrayList daysInWeekArray(LocalDate selectedDate) + { + ArrayList 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; + } + + +} diff --git a/app/src/main/java/codewithcal/au/calendarappexample/CalendarViewHolder.java b/app/src/main/java/codewithcal/au/calendarappexample/CalendarViewHolder.java index 3867d22..1ceb306 100644 --- a/app/src/main/java/codewithcal/au/calendarappexample/CalendarViewHolder.java +++ b/app/src/main/java/codewithcal/au/calendarappexample/CalendarViewHolder.java @@ -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 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 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())); } } diff --git a/app/src/main/java/codewithcal/au/calendarappexample/Event.java b/app/src/main/java/codewithcal/au/calendarappexample/Event.java new file mode 100644 index 0000000..097b996 --- /dev/null +++ b/app/src/main/java/codewithcal/au/calendarappexample/Event.java @@ -0,0 +1,65 @@ +package codewithcal.au.calendarappexample; + +import java.time.LocalDate; +import java.time.LocalTime; +import java.util.ArrayList; + +public class Event +{ + public static ArrayList eventsList = new ArrayList<>(); + + public static ArrayList eventsForDate(LocalDate date) + { + ArrayList events = new ArrayList<>(); + + for(Event event : eventsList) + { + if(event.getDate().equals(date)) + events.add(event); + } + + return events; + } + + + private String name; + private LocalDate date; + private LocalTime time; + + public Event(String name, LocalDate date, LocalTime time) + { + this.name = name; + this.date = date; + this.time = time; + } + + public String getName() + { + return name; + } + + public void setName(String name) + { + this.name = name; + } + + public LocalDate getDate() + { + return date; + } + + public void setDate(LocalDate date) + { + this.date = date; + } + + public LocalTime getTime() + { + return time; + } + + public void setTime(LocalTime time) + { + this.time = time; + } +} diff --git a/app/src/main/java/codewithcal/au/calendarappexample/EventAdapter.java b/app/src/main/java/codewithcal/au/calendarappexample/EventAdapter.java new file mode 100644 index 0000000..acf084b --- /dev/null +++ b/app/src/main/java/codewithcal/au/calendarappexample/EventAdapter.java @@ -0,0 +1,37 @@ +package codewithcal.au.calendarappexample; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.util.List; + +public class EventAdapter extends ArrayAdapter +{ + public EventAdapter(@NonNull Context context, List events) + { + super(context, 0, events); + } + + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) + { + Event event = getItem(position); + + if (convertView == null) + convertView = LayoutInflater.from(getContext()).inflate(R.layout.event_cell, parent, false); + + TextView eventCellTV = convertView.findViewById(R.id.eventCellTV); + + String eventTitle = event.getName() +" "+ CalendarUtils.formattedTime(event.getTime()); + eventCellTV.setText(eventTitle); + return convertView; + } +} diff --git a/app/src/main/java/codewithcal/au/calendarappexample/EventEditActivity.java b/app/src/main/java/codewithcal/au/calendarappexample/EventEditActivity.java new file mode 100644 index 0000000..9c9b365 --- /dev/null +++ b/app/src/main/java/codewithcal/au/calendarappexample/EventEditActivity.java @@ -0,0 +1,44 @@ +package codewithcal.au.calendarappexample; + +import androidx.appcompat.app.AppCompatActivity; + +import android.os.Bundle; +import android.view.View; +import android.widget.EditText; +import android.widget.TextView; + +import java.time.LocalTime; + +public class EventEditActivity extends AppCompatActivity +{ + private EditText eventNameET; + private TextView eventDateTV, eventTimeTV; + + private LocalTime time; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_event_edit); + initWidgets(); + time = LocalTime.now(); + eventDateTV.setText("Date: " + CalendarUtils.formattedDate(CalendarUtils.selectedDate)); + eventTimeTV.setText("Time: " + CalendarUtils.formattedTime(time)); + } + + private void initWidgets() + { + eventNameET = findViewById(R.id.eventNameET); + eventDateTV = findViewById(R.id.eventDateTV); + eventTimeTV = findViewById(R.id.eventTimeTV); + } + + public void saveEventAction(View view) + { + String eventName = eventNameET.getText().toString(); + Event newEvent = new Event(eventName, CalendarUtils.selectedDate, time); + Event.eventsList.add(newEvent); + finish(); + } +} \ No newline at end of file diff --git a/app/src/main/java/codewithcal/au/calendarappexample/MainActivity.java b/app/src/main/java/codewithcal/au/calendarappexample/MainActivity.java index 0cf6f4c..762fafa 100644 --- a/app/src/main/java/codewithcal/au/calendarappexample/MainActivity.java +++ b/app/src/main/java/codewithcal/au/calendarappexample/MainActivity.java @@ -4,6 +4,7 @@ import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; @@ -14,11 +15,14 @@ import java.time.format.DateTimeFormatter; import java.util.ArrayList; +import static codewithcal.au.calendarappexample.CalendarUtils.daysInMonthArray; +import static codewithcal.au.calendarappexample.CalendarUtils.monthYearFromDate; + public class MainActivity extends AppCompatActivity implements CalendarAdapter.OnItemListener { private TextView monthYearText; private RecyclerView calendarRecyclerView; - private LocalDate selectedDate; + @Override protected void onCreate(Bundle savedInstanceState) @@ -26,7 +30,7 @@ protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initWidgets(); - selectedDate = LocalDate.now(); + CalendarUtils.selectedDate = LocalDate.now(); setMonthView(); } @@ -38,8 +42,8 @@ private void initWidgets() private void setMonthView() { - monthYearText.setText(monthYearFromDate(selectedDate)); - ArrayList daysInMonth = daysInMonthArray(selectedDate); + monthYearText.setText(monthYearFromDate(CalendarUtils.selectedDate)); + ArrayList daysInMonth = daysInMonthArray(CalendarUtils.selectedDate); CalendarAdapter calendarAdapter = new CalendarAdapter(daysInMonth, this); RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7); @@ -47,57 +51,32 @@ private void setMonthView() calendarRecyclerView.setAdapter(calendarAdapter); } - private ArrayList daysInMonthArray(LocalDate date) - { - ArrayList daysInMonthArray = new ArrayList<>(); - YearMonth yearMonth = YearMonth.from(date); - - int daysInMonth = yearMonth.lengthOfMonth(); - - LocalDate firstOfMonth = selectedDate.withDayOfMonth(1); - int dayOfWeek = firstOfMonth.getDayOfWeek().getValue(); - - for(int i = 1; i <= 42; i++) - { - if(i <= dayOfWeek || i > daysInMonth + dayOfWeek) - { - daysInMonthArray.add(""); - } - else - { - daysInMonthArray.add(String.valueOf(i - dayOfWeek)); - } - } - return daysInMonthArray; - } - - private String monthYearFromDate(LocalDate date) - { - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM yyyy"); - return date.format(formatter); - } - public void previousMonthAction(View view) { - selectedDate = selectedDate.minusMonths(1); + CalendarUtils.selectedDate = CalendarUtils.selectedDate.minusMonths(1); setMonthView(); } public void nextMonthAction(View view) { - selectedDate = selectedDate.plusMonths(1); + CalendarUtils.selectedDate = CalendarUtils.selectedDate.plusMonths(1); setMonthView(); } @Override - public void onItemClick(int position, String dayText) + public void onItemClick(int position, LocalDate date) { - if(!dayText.equals("")) + if(date != null) { - String message = "Selected Date " + dayText + " " + monthYearFromDate(selectedDate); - Toast.makeText(this, message, Toast.LENGTH_LONG).show(); + CalendarUtils.selectedDate = date; + setMonthView(); } } + + public void weeklyAction(View view) + { + startActivity(new Intent(this, WeekViewActivity.class)); + } } diff --git a/app/src/main/java/codewithcal/au/calendarappexample/WeekViewActivity.java b/app/src/main/java/codewithcal/au/calendarappexample/WeekViewActivity.java new file mode 100644 index 0000000..3f897a1 --- /dev/null +++ b/app/src/main/java/codewithcal/au/calendarappexample/WeekViewActivity.java @@ -0,0 +1,93 @@ +package codewithcal.au.calendarappexample; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.recyclerview.widget.GridLayoutManager; +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 android.widget.Toast; + +import java.time.LocalDate; +import java.util.ArrayList; + +import static codewithcal.au.calendarappexample.CalendarUtils.daysInMonthArray; +import static codewithcal.au.calendarappexample.CalendarUtils.daysInWeekArray; +import static codewithcal.au.calendarappexample.CalendarUtils.monthYearFromDate; + +public class WeekViewActivity extends AppCompatActivity implements CalendarAdapter.OnItemListener +{ + private TextView monthYearText; + private RecyclerView calendarRecyclerView; + private ListView eventListView; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_week_view); + initWidgets(); + setWeekView(); + } + + private void initWidgets() + { + calendarRecyclerView = findViewById(R.id.calendarRecyclerView); + monthYearText = findViewById(R.id.monthYearTV); + eventListView = findViewById(R.id.eventListView); + } + + private void setWeekView() + { + monthYearText.setText(monthYearFromDate(CalendarUtils.selectedDate)); + ArrayList days = daysInWeekArray(CalendarUtils.selectedDate); + + CalendarAdapter calendarAdapter = new CalendarAdapter(days, this); + RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7); + calendarRecyclerView.setLayoutManager(layoutManager); + calendarRecyclerView.setAdapter(calendarAdapter); + setEventAdpater(); + } + + + public void previousWeekAction(View view) + { + CalendarUtils.selectedDate = CalendarUtils.selectedDate.minusWeeks(1); + setWeekView(); + } + + public void nextWeekAction(View view) + { + CalendarUtils.selectedDate = CalendarUtils.selectedDate.plusWeeks(1); + setWeekView(); + } + + @Override + public void onItemClick(int position, LocalDate date) + { + CalendarUtils.selectedDate = date; + setWeekView(); + } + + @Override + protected void onResume() + { + super.onResume(); + setEventAdpater(); + } + + private void setEventAdpater() + { + ArrayList dailyEvents = Event.eventsForDate(CalendarUtils.selectedDate); + EventAdapter eventAdapter = new EventAdapter(getApplicationContext(), dailyEvents); + eventListView.setAdapter(eventAdapter); + } + + public void newEventAction(View view) + { + startActivity(new Intent(this, EventEditActivity.class)); + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_event_edit.xml b/app/src/main/res/layout/activity_event_edit.xml new file mode 100644 index 0000000..50ee1c7 --- /dev/null +++ b/app/src/main/res/layout/activity_event_edit.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + +