diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/Globals.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/Globals.kt index 84bc29ee703..24e249b9a86 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/Globals.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/Globals.kt @@ -3,12 +3,15 @@ package com.lagradost.cloudstream3.ui.settings import android.content.Context import com.lagradost.cloudstream4.utils.DeviceLayout +// TODO: remove and use DeviceLayout directly. +// this currently just acts as a wrapper to +// avoid changing so much at once. object Globals { var beneneCount = 0 - const val PHONE: Int = DeviceLayout.PHONE - const val TV: Int = DeviceLayout.TV - const val EMULATOR: Int = DeviceLayout.EMULATOR + const val PHONE: Int = 0b00001 + const val TV: Int = 0b00010 + const val EMULATOR: Int = 0b00100 fun Context.updateTv() { DeviceLayout.update() @@ -16,5 +19,5 @@ object Globals { fun isLandscape(): Boolean = DeviceLayout.isLandscape() - fun isLayout(flags: Int): Boolean = DeviceLayout.isLayout(flags) + fun isLayout(flags: Int): Boolean = DeviceLayout.isLayout(DeviceLayout.Layout(flags)) } diff --git a/composeApp/src/androidMain/kotlin/com/lagradost/cloudstream4/utils/DeviceInfo.android.kt b/composeApp/src/androidMain/kotlin/com/lagradost/cloudstream4/utils/DeviceInfo.android.kt index 345bc645ebe..15c9186c5e4 100644 --- a/composeApp/src/androidMain/kotlin/com/lagradost/cloudstream4/utils/DeviceInfo.android.kt +++ b/composeApp/src/androidMain/kotlin/com/lagradost/cloudstream4/utils/DeviceInfo.android.kt @@ -10,8 +10,8 @@ import com.lagradost.cloudstream4.preferences.PreferenceDefaults import com.lagradost.cloudstream4.preferences.PreferenceKeys internal actual object DeviceInfo { - actual fun getDeviceType(): DeviceType { - val context = getContext() as? Context ?: return DeviceType.PHONE + actual fun getDetectedLayout(): DeviceLayout.Layout { + val context = getContext() as? Context ?: return DeviceLayout.PHONE val uiModeManager = context.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager? val isTelevisionMode = uiModeManager?.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION val model = Build.MODEL.lowercase() @@ -20,8 +20,8 @@ internal actual object DeviceInfo { || Build.MODEL.contains("AFT") // AFT = Fire TV || model.contains("firestick") || model.contains("fire tv") - || model.contains("chromecast") -> DeviceType.TV - else -> DeviceType.PHONE + || model.contains("chromecast") -> DeviceLayout.TV + else -> DeviceLayout.PHONE } } diff --git a/composeApp/src/commonMain/kotlin/com/lagradost/cloudstream4/utils/DeviceInfo.kt b/composeApp/src/commonMain/kotlin/com/lagradost/cloudstream4/utils/DeviceInfo.kt index aeb15944fc1..095b580ff90 100644 --- a/composeApp/src/commonMain/kotlin/com/lagradost/cloudstream4/utils/DeviceInfo.kt +++ b/composeApp/src/commonMain/kotlin/com/lagradost/cloudstream4/utils/DeviceInfo.kt @@ -1,11 +1,7 @@ package com.lagradost.cloudstream4.utils internal expect object DeviceInfo { - fun getDeviceType(): DeviceType + fun getDetectedLayout(): DeviceLayout.Layout fun isLandscape(): Boolean fun getLayoutPreference(): Int } - -enum class DeviceType { - PHONE, TV, EMULATOR, COMPUTER -} diff --git a/composeApp/src/commonMain/kotlin/com/lagradost/cloudstream4/utils/DeviceLayout.kt b/composeApp/src/commonMain/kotlin/com/lagradost/cloudstream4/utils/DeviceLayout.kt index 6d82b904780..a89fb292639 100644 --- a/composeApp/src/commonMain/kotlin/com/lagradost/cloudstream4/utils/DeviceLayout.kt +++ b/composeApp/src/commonMain/kotlin/com/lagradost/cloudstream4/utils/DeviceLayout.kt @@ -1,12 +1,21 @@ package com.lagradost.cloudstream4.utils +import com.lagradost.cloudstream4.preferences.PreferenceDefaults +import kotlin.jvm.JvmInline + object DeviceLayout { - const val PHONE: Int = 0b00001 - const val TV: Int = 0b00010 - const val EMULATOR: Int = 0b00100 - const val COMPUTER: Int = 0b01000 + @JvmInline // This still works but has no affect on non-JVM targets + value class Layout(private val value: Int) { + infix fun or(other: Layout) = Layout(value or other.value) + internal fun and(other: Layout) = (value and other.value) != 0 + } + + val PHONE = Layout(0b00001) + val TV = Layout(0b00010) + val EMULATOR = Layout(0b00100) + val COMPUTER = Layout(0b01000) - private var layoutId = -1 + private var layoutId = Layout(PreferenceDefaults.APP_LAYOUT) // TODO when fully on Compose // private val layoutId: Int get() = resolveLayout() @@ -19,7 +28,7 @@ object DeviceLayout { * * Valid flags are: PHONE, TV, EMULATOR, or COMPUTER */ - fun isLayout(flags: Int): Boolean = (layoutId and flags) != 0 + fun isLayout(flags: Layout): Boolean = layoutId.and(flags) /** Returns true if the current orientation is landscape. */ fun isLandscape(): Boolean = @@ -36,14 +45,9 @@ object DeviceLayout { layoutId = resolveLayout() } - private fun resolveLayout(): Int { + private fun resolveLayout(): Layout { return when (DeviceInfo.getLayoutPreference()) { - -1 -> when (DeviceInfo.getDeviceType()) { - DeviceType.COMPUTER -> COMPUTER - DeviceType.EMULATOR -> EMULATOR - DeviceType.PHONE -> PHONE - DeviceType.TV -> TV - } + PreferenceDefaults.APP_LAYOUT -> DeviceInfo.getDetectedLayout() 0 -> PHONE 1 -> TV 2 -> EMULATOR diff --git a/composeApp/src/jvmMain/kotlin/com/lagradost/cloudstream4/utils/DeviceInfo.jvm.kt b/composeApp/src/jvmMain/kotlin/com/lagradost/cloudstream4/utils/DeviceInfo.jvm.kt index 93ffcdd0255..c07067cca76 100644 --- a/composeApp/src/jvmMain/kotlin/com/lagradost/cloudstream4/utils/DeviceInfo.jvm.kt +++ b/composeApp/src/jvmMain/kotlin/com/lagradost/cloudstream4/utils/DeviceInfo.jvm.kt @@ -1,9 +1,10 @@ package com.lagradost.cloudstream4.utils +import com.lagradost.cloudstream4.preferences.PreferenceDefaults import java.awt.Toolkit internal actual object DeviceInfo { - actual fun getDeviceType(): DeviceType = DeviceType.COMPUTER + actual fun getDetectedLayout(): DeviceLayout.Layout = DeviceLayout.COMPUTER actual fun isLandscape(): Boolean { return try { @@ -14,5 +15,5 @@ internal actual object DeviceInfo { } } - actual fun getLayoutPreference(): Int = -1 + actual fun getLayoutPreference(): Int = PreferenceDefaults.APP_LAYOUT }