From 57424fca35e77921a0e780ccfa1702894aac1407 Mon Sep 17 00:00:00 2001 From: Laurent Date: Wed, 27 Feb 2019 20:21:17 +0100 Subject: [PATCH] Split extensions into separate files --- .../android/ui/view/RowRepresentable.kt | 2 +- .../android/util/DateExtension.kt | 81 +++++++++++ .../pokeranalytics/android/util/Extensions.kt | 137 ------------------ .../android/util/NumbersExtension.kt | 32 ++++ .../android/util/UIExtensions.kt | 29 ++++ 5 files changed, 143 insertions(+), 138 deletions(-) create mode 100644 app/src/main/java/net/pokeranalytics/android/util/DateExtension.kt delete mode 100644 app/src/main/java/net/pokeranalytics/android/util/Extensions.kt create mode 100644 app/src/main/java/net/pokeranalytics/android/util/NumbersExtension.kt create mode 100644 app/src/main/java/net/pokeranalytics/android/util/UIExtensions.kt diff --git a/app/src/main/java/net/pokeranalytics/android/ui/view/RowRepresentable.kt b/app/src/main/java/net/pokeranalytics/android/ui/view/RowRepresentable.kt index 44db8bce..1fad8382 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/view/RowRepresentable.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/view/RowRepresentable.kt @@ -12,7 +12,7 @@ interface RowRepresentable : Displayable { fun getDisplayName(): String { return "UNKNOWN NAME" } - + } /** diff --git a/app/src/main/java/net/pokeranalytics/android/util/DateExtension.kt b/app/src/main/java/net/pokeranalytics/android/util/DateExtension.kt new file mode 100644 index 00000000..c50f4757 --- /dev/null +++ b/app/src/main/java/net/pokeranalytics/android/util/DateExtension.kt @@ -0,0 +1,81 @@ +package net.pokeranalytics.android.util + +import java.text.DateFormat +import java.text.SimpleDateFormat +import java.util.* + + +// Calendar + +// Return if the calendar dates are in the same month +fun Calendar.isSameMonth(calendar: Calendar): Boolean { + return calendar.get(Calendar.YEAR) == this.get(Calendar.YEAR) && + calendar.get(Calendar.MONTH) == this.get(Calendar.MONTH) +} +// Return if the calendar dates are in the same day +fun Calendar.isSameDay(calendar: Calendar): Boolean { + return calendar.get(Calendar.YEAR) == this.get(Calendar.YEAR) && + calendar.get(Calendar.MONTH) == this.get(Calendar.MONTH) && + calendar.get(Calendar.DAY_OF_MONTH) == this.get(Calendar.DAY_OF_MONTH) +} + +// Date + +// Return a short string of the date +fun Date.shortDate(): String { + return DateFormat.getDateInstance(DateFormat.SHORT).format(this) +} +// Return a short string of the date +fun Date.mediumDate(): String { + return DateFormat.getDateInstance(DateFormat.MEDIUM).format(this) +} +// Return a long string of the date +fun Date.longDate(): String { + return DateFormat.getDateInstance(DateFormat.LONG).format(this) +} +// Return a short string of the date +fun Date.fullDate(): String { + return DateFormat.getDateInstance(DateFormat.FULL).format(this) +} + +// Return a short string of the date & time +fun Date.shortDateTime(): String { + return DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(this) +} +// Return a medium string of the date & time +fun Date.mediumDateTime(): String { + return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(this) +} +// Return a long string of the date & time +fun Date.longDateTime(): String { + return DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(this) +} +// Return the full string of the date & time +fun Date.fullDateTime(): String { + return DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(this) +} +// Return the day number of the date +fun Date.getDayNumber() : String { + return SimpleDateFormat("dd", Locale.getDefault()).format(this) +} +// Return the 3 first letters of the date's day +fun Date.getShortDayName() : String { + return SimpleDateFormat("EE", Locale.getDefault()).format(this).substring(0, 3) +} +// Return the month & year of the date +fun Date.getMonthAndYear(): String { + return SimpleDateFormat("MMMM YYYY", Locale.getDefault()).format(this).capitalize() +} + +// Return the duration between two dates +fun Date.getDuration(toDate: Date) : String { + val difference = (toDate.time - this.time).toInt() + val numOfDays = (difference / (1000 * 60 * 60 * 24)) + val hours = (difference / (1000 * 60 * 60)) + val minutes = (difference / (1000 * 60)) % 60 + + val hoursStr = if (hours < 10) "0$hours" else "$hours" + val minutesStr = if (minutes < 10) "0$minutes" else "$minutes" + + return "$hoursStr:$minutesStr" +} diff --git a/app/src/main/java/net/pokeranalytics/android/util/Extensions.kt b/app/src/main/java/net/pokeranalytics/android/util/Extensions.kt deleted file mode 100644 index 16388ccd..00000000 --- a/app/src/main/java/net/pokeranalytics/android/util/Extensions.kt +++ /dev/null @@ -1,137 +0,0 @@ -package net.pokeranalytics.android.util - -import android.content.res.Resources -import android.widget.Toast -import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity -import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment -import java.text.DateFormat -import java.text.DecimalFormat -import java.text.NumberFormat -import java.text.SimpleDateFormat -import java.util.* - - - -// Sizes -val Int.dp: Int - get() = (this / Resources.getSystem().displayMetrics.density).toInt() -val Int.px: Int - get() = (this * Resources.getSystem().displayMetrics.density).toInt() -val Float.dp: Float - get() = (this / Resources.getSystem().displayMetrics.density) -val Float.px: Float - get() = (this * Resources.getSystem().displayMetrics.density) - - -// Double - -fun Double.round(): String { - val formatter = DecimalFormat("##.##") - return formatter.format(this) -} - -fun Double.toCurrency(): String { - val format = NumberFormat.getCurrencyInstance() - format.maximumFractionDigits = 2 - format.minimumFractionDigits = 0 - return format.format(this) -} - -// Return the time from milliseconds to hours:minutes -fun Long.toMinutes() : String { - val totalMinutes = this / (1000 * 60) - val hours = totalMinutes / 60 - val minutesLeft = totalMinutes % 60 - var duration = "" - duration += if (hours < 10) "0$hours" else hours.toString() - duration += ":" - duration += if (minutesLeft < 10) "0$minutesLeft" else minutesLeft.toString() - return duration - -} - -// Calendar - -// Return if the calendar dates are in the same month -fun Calendar.isSameMonth(calendar: Calendar): Boolean { - return calendar.get(Calendar.YEAR) == this.get(Calendar.YEAR) && - calendar.get(Calendar.MONTH) == this.get(Calendar.MONTH) -} -// Return if the calendar dates are in the same day -fun Calendar.isSameDay(calendar: Calendar): Boolean { - return calendar.get(Calendar.YEAR) == this.get(Calendar.YEAR) && - calendar.get(Calendar.MONTH) == this.get(Calendar.MONTH) && - calendar.get(Calendar.DAY_OF_MONTH) == this.get(Calendar.DAY_OF_MONTH) -} - - -// Date - -// Return a short string of the date -fun Date.shortDate(): String { - return DateFormat.getDateInstance(DateFormat.SHORT).format(this) -} -// Return a short string of the date -fun Date.mediumDate(): String { - return DateFormat.getDateInstance(DateFormat.MEDIUM).format(this) -} -// Return a long string of the date -fun Date.longDate(): String { - return DateFormat.getDateInstance(DateFormat.LONG).format(this) -} -// Return a short string of the date -fun Date.fullDate(): String { - return DateFormat.getDateInstance(DateFormat.FULL).format(this) -} - -// Return a short string of the date & time -fun Date.shortDateTime(): String { - return DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(this) -} -// Return a medium string of the date & time -fun Date.mediumDateTime(): String { - return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(this) -} -// Return a long string of the date & time -fun Date.longDateTime(): String { - return DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(this) -} -// Return the full string of the date & time -fun Date.fullDateTime(): String { - return DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(this) -} -// Return the day number of the date -fun Date.getDayNumber() : String { - return SimpleDateFormat("dd", Locale.getDefault()).format(this) -} -// Return the 3 first letters of the date's day -fun Date.getShortDayName() : String { - return SimpleDateFormat("EE", Locale.getDefault()).format(this).substring(0, 3) -} -// Return the month & year of the date -fun Date.getMonthAndYear(): String { - return SimpleDateFormat("MMMM YYYY", Locale.getDefault()).format(this).capitalize() -} - -// Return the duration between two dates -fun Date.getDuration(toDate: Date) : String { - val difference = (toDate.time - this.time).toInt() - val numOfDays = (difference / (1000 * 60 * 60 * 24)) - val hours = (difference / (1000 * 60 * 60)) - val minutes = (difference / (1000 * 60)) % 60 - - val hoursStr = if (hours < 10) "0$hours" else "$hours" - val minutesStr = if (minutes < 10) "0$minutes" else "$minutes" - - return "$hoursStr:$minutesStr" -} - -// Toast - -fun PokerAnalyticsActivity.toast(message: String) { - Toast.makeText(this, message, Toast.LENGTH_SHORT).show() -} - -fun PokerAnalyticsFragment.toast(message: String) { - Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show() -} \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/util/NumbersExtension.kt b/app/src/main/java/net/pokeranalytics/android/util/NumbersExtension.kt new file mode 100644 index 00000000..98abbb80 --- /dev/null +++ b/app/src/main/java/net/pokeranalytics/android/util/NumbersExtension.kt @@ -0,0 +1,32 @@ +package net.pokeranalytics.android.util + +import java.text.DecimalFormat +import java.text.NumberFormat + + +// Double + +fun Double.round(): String { + val formatter = DecimalFormat("##.##") + return formatter.format(this) +} + +fun Double.toCurrency(): String { + val format = NumberFormat.getCurrencyInstance() + format.maximumFractionDigits = 2 + format.minimumFractionDigits = 0 + return format.format(this) +} + +// Return the time from milliseconds to hours:minutes +fun Long.toMinutes() : String { + val totalMinutes = this / (1000 * 60) + val hours = totalMinutes / 60 + val minutesLeft = totalMinutes % 60 + var duration = "" + duration += if (hours < 10) "0$hours" else hours.toString() + duration += ":" + duration += if (minutesLeft < 10) "0$minutesLeft" else minutesLeft.toString() + return duration + +} \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/util/UIExtensions.kt b/app/src/main/java/net/pokeranalytics/android/util/UIExtensions.kt new file mode 100644 index 00000000..f663ba53 --- /dev/null +++ b/app/src/main/java/net/pokeranalytics/android/util/UIExtensions.kt @@ -0,0 +1,29 @@ +package net.pokeranalytics.android.util + +import android.content.res.Resources +import android.widget.Toast +import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity +import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment + + +// Sizes +val Int.dp: Int + get() = (this / Resources.getSystem().displayMetrics.density).toInt() +val Int.px: Int + get() = (this * Resources.getSystem().displayMetrics.density).toInt() +val Float.dp: Float + get() = (this / Resources.getSystem().displayMetrics.density) +val Float.px: Float + get() = (this * Resources.getSystem().displayMetrics.density) + + + +// Toast + +fun PokerAnalyticsActivity.toast(message: String) { + Toast.makeText(this, message, Toast.LENGTH_SHORT).show() +} + +fun PokerAnalyticsFragment.toast(message: String) { + Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show() +} \ No newline at end of file