Bimba.git

ref: 343255d5c2e7108dd510ca9e7707d2d3d67e0bc6

app/src/main/java/xyz/apiote/bimba/czwek/units/UnitSystem.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
package xyz.apiote.bimba.czwek.units

import android.content.Context
import android.icu.util.LocaleData
import android.icu.util.LocaleData.MeasurementSystem
import android.icu.util.ULocale
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.preference.PreferenceManager
import java.util.Locale

abstract class UnitSystem(val base: Int) {
	companion object {
		@RequiresApi(Build.VERSION_CODES.P)
		private fun forMeasureSystem(ms: MeasurementSystem) =
			when (ms) {
				MeasurementSystem.SI -> {
					Metric
				}

				MeasurementSystem.UK -> {
					Imperial
				}

				MeasurementSystem.US -> {
					USCustomary
				}

				else -> {
					Metric
				}
			}

		private fun forLocale(country: String): UnitSystem = if (setOf(
				"AG", "BS", "BZ", "DM", "GD", "MH", "FM", "PW", "KN", "LC", "VC", "GB", "AI", "VG",
				"IO", "KY", "FK", "MS", "SH", "TC", "GG", "IM", "JE", "US", "AS", "GU", "MP", "PR", "VI"
			).contains(country)
		) {
			Imperial
		} else {
			Metric
		}

		private fun getDefault() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
			forMeasureSystem(LocaleData.getMeasurementSystem(ULocale.forLocale(Locale.getDefault())))
		} else {
			forLocale(Locale.getDefault().country)
		}

		fun getSelected(context: Context): UnitSystem =
			when (PreferenceManager.getDefaultSharedPreferences(context)
				.getString("unit_system", "default")) {
				"default" -> getDefault()
				"metric" -> Metric
				"imperial" -> Imperial
				"customary" -> USCustomary
				"tgm10" -> TGM(10)
				"tgm12" -> TGM(12)
				else -> getDefault()
			}
	}

	abstract fun timeUnit(count: Long): TimeUnit
	abstract fun timeUnit(other: TimeUnit): TimeUnit
	abstract fun speedUnit(count: Double): SpeedUnit
	abstract fun speedUnit(other: SpeedUnit): SpeedUnit
	abstract fun distanceUnit(count: Double): DistanceUnit
	abstract fun distanceUnit(other: DistanceUnit): DistanceUnit

	abstract fun toString(context: Context, s: SpeedUnit): String
	abstract fun toString(context: Context, t: TimeUnit): String
	abstract fun toString(context: Context, d: DistanceUnit): String
}

interface TimeUnit {
	fun milliseconds(): Long
	fun toString(context: Context, base: Int): String
	fun contentDescription(context: Context, base: Int): String
}

interface SpeedUnit {
	fun mps(): Double
	fun toString(context: Context, base: Int): String
	fun contentDescription(context: Context, base: Int): String
}

interface DistanceUnit {
	fun meters(): Double
	fun toString(context: Context, base: Int): String
	fun contentDescription(context: Context, base: Int): String
}