Skip to content

Commit

Permalink
Update library version to 1.2.2
Browse files Browse the repository at this point in the history
- Fixed wrong item being selected.
- Updated support library to v26.1.0
- Upgraded minSdkVersion to 14
  • Loading branch information
muraee committed Dec 15, 2017
1 parent 3707681 commit 18d37f0
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 115 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ repositories {
}
dependencies {
compile 'devs.mulham.horizontalcalendar:horizontalcalendar:1.2.0'
compile 'devs.mulham.horizontalcalendar:horizontalcalendar:1.2.2'
}
```

## Prerequisites
The minimum API level supported by this library is **API 9 (GINGERBREAD)**.
The minimum API level supported by this library is **API 14 (ICE_CREAM_SANDWICH)**.

## Usage
- Add `HorizontalCalendarView` to your layout file, for example:
Expand Down
14 changes: 7 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
compileSdkVersion 26
buildToolsVersion '26.0.2'

defaultConfig {
applicationId "devs.mulham.raee.horizontalcalendarlibrary"
minSdkVersion 14
targetSdkVersion 25
targetSdkVersion 26
versionCode 1
versionName "1.1"
versionName "1.0"
}
buildTypes {
release {
Expand All @@ -22,8 +22,8 @@ android {
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':horizontalcalendar')
compile 'com.android.support:appcompat-v7:25.3.0'
compile 'com.android.support:design:25.3.0'
compile 'com.android.support:support-v4:25.3.0'
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:design:26.1.0'
compile 'com.android.support:support-v4:26.1.0'
testCompile 'junit:junit:4.12'
}
23 changes: 13 additions & 10 deletions app/src/main/java/devs/mulham/raee/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

/** end after 2 months from now */
/* end after 2 months from now */
Calendar endDate = Calendar.getInstance();
endDate.add(Calendar.MONTH, 2);

/** start 2 months ago from now */
/* start 2 months ago from now */
Calendar startDate = Calendar.getInstance();
startDate.add(Calendar.MONTH, -2);

final Calendar defaultDate = Calendar.getInstance();
defaultDate.add(Calendar.MONTH, -1);
defaultDate.add(Calendar.DAY_OF_WEEK, +5);
final Calendar defaultDateCalendar = Calendar.getInstance();
defaultDateCalendar.add(Calendar.MONTH, -1);
defaultDateCalendar.add(Calendar.DAY_OF_WEEK, +5);

final Date defaultDate = defaultDateCalendar.getTime();

horizontalCalendar = new HorizontalCalendar.Builder(this, R.id.calendarView)
.startDate(startDate.getTime())
Expand All @@ -51,21 +53,22 @@ protected void onCreate(Bundle savedInstanceState) {
.monthFormat("MMM")
.showDayName(true)
.showMonthName(true)
.selectedDateBackground(ContextCompat.getDrawable(this, R.drawable.sample_selected_background))
.defaultSelectedDate(defaultDate.getTime())
.defaultSelectedDate(defaultDate)
.textColor(Color.LTGRAY, Color.WHITE)
.build();

Log.i("Default Date", DateFormat.getDateInstance().format(defaultDate));

horizontalCalendar.setCalendarListener(new HorizontalCalendarListener() {
@Override
public void onDateSelected(Date date, int position) {
Toast.makeText(MainActivity.this, DateFormat.getDateInstance().format(date) + " is selected!", Toast.LENGTH_SHORT).show();
Log.d("Selected Item: ", DateFormat.getDateInstance().format(date));
Log.i("onDateSelected", DateFormat.getDateInstance().format(date) + " - Position = " + position);
}

});

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down
9 changes: 7 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
buildscript {
repositories {
jcenter()
maven {
url 'https://maven.google.com/'
name 'Google'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
}

allprojects {
repositories {
jcenter()
google()
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Mar 12 23:59:56 CET 2017
#Fri Dec 15 19:42:39 CET 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
16 changes: 8 additions & 8 deletions horizontalcalendar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ext {
siteUrl = 'https://github.com/Mulham-Raee/Horizontal-Calendar'
gitUrl = 'https://github.com/Mulham-Raee/Horizontal-Calendar.git'

libraryVersion = '1.2.0'
libraryVersion = '1.2.2'

developerId = 'mulham-raee'
developerName = 'Mulham Raee'
Expand All @@ -28,16 +28,16 @@ ext {
version = libraryVersion

android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
compileSdkVersion 26
buildToolsVersion '26.0.2'

lintOptions {
abortOnError false
}

defaultConfig {
minSdkVersion 9
targetSdkVersion 25
minSdkVersion 14
targetSdkVersion 26
versionCode 1
versionName version
}
Expand All @@ -51,8 +51,8 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'
}


Expand Down Expand Up @@ -101,7 +101,7 @@ bintray {
version {
desc = libraryDescription
name = libraryVersion
vcsTag = 'v1.2.0'
vcsTag = 'v' + libraryVersion
gpg {
sign = true //Determines whether to GPG sign the files. The default is false
passphrase = properties.getProperty("bintray.gpg.password")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
* See {@link HorizontalCalendarView HorizontalCalendarView}
*
* @author Mulham-Raee
* @version 1.1
* @see HorizontalCalendarListener
* @since v1.0.0
*/
public final class HorizontalCalendar {

Expand Down Expand Up @@ -93,7 +93,7 @@ void loadHorizontalCalendar() {
dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());

mListDays = new ArrayList<>();
calendarView = (HorizontalCalendarView) rootView.findViewById(calendarId);
calendarView = rootView.findViewById(calendarId);
calendarView.setHasFixedSize(true);
calendarView.setHorizontalScrollBarEnabled(false);
calendarView.setHorizontalCalendar(this);
Expand Down Expand Up @@ -135,15 +135,15 @@ public void selectDate(Date date, boolean immediate) {
handler.date = date;
handler.immediate = immediate;
} else {
int datePosition = positionOfDate(date);
if (immediate) {
int datePosition = positionOfDate(date);
centerToPositionWithNoAnimation(datePosition);
if (calendarListener != null) {
calendarListener.onDateSelected(date, datePosition);
}
} else {
calendarView.setSmoothScrollSpeed(HorizontalLayoutManager.SPEED_NORMAL);
centerCalendarToPosition(positionOfDate(date));
centerCalendarToPosition(datePosition);
}
}
}
Expand All @@ -155,8 +155,8 @@ public void selectDate(Date date, boolean immediate) {
*/
void centerCalendarToPosition(int position) {
if (position != -1) {
int shiftCells = numberOfDatesOnScreen / 2;
int centerItem = calendarView.getPositionOfCenterItem();
final int centerItem = calendarView.getPositionOfCenterItem();
final int shiftCells = getShiftCells();

if (position > centerItem) {
calendarView.smoothScrollToPosition(position + shiftCells);
Expand All @@ -168,8 +168,8 @@ void centerCalendarToPosition(int position) {

private void centerToPositionWithNoAnimation(final int position) {
if (position != -1) {
int shiftCells = numberOfDatesOnScreen / 2;
int centerItem = calendarView.getPositionOfCenterItem();
final int centerItem = calendarView.getPositionOfCenterItem();
final int shiftCells = getShiftCells();

if (position > centerItem) {
calendarView.scrollToPosition(position + shiftCells);
Expand All @@ -180,12 +180,31 @@ private void centerToPositionWithNoAnimation(final int position) {
calendarView.post(new Runnable() {
@Override
public void run() {
mCalendarAdapter.notifyDataSetChanged();
final int newSelectedItem = calendarView.getPositionOfCenterItem();
//refresh to update background colors
refreshItemsSelector(newSelectedItem, centerItem);
}
});
}
}

private int getShiftCells() {
return numberOfDatesOnScreen / 2;
}

void refreshItemSelector(int position){
mCalendarAdapter.notifyItemChanged(position, "UPDATE_SELECTOR");
}

void refreshItemsSelector(int position1, int... positions){
refreshItemSelector(position1);
if ((positions != null) && (positions.length > 0)){
for (int pos : positions){
refreshItemSelector(pos);
}
}
}

public void show() {
calendarView.setVisibility(View.VISIBLE);
}
Expand Down Expand Up @@ -330,18 +349,20 @@ public void setTextSizeDayName(float textSizeDayName) {
public int positionOfDate(Date date) {
if (date.after(dateEndCalendar) || date.before(dateStartCalendar)) {
return -1;
} else if (isDatesDaysEquals(date, dateStartCalendar)) {
return 0;
} else if (isDatesDaysEquals(date, dateEndCalendar)) {
return mListDays.size() - 1;
}

long diff = date.getTime() - dateStartCalendar.getTime(); //result in millis
long days = (diff / (24 * 60 * 60 * 1000));
int position ;
if (isDatesDaysEquals(date, dateStartCalendar)) {
position = 0;
} else {
long diff = date.getTime() - dateStartCalendar.getTime(); //result in millis
long days = (diff / (24 * 60 * 60 * 1000));

int position = (int) days + 2;
position = (int) days;
}

return position;
final int shiftCells = getShiftCells();
return position + shiftCells;
}

/**
Expand Down Expand Up @@ -560,10 +581,10 @@ protected Void doInBackground(Void... params) {
GregorianCalendar calendar = new GregorianCalendar();

calendar.setTime(dateStartCalendar);
calendar.add(Calendar.DATE, -(numberOfDatesOnScreen / 2));
calendar.add(Calendar.DATE, -getShiftCells());
Date dateStartBefore = calendar.getTime();
calendar.setTime(dateEndCalendar);
calendar.add(Calendar.DATE, numberOfDatesOnScreen / 2);
calendar.add(Calendar.DATE, getShiftCells());
Date dateEndAfter = calendar.getTime();

Date date = dateStartBefore;
Expand Down Expand Up @@ -645,9 +666,9 @@ public void run() {
if ((lastSelectedItem == -1) || (lastSelectedItem != positionOfCenterItem)) {
//On Scroll, agenda is refresh to update background colors
//mCalendarAdapter.notifyItemRangeChanged(getSelectedDatePosition() - 2, 5, "UPDATE_SELECTOR");
mCalendarAdapter.notifyItemChanged(positionOfCenterItem, "UPDATE_SELECTOR");
refreshItemSelector(positionOfCenterItem);
if (lastSelectedItem != -1) {
mCalendarAdapter.notifyItemChanged(lastSelectedItem, "UPDATE_SELECTOR");
refreshItemSelector(lastSelectedItem);
}
lastSelectedItem = positionOfCenterItem;
}
Expand Down
Loading

0 comments on commit 18d37f0

Please sign in to comment.