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

Use persian date picker as a view #93

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ android {
}

dependencies {

implementation "androidx.appcompat:appcompat:$appCompat"
implementation "com.google.android.material:material:$material"

implementation project(":persiandatepicker")

}
repositories {
mavenCentral()
Expand Down
24 changes: 23 additions & 1 deletion app/src/main/java/ir/hamsaa/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@

import androidx.appcompat.app.AppCompatActivity;

import java.text.ParseException;

import ir.hamsaa.persiandatepicker.Listener;
import ir.hamsaa.persiandatepicker.PersianDatePickerDialog;
import ir.hamsaa.persiandatepicker.api.PersianPickerDate;
import ir.hamsaa.persiandatepicker.api.PersianPickerListener;
import ir.hamsaa.persiandatepicker.date.PersianDateFixedLeapYear;
import ir.hamsaa.persiandatepicker.date.PersianDateImpl;
import ir.hamsaa.persiandatepicker.util.PersianCalendar;
import saman.zamani.persiandate.PersianDateFormat;


public class MainActivity extends AppCompatActivity {
Expand All @@ -28,6 +32,25 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


PersianDateFixedLeapYear persianDate = new PersianDateFixedLeapYear();
Log.d(TAG, "today: " + persianDate.getShDay());
persianDate.addDay(1);
Log.d(TAG, "tomorrow: " + persianDate.getShDay());


// format `persianDate`
Log.d(TAG, PersianDateFormat.format(persianDate, "Y/m/d"));

// format string persian date
try {
Log.d(TAG, PersianDateFormat.format(new PersianDateFormat("yyyy/MM/dd").parse("1401/03/16"), "y-m-d"));
} catch (ParseException e) {
e.printStackTrace();
}


}


Expand Down Expand Up @@ -90,7 +113,6 @@ public void showCalendarInDarkMode(View v) {
.setPickerBackgroundDrawable(R.drawable.darkmode_bg)
.setTitleType(PersianDatePickerDialog.MONTH_YEAR)
.setShowDayPicker(false)

.setCancelable(false)
.setListener(new Listener() {
@Override
Expand Down
Binary file added app/src/main/res/font/shabnam_light_fd.ttf
Binary file not shown.
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -22,4 +23,12 @@
android:layout_height="wrap_content"
android:onClick="showCalendarInDarkMode"
android:text="show in darkMode" />

<ir.hamsaa.persiandatepicker.PersianDatePicker
android:layout_width="250dp"
android:layout_height="150dp"
app:fontFamily="@font/shabnam_light_fd"
app:background="@color/colorAccent"
app:backgroundColor="@color/colorPrimaryDark"/>

</LinearLayout>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
ext {
appCompat = '1.4.1'
material = '1.5.0'
persianDate = '1.3.4'
persianDate = '1.5.0'

libraryMinSdk = 14
libraryTargetSdk = 31
Expand All @@ -15,7 +15,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
classpath 'com.android.tools.build:gradle:7.1.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
7 changes: 3 additions & 4 deletions persiandatepicker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ android {
}

dependencies {
api "androidx.appcompat:appcompat:$appCompat"
api "com.google.android.material:material:$material"
implementation "com.github.samanzamani:PersianDate:$persianDate"

implementation "androidx.appcompat:appcompat:$appCompat"
implementation "com.google.android.material:material:$material"
api "com.github.samanzamani:PersianDate:$persianDate"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.core.content.res.ResourcesCompat;

import java.util.Date;

Expand All @@ -27,7 +28,7 @@
import ir.hamsaa.persiandatepicker.view.PersianNumberPicker;


class PersianDatePicker extends LinearLayout {
public class PersianDatePicker extends LinearLayout {

private PersianPickerDate persianDate;
private int selectedMonth;
Expand All @@ -46,7 +47,7 @@ class PersianDatePicker extends LinearLayout {

private boolean displayDescription;
private final TextView descriptionTextView;
private Typeface typeFace;
public static Typeface typeFace;
private int dividerColor;
private int yearRange;

Expand All @@ -61,6 +62,14 @@ public PersianDatePicker(Context context, AttributeSet attrs) {
public PersianDatePicker(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);


TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PersianDatePicker, 0, 0);
yearRange = a.getInteger(R.styleable.PersianDatePicker_yearRange, 10);

if (a.hasValue(R.styleable.PersianDatePicker_fontFamily))
typeFace = ResourcesCompat.getFont(context, a.getResourceId(R.styleable.PersianDatePicker_fontFamily, 0));


// inflate views
View view = LayoutInflater.from(context).inflate(R.layout.sl_persian_date_picker, this);

Expand All @@ -70,7 +79,6 @@ public PersianDatePicker(Context context, AttributeSet attrs, int defStyle) {
dayNumberPicker = view.findViewById(R.id.dayNumberPicker);
descriptionTextView = view.findViewById(R.id.descriptionTextView);


yearNumberPicker.setFormatter(new NumberPicker.Formatter() {
@Override
public String format(int i) {
Expand All @@ -95,17 +103,6 @@ public String format(int i) {
// init calendar
persianDate = new PersianDateImpl();

// update variables from xml
updateVariablesFromXml(context, attrs);

// update view
updateViewData();
}

private void updateVariablesFromXml(Context context, AttributeSet attrs) {

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PersianDatePicker, 0, 0);
yearRange = a.getInteger(R.styleable.PersianDatePicker_yearRange, 10);
/*
* Initializing yearNumberPicker min and max values If minYear and
* maxYear attributes are not set, use (current year - 10) as min and
Expand All @@ -131,6 +128,14 @@ private void updateVariablesFromXml(Context context, AttributeSet attrs) {
maxYear = selectedYear + yearRange;
}

if (a.hasValue(R.styleable.PersianDatePicker_background))
setBackgroundDrawable(a.getResourceId(R.styleable.PersianDatePicker_background, 0));
if (a.hasValue(R.styleable.PersianDatePicker_backgroundColor))
setBackgroundColor(a.getColor(R.styleable.PersianDatePicker_backgroundColor, 0));

// update view
updateViewData();

a.recycle();
}

Expand Down Expand Up @@ -216,11 +221,11 @@ private void updateViewData() {
dayNumberPicker.setTypeFace(typeFace);
}

if (dividerColor > 0) {
setDividerColor(yearNumberPicker, dividerColor);
setDividerColor(monthNumberPicker, dividerColor);
setDividerColor(dayNumberPicker, dividerColor);
}

setDividerColor(yearNumberPicker, dividerColor);
setDividerColor(monthNumberPicker, dividerColor);
setDividerColor(dayNumberPicker, dividerColor);


yearNumberPicker.setMinValue(minYear);
yearNumberPicker.setMaxValue(maxYear);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Date;

import ir.hamsaa.persiandatepicker.date.PersianDateFixedLeapYear;

public interface PersianPickerDate {

void setDate(Long timestamp);
Expand All @@ -10,6 +12,8 @@ public interface PersianPickerDate {

void setDate(int persianYear, int persianMonth, int persianDay);

PersianDateFixedLeapYear getPersianDate();

int getPersianYear();

int getPersianMonth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public void setDate(int persianYear, int persianMonth, int persianDay) {
}
}

@Override
public PersianDateFixedLeapYear getPersianDate() {
return persianDate;
}

@Override
public int getPersianYear() {
return persianDate.getShYear();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ir.hamsaa.persiandatepicker.util;

import java.text.ParseException;

import saman.zamani.persiandate.PersianDate;
import saman.zamani.persiandate.PersianDateFormat;

public class PersianDateHelper {

static public boolean isValidDate(String date) {
try {
return !parseAndFormatDate(date).isEmpty();
} catch (Throwable ignore) {

}
return false;
}

static public PersianDate parseDate(String date) {
return parseDateTime(date, "yyyy/MM/dd");
}

static public PersianDate parseTime(String date) {
return parseDateTime(date, "HH:mm:ss");
}

static public PersianDate parseDateTime(String datetime) {
return parseDateTime(datetime, "yyyy/MM/dd HH:mm:ss");
}

static public PersianDate parseDateTime(String date, String pattern) {
try {
return new PersianDateFormat(pattern).parse(date);
} catch (ParseException ignore) {
}
return null;
}

static public String parseAndFormatDate(String date) {
try {
return PersianDateFormat.format(parseDate(date), "Y/m/d");
} catch (Throwable ignore) {

}
return "";
}

static public String formatDate(PersianDate date) {
try {
return PersianDateFormat.format(date, "Y/m/d");
} catch (Throwable ignore) {

}
return "";
}

static public String formatTime(PersianDate time) {
try {
return PersianDateFormat.format(time, "H:i:s");
} catch (Throwable ignore) {

}
return "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.ArrayList;
import java.util.List;

import ir.hamsaa.persiandatepicker.PersianDatePicker;
import ir.hamsaa.persiandatepicker.PersianDatePickerDialog;

/**
Expand Down Expand Up @@ -61,6 +62,8 @@ private void updateView(View view) {
if (view instanceof TextView) {
if (PersianDatePickerDialog.typeFace != null)
((TextView) view).setTypeface(PersianDatePickerDialog.typeFace);
if (PersianDatePicker.typeFace != null)
((TextView) view).setTypeface(PersianDatePicker.typeFace);
}
}

Expand Down
4 changes: 4 additions & 0 deletions persiandatepicker/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
<attr name="displayMonthNames" format="boolean" />
<attr name="yearRange" format="integer" />
<attr name="displayDescription" format="boolean" />
<attr name="fontFamily"/>
<attr name="background"/>
<attr name="backgroundColor"/>
</declare-styleable>

<attr name="numberPickerTheme" format="reference"/>

</resources>