GitNex-Android-App/app/src/main/java/org/mian/gitnex/helpers/TimeHelper.java

94 lines
2.2 KiB
Java
Raw Normal View History

package org.mian.gitnex.helpers;
import android.content.Context;
import org.mian.gitnex.R;
import org.ocpsoft.prettytime.PrettyTime;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* Author M M Arif
*/
public class TimeHelper {
public static String customDateFormatForToast(String customDate) {
String[] parts = customDate.split("\\+");
String part1 = parts[0] + "Z";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
Date createdTime = null;
try {
createdTime = formatter.parse(part1);
} catch(ParseException ignored) {}
DateFormat format = DateFormat.getDateTimeInstance();
assert createdTime != null;
return format.format(createdTime);
}
public static String formatTime(Date date, Locale locale, String timeFormat, Context context) {
if(date != null) {
switch(timeFormat) {
case "pretty": {
PrettyTime prettyTime = new PrettyTime(locale);
return prettyTime.format(date);
}
case "normal": {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd '" + context.getResources().getString(R.string.timeAtText) + "' HH:mm", locale);
return formatter.format(date);
}
case "normal1": {
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy '" + context.getResources().getString(R.string.timeAtText) + "' HH:mm", locale);
return formatter.format(date);
}
}
}
return "";
}
public static String customDateFormatForToastDateFormat(Date customDate) {
DateFormat format = DateFormat.getDateTimeInstance();
return format.format(customDate);
}
Add option to change times that are used to switch to light/dark theme (#932) ### Describe what your pull request does and which issue you’re targeting Closes #928 This allows to select the time to switch between themes. The mentioned color/contrast problem with the time selection isn't fixed. I also simplified the settings code (https://codeberg.org/qwerty287/GitNex/commit/0fe854fd49b0ba1ee0ac73f1ac1a50b7a4ca955a). And with [this](https://codeberg.org/qwerty287/GitNex/commit/a590f5bc4cc0325fa57e0d65a1e6b00582d90884) GitNex hides the two options to change the time if your theme isn't `Auto (Light/Dark)` or `Auto (Retro/Dark)`. I could apply this also to the notifications settings, that means that the advanced settings for notifications (polling delay, light...) are hidden if the notifications aren't enabled. this is ready to review/merge <br><br> <!-- Make sure you are targeting the "main" branch, pull requests on release branches are only allowed for bug fixes. --> - [X] I carefully read the [contribution guidelines](https://codeberg.org/GitNex/GitNex/src/branch/main/CONTRIBUTING.md). - [X] I'm following the code standards as defined [here](https://codeberg.org/gitnex/GitNex/wiki/Code-Standards). - [X] By submitting this pull request, I permit GitNex to license my work under the [GNU General Public License v3](https://codeberg.org/GitNex/GitNex/src/branch/main/LICENSE). Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@swatian.com> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/932 Reviewed-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2021-08-02 18:59:30 +02:00
public static boolean timeBetweenHours(int fromHour, int toHour, int fromMinute, int toMinute) {
Calendar cal = Calendar.getInstance();
Calendar from = Calendar.getInstance();
from.set(Calendar.HOUR_OF_DAY, fromHour);
Add option to change times that are used to switch to light/dark theme (#932) ### Describe what your pull request does and which issue you’re targeting Closes #928 This allows to select the time to switch between themes. The mentioned color/contrast problem with the time selection isn't fixed. I also simplified the settings code (https://codeberg.org/qwerty287/GitNex/commit/0fe854fd49b0ba1ee0ac73f1ac1a50b7a4ca955a). And with [this](https://codeberg.org/qwerty287/GitNex/commit/a590f5bc4cc0325fa57e0d65a1e6b00582d90884) GitNex hides the two options to change the time if your theme isn't `Auto (Light/Dark)` or `Auto (Retro/Dark)`. I could apply this also to the notifications settings, that means that the advanced settings for notifications (polling delay, light...) are hidden if the notifications aren't enabled. this is ready to review/merge <br><br> <!-- Make sure you are targeting the "main" branch, pull requests on release branches are only allowed for bug fixes. --> - [X] I carefully read the [contribution guidelines](https://codeberg.org/GitNex/GitNex/src/branch/main/CONTRIBUTING.md). - [X] I'm following the code standards as defined [here](https://codeberg.org/gitnex/GitNex/wiki/Code-Standards). - [X] By submitting this pull request, I permit GitNex to license my work under the [GNU General Public License v3](https://codeberg.org/GitNex/GitNex/src/branch/main/LICENSE). Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@swatian.com> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/932 Reviewed-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2021-08-02 18:59:30 +02:00
from.set(Calendar.MINUTE, fromMinute);
Calendar to = Calendar.getInstance();
to.set(Calendar.HOUR_OF_DAY, toHour);
Add option to change times that are used to switch to light/dark theme (#932) ### Describe what your pull request does and which issue you’re targeting Closes #928 This allows to select the time to switch between themes. The mentioned color/contrast problem with the time selection isn't fixed. I also simplified the settings code (https://codeberg.org/qwerty287/GitNex/commit/0fe854fd49b0ba1ee0ac73f1ac1a50b7a4ca955a). And with [this](https://codeberg.org/qwerty287/GitNex/commit/a590f5bc4cc0325fa57e0d65a1e6b00582d90884) GitNex hides the two options to change the time if your theme isn't `Auto (Light/Dark)` or `Auto (Retro/Dark)`. I could apply this also to the notifications settings, that means that the advanced settings for notifications (polling delay, light...) are hidden if the notifications aren't enabled. this is ready to review/merge <br><br> <!-- Make sure you are targeting the "main" branch, pull requests on release branches are only allowed for bug fixes. --> - [X] I carefully read the [contribution guidelines](https://codeberg.org/GitNex/GitNex/src/branch/main/CONTRIBUTING.md). - [X] I'm following the code standards as defined [here](https://codeberg.org/gitnex/GitNex/wiki/Code-Standards). - [X] By submitting this pull request, I permit GitNex to license my work under the [GNU General Public License v3](https://codeberg.org/GitNex/GitNex/src/branch/main/LICENSE). Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@swatian.com> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/932 Reviewed-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2021-08-02 18:59:30 +02:00
to.set(Calendar.MINUTE, toMinute);
if(to.before(from)) {
if(cal.after(to)) {
to.add(Calendar.DATE, 1);
} else {
from.add(Calendar.DATE, -1);
}
}
return cal.after(from) && cal.before(to);
}
}