Skip to content

Commit

Permalink
refactor: Rename CompatV23 to BaseCompat
Browse files Browse the repository at this point in the history
When `minSdk` is bumped to 24, we use the same file, so there are less
changes in the commit, compared to moving all definitions to CompatV24.kt
  • Loading branch information
Arthur-Milchior authored and mikehardy committed Nov 11, 2024
1 parent 15b748a commit 61fb493
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ import java.io.Serializable
import java.util.Locale
import kotlin.time.Duration

/** Baseline implementation of [Compat]. Check [Compat]'s for more detail. */
/** Baseline implementation of [Compat], for API V23. Check [Compat] for more detail. */
@KotlinCleanup("add extension method logging file.delete() failure" + "Fix Deprecation")
@Suppress("Deprecation")
open class CompatV23 : Compat {
open class BaseCompat : Compat {
// Until API26, ignore notification channels
override fun setupNotificationChannel(context: Context) { /* pre-API26, do nothing */
}
Expand Down Expand Up @@ -322,3 +322,5 @@ open class CompatV23 : Compat {
}
}
}

typealias CompatV23 = BaseCompat
30 changes: 16 additions & 14 deletions AnkiDroid/src/main/java/com/ichi2/compat/Compat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import android.graphics.Bitmap.CompressFormat
import android.media.MediaRecorder
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.view.KeyboardShortcutGroup
import android.view.View
import androidx.annotation.CheckResult
Expand All @@ -50,18 +51,19 @@ import kotlin.time.Duration
*
* A set of implementations for the supported platforms are available.
*
*
* Each implementation ends with a `V<n>` suffix, identifying the minimum API version on which this implementation
* can be used. For example, see [CompatV23].
* BaseCompat is the implementation for our current [minSdk]. It is overridden by `CompatV<n>`,
* identifying the minimum API version on which this implementation
* can be used. For example, see [CompatV33].
*
*
* Each implementation `CompatVn` should extend the implementation `CompatVm` for the greatest m<n such that `CompatVm`
* exists. E.g. as of July 2021 `CompatV23` extends `CompatV21` because there is no `CompatV22`.
* If `CompatV22` were to be created one day, it will extends `CompatV22` and be extended by `CompatV23`.
* exists, or BaseCompat if no such `m` exists.
* E.g. as of November 24 `CompatV29` extends `CompatV26` because there is no `CompatV27` or `CompatV28`.
* If `CompatV27` were to be created one day, it will extends `CompatV26` and be extended by `CompatV29`.
*
*
* Each method `method` must be implemented in the lowest Compat implementation (right now `CompatV21`, but
* it will change when min sdk change). It must also be implemented in `CompatVn` if, in version `n` and higher,
* Each method `method` must be implemented in [BaseCompat] .
* It must also be implemented in `CompatVn` if, in version `n` and higher,
* a different implementation must be used. This can be done either because some method used in the API `n` got
* deprecated, changed its behavior, or because the implementation of `method` can be more efficient.
*
Expand All @@ -72,13 +74,13 @@ import kotlin.time.Duration
* notification channels were introduced in API 26.
*
*
* Example: `CompatV26` extends `CompatV23` which extends `CompatV21`. The method `vibrate` is
* defined in `CompatV21` where only the number of seconds of vibration is taken into consideration, and is
* redefined in `CompatV26` - using `@Override` - where the style of vibration is also taken into
* consideration. It means that on devices using APIs 21 to 25 included, the implementation of `CompatV21` is
* used, and on devices using API 26 and higher, the implementation of `CompatV26` is used.
* On the other hand a method like `setTime` that got defined in `CompatV21` and redefined in
* `CompatV23` due to a change of API, need not be implemented again in CompatV26.
* Example: [CompatV26] extends [CompatV24] which extends [BaseCompat]. The method `vibrate` is
* defined in [BaseCompat] where only the number of seconds of vibration is taken into consideration, and is
* redefined in [CompatV26] - using `@Override` - where the style of vibration is also taken into
* consideration. It means that on devices using APIs 23 to 25 included, the implementation of [BaseCompat] is
* used, and on devices using API 26 and higher, the implementation of [CompatV26] is used.
* On the other hand a method like [Compat.saveImage] that got defined in [BaseCompat] and redefined in
* [CompatV29] in order to use [Environment.DIRECTORY_PICTURES], need not be implemented again in [CompatV26].
*/
interface Compat {
fun setupNotificationChannel(context: Context)
Expand Down
4 changes: 1 addition & 3 deletions AnkiDroid/src/main/java/com/ichi2/compat/CompatHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.ichi2.compat
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.Intent.CATEGORY_DEFAULT
import android.content.IntentFilter
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
Expand All @@ -32,7 +31,6 @@ import android.view.KeyEvent.KEYCODE_PAGE_UP
import android.view.View
import androidx.appcompat.widget.TooltipCompat
import androidx.core.content.ContextCompat
import com.ichi2.compat.CompatHelper.Companion.compat
import java.io.Serializable

/**
Expand All @@ -54,7 +52,7 @@ class CompatHelper private constructor() {
sdkVersion >= Build.VERSION_CODES.Q -> CompatV29()
sdkVersion >= Build.VERSION_CODES.O -> CompatV26()
sdkVersion >= Build.VERSION_CODES.N -> CompatV24()
else -> CompatV23()
else -> BaseCompat()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CompatDeleteFileTest(
@JvmStatic // required for Parameters
@Parameterized.Parameters(name = "{1}")
fun data(): Iterable<Array<Any>> = sequence {
yield(arrayOf(CompatV23(), "CompatV23"))
yield(arrayOf(BaseCompat(), "BaseCompat"))
yield(arrayOf(CompatV26(), "CompatV26"))
}.asIterable()
}
Expand Down
6 changes: 3 additions & 3 deletions AnkiDroid/src/test/java/com/ichi2/compat/Test21And26.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ abstract class Test21And26 {
@JvmStatic // required for Parameters
@Parameterized.Parameters(name = "{1}")
fun data(): Iterable<Array<Any>> = sequence {
yield(arrayOf(CompatV23(), "CompatV23"))
yield(arrayOf(BaseCompat(), "BaseCompat"))
yield(arrayOf(CompatV26(), "CompatV26"))
}.asIterable()

Expand Down Expand Up @@ -75,8 +75,8 @@ abstract class Test21And26 {
/** Used in the "Test Results" Window */
lateinit var unitTestDescription: String

val isV23: Boolean
get() = compat is CompatV23
val isBaseCompat: Boolean
get() = compat is BaseCompat
val isV26: Boolean
get() = compat is CompatV26

Expand Down

0 comments on commit 61fb493

Please sign in to comment.