Bimba.git

ref: 16c836581cf303adc064752dba313811be75e0da

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
package ml.adamsprogs.bimba

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import ml.adamsprogs.bimba.activities.StopActivity
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList

internal fun String.toPascalCase(): String { //check
    val builder = StringBuilder(this)
    var isLastCharSeparator = true
    builder.forEach {
        isLastCharSeparator = if ((it in 'a'..'z' || it in 'A'..'Z') && isLastCharSeparator) {
            it.toUpperCase()
            false
        } else
            true
    }
    return builder.toString()
}

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
}

const val ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

fun calendarFromIso(iso: String): Calendar { // check
    val calendar = Calendar.getInstance()
    val dateFormat = SimpleDateFormat(ISO_8601_DATE_FORMAT)
    val date = dateFormat.parse(iso)
    //date.hours = date.getHours() - 1 //fixme why?
    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)
}