ref: v2.0-beta2
app/src/main/java/ml/adamsprogs/bimba/extensions.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
package ml.adamsprogs.bimba import android.annotation.SuppressLint import android.content.Context import android.graphics.drawable.Drawable import android.os.Build import ml.adamsprogs.bimba.activities.StopActivity import java.io.* import java.text.SimpleDateFormat import java.util.* import kotlin.collections.ArrayList internal fun Calendar.rollTime(seconds: Int): Calendar { val hour = seconds / 3600 val minute = (seconds % 3600) / 60 val second = (seconds % 60) this.set(Calendar.HOUR_OF_DAY, hour) this.set(Calendar.MINUTE, minute) this.set(Calendar.SECOND, second) this.set(Calendar.MILLISECOND, 0) return this } internal fun Calendar.secondsAfterMidnight(): Int { val hour = this.get(Calendar.HOUR_OF_DAY) val minute = this.get(Calendar.MINUTE) val second = this.get(Calendar.SECOND) return hour * 3600 + minute * 60 + second } internal fun Calendar.toIsoDate(): String { val year = this.get(Calendar.YEAR) val month = String.format("%02d", this.get(Calendar.MONTH) + 1) val day = String.format("%02d", this.get(Calendar.DAY_OF_MONTH)) return "$year$month$day" } const val ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" const val ISO_8601_DATE_ONLY_FORMAT = "yyyyMMdd" @SuppressLint("SimpleDateFormat") fun calendarFromIso(iso: String): Calendar { val calendar = Calendar.getInstance() val dateFormat = SimpleDateFormat(ISO_8601_DATE_FORMAT) val date = dateFormat.parse(iso) calendar.time = date return calendar } @SuppressLint("SimpleDateFormat") fun calendarFromIsoD(iso: String): Calendar { val calendar = Calendar.getInstance() val dateFormat = SimpleDateFormat(ISO_8601_DATE_ONLY_FORMAT) val date = dateFormat.parse(iso) calendar.time = date return calendar } fun getColour(id: Int, context: Context): Int { @Suppress("DEPRECATION") (return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) context.resources.getColor(id, null) else context.resources.getColor(id)) } fun getDrawable(id: Int, context: Context): Drawable { @Suppress("DEPRECATION") (return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) context.resources.getDrawable(id, null) else context.resources.getDrawable(id)) } internal fun Calendar.getMode(): Int { return when (this.get(Calendar.DAY_OF_WEEK)) { Calendar.SUNDAY -> StopActivity.MODE_SUNDAYS Calendar.SATURDAY -> StopActivity.MODE_SATURDAYS else -> StopActivity.MODE_WORKDAYS } } internal fun CharSequence.safeSplit(vararg delimiters: String, ignoreCase: Boolean = false, limit: Int = 0): List<String> { if (this == "") return ArrayList() return this.split(*delimiters, ignoreCase = ignoreCase, limit = limit) } internal fun Context.getSecondaryExternalFilesDir(): File { val dirs = this.getExternalFilesDirs(null) return dirs[0] // return dirs[dirs.size - 1] } internal fun InputStream.listenableCopyTo(out: OutputStream, bufferSize: Int = DEFAULT_BUFFER_SIZE, listener: (Long) -> Unit): Long { //fixme may devour RAM? var bytesCopied: Long = 0 val buffer = ByteArray(bufferSize) var bytes = read(buffer) while (bytes >= 0) { out.write(buffer, 0, bytes) bytesCopied += bytes listener(bytesCopied) bytes = read(buffer) } return bytesCopied } |