Skip to content
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

Week view added #2

Open
wants to merge 2 commits into
base: develop
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
15 changes: 1 addition & 14 deletions AndroidCalendar/.idea/misc.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,76 @@
package com.androidcalendar.adapters;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.androidcalendar.objects.CalendarWeek;
import com.androidcalendar.views.CalendarWeekView;

import java.util.List;


public class CalendarWeekViewPagerAdapter extends PagerAdapter {

List<CalendarWeek> mData;

public CalendarWeekViewPagerAdapter(List<CalendarWeek> list) {
mData = list;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
CalendarWeek month = mData.get(position);
CalendarWeekView calendarView = new CalendarWeekView(container.getContext());
calendarView.buildView(month);
(container).addView(calendarView, 0);
calendarView.setTag(month);
return calendarView;
}

@Override
public int getCount() {
return mData.size();
}

@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

@Override
public int getItemPosition(Object object) {
View view = (View) object;
CalendarWeek month = (CalendarWeek) view.getTag();
int position = mData.indexOf(month);

if (position >= 0) {
return position;
} else {
return POSITION_NONE;
}
}

public void addNext(CalendarWeek month) {
mData.add(month);
notifyDataSetChanged();
}

public void addPrev(CalendarWeek month) {
mData.add(0, month);
notifyDataSetChanged();
}

public String getItemPageHeader(int position) {
return mData.get(position).toString();
}

public CalendarWeek getItem(int position) {
return mData.get(position);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public CalendarDate(long millis) {
mYear = calendar.get(Calendar.YEAR);
}


public int getDay() {
return mDay;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.androidcalendar.objects;

import android.util.Log;

import com.androidcalendar.utils.Utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


public class CalendarWeek {

public static final int NUMBER_OF_WEEKS_IN_WEEK = 1;
public static final int NUMBER_OF_DAYS_IN_WEEK = 7;
private int mMonth;
private int mYear;
private int mWeekOfMonth;
private int mWeekOfYear;
private List<CalendarDate> mDays;
private int mCurrentDayOfMonth;

public CalendarWeek(Calendar calendar) {
mMonth = calendar.get(Calendar.MONTH);
mYear = calendar.get(Calendar.YEAR);
mWeekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
mWeekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
mCurrentDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
createWeekDays();
}

public CalendarWeek(CalendarWeek other, int value) {
Calendar calendar = Calendar.getInstance();

// set calendarWeek
calendar.set(other.mYear, other.mMonth, other.mCurrentDayOfMonth);

// add number of weeks to current calendar
calendar.add(Calendar.WEEK_OF_YEAR, value);


mMonth = calendar.get(Calendar.MONTH);
mYear = calendar.get(Calendar.YEAR);
mWeekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
mWeekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
mCurrentDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
createWeekDays();
}

private void createWeekDays() {
Calendar calendar = Calendar.getInstance();
calendar.set(mYear, mMonth, mCurrentDayOfMonth);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
calendar.add(Calendar.DATE, 1 - dayOfWeek);

Log.d("TEST", "Day of week :" + dayOfWeek + " DAte :" + calendar.getTime().toString());

CalendarDate date = new CalendarDate(calendar.get(Calendar.DAY_OF_MONTH), mMonth, mYear);

mDays = new ArrayList<>();
for (int i = 0; i < NUMBER_OF_DAYS_IN_WEEK; i++) {
CalendarDate day = new CalendarDate(date.getDay(), date.getMonth(), date.getYear());
mDays.add(day);
date.addDays(1);
}
}

public int getMonth() {
return mMonth;
}

public int getYear() {
return mYear;
}

public int getCurrentDayOfMonth() {
return mCurrentDayOfMonth;
}

public List<CalendarDate> getDays() {
return mDays;
}

@Override
public String toString() {
return Utils.monthToString(mMonth) + " " + mYear;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.androidcalendar.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.GridLayout;
import android.widget.TextView;

import com.androidcalendar.R;
import com.androidcalendar.objects.CalendarDate;
import com.androidcalendar.objects.CalendarWeek;
import com.androidcalendar.utils.Utils;


public class CalendarWeekView extends FrameLayout {

private GridLayout mGridLayout;
private ViewGroup mLayoutDays;

public CalendarWeekView(Context context) {
super(context);
init();
}

public CalendarWeekView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public CalendarWeekView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

private void init() {
inflate(getContext(), R.layout.view_calendar_month, this);
mGridLayout = (GridLayout) findViewById(R.id.view_calendar_month_grid);
mLayoutDays = (ViewGroup) findViewById(R.id.view_calendar_month_layout_days);
}

public void buildView(CalendarWeek calendarWeek) {
buildDaysLayout();
buildGridView(calendarWeek);
}

private void buildDaysLayout() {
String[] days;
days = getResources().getStringArray(R.array.days_sunday_array);

for (int i = 0; i < mLayoutDays.getChildCount(); i++) {
TextView textView = (TextView) mLayoutDays.getChildAt(i);
textView.setText(days[i]);
}
}

private void buildGridView(CalendarWeek calendarWeek) {
int row = CalendarWeek.NUMBER_OF_WEEKS_IN_WEEK;
int col = CalendarWeek.NUMBER_OF_DAYS_IN_WEEK;
mGridLayout.setRowCount(row);
mGridLayout.setColumnCount(col);

int screenWidth = Utils.getScreenWidth(getContext());
int width = screenWidth / col;

for (CalendarDate day : calendarWeek.getDays()) {
CalendarDayView dayView = new CalendarDayView(getContext(), day);
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.width = width;
params.height = LayoutParams.WRAP_CONTENT;

dayView.setLayoutParams(params);
dayView.setTextDay("" + day.getDay());
decorateDayView(dayView, day, calendarWeek.getMonth());
mGridLayout.addView(dayView);
}
}

protected void decorateDayView(CalendarDayView dayView, CalendarDate day, int month) {

// if (day.getMonth() != month) {
// dayView.setOtherMothTextColor();
// } else {
// dayView.setThisMothTextColor();
// }

dayView.setThisMothTextColor();

if (day.isToday()) {
dayView.setPurpleSolidOvalBackground();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ public void onPageSelected(int position) {

@Override
public void onPageScrollStateChanged(int state) {

int position = mViewPager.getCurrentItem();
mPagerTextMonth.setText(mViewPagerAdapter.getItemPageHeader(position));

// current item is the first item in the list
if (state == ViewPager.SCROLL_STATE_IDLE && position == 1) {
addPrev();
Expand Down
Loading