Skip to content

Commit 8d54952

Browse files
committed
refactor: move usingStyledAttributes
The name is still the same to mimic the default method `withStyledAttributes`
1 parent 6496d85 commit 8d54952

File tree

5 files changed

+49
-32
lines changed

5 files changed

+49
-32
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/preferences/ExtendedPreferenceCategory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import androidx.core.view.isVisible
2222
import androidx.preference.PreferenceCategory
2323
import androidx.preference.PreferenceViewHolder
2424
import com.ichi2.anki.R
25+
import com.ichi2.anki.utils.ext.usingStyledAttributes
2526
import com.ichi2.anki.utils.openUrl
26-
import com.ichi2.preferences.usingStyledAttributes
2727

2828
/**
2929
* Extended version of [androidx.preference.PreferenceCategory] with the extra attributes:

AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/permissions/PermissionItem.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import androidx.appcompat.widget.SwitchCompat
2525
import androidx.core.content.withStyledAttributes
2626
import com.google.android.material.color.MaterialColors
2727
import com.ichi2.anki.R
28-
import com.ichi2.preferences.usingStyledAttributes
28+
import com.ichi2.anki.utils.ext.usingStyledAttributes
2929
import com.ichi2.ui.FixedTextView
3030
import com.ichi2.utils.Permissions
3131
import timber.log.Timber

AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/AnswerButton.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import androidx.core.text.buildSpannedString
2222
import androidx.core.text.inSpans
2323
import com.google.android.material.button.MaterialButton
2424
import com.ichi2.anki.R
25-
import com.ichi2.preferences.usingStyledAttributes
25+
import com.ichi2.anki.utils.ext.usingStyledAttributes
2626

2727
class AnswerButton
2828
@JvmOverloads
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2025 Brayan Oliveira <69634269+brayandso@users.noreply.github.con>
3+
*
4+
* This program is free software; you can redistribute it and/or modify it under
5+
* the terms of the GNU General Public License as published by the Free Software
6+
* Foundation; either version 3 of the License, or (at your option) any later
7+
* version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
10+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11+
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License along with
14+
* this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
package com.ichi2.anki.utils.ext
17+
18+
import android.content.Context
19+
import android.content.res.TypedArray
20+
import android.util.AttributeSet
21+
import androidx.annotation.AttrRes
22+
import androidx.annotation.StyleRes
23+
import kotlin.contracts.ExperimentalContracts
24+
import kotlin.contracts.InvocationKind
25+
import kotlin.contracts.contract
26+
27+
/**
28+
* Run a [block] on a [TypedArray] receiver that is recycled at the end.
29+
* @return The return value of the block.
30+
*
31+
* @see android.content.res.Resources.Theme.obtainStyledAttributes
32+
* @see androidx.core.content.withStyledAttributes
33+
*/
34+
@OptIn(ExperimentalContracts::class)
35+
inline fun <T> Context.usingStyledAttributes(
36+
set: AttributeSet?,
37+
attrs: IntArray,
38+
@AttrRes defStyleAttr: Int = 0,
39+
@StyleRes defStyleRes: Int = 0,
40+
block: TypedArray.() -> T,
41+
): T {
42+
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
43+
44+
val typedArray = obtainStyledAttributes(set, attrs, defStyleAttr, defStyleRes)
45+
return typedArray.block().also { typedArray.recycle() }
46+
}

AnkiDroid/src/main/java/com/ichi2/preferences/PreferenceUtil.kt

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,9 @@
1414

1515
package com.ichi2.preferences
1616

17-
import android.content.Context
18-
import android.content.res.TypedArray
19-
import android.util.AttributeSet
20-
import androidx.annotation.AttrRes
21-
import androidx.annotation.StyleRes
2217
import androidx.fragment.app.DialogFragment
2318
import androidx.fragment.app.Fragment
2419
import androidx.preference.PreferenceFragmentCompat
25-
import kotlin.contracts.ExperimentalContracts
26-
import kotlin.contracts.InvocationKind
27-
import kotlin.contracts.contract
2820

2921
/**
3022
* By implementing this interface, a preference can specify which dialog fragment is opened on click.
@@ -40,24 +32,3 @@ interface DialogFragmentProvider {
4032
*/
4133
fun makeDialogFragment(): DialogFragment?
4234
}
43-
44-
/**
45-
* Run a [block] on a [TypedArray] receiver that is recycled at the end.
46-
* @return The return value of the block.
47-
*
48-
* @see android.content.res.Resources.Theme.obtainStyledAttributes
49-
* @see androidx.core.content.withStyledAttributes
50-
*/
51-
@OptIn(ExperimentalContracts::class)
52-
inline fun <T> Context.usingStyledAttributes(
53-
set: AttributeSet?,
54-
attrs: IntArray,
55-
@AttrRes defStyleAttr: Int = 0,
56-
@StyleRes defStyleRes: Int = 0,
57-
block: TypedArray.() -> T,
58-
): T {
59-
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
60-
61-
val typedArray = obtainStyledAttributes(set, attrs, defStyleAttr, defStyleRes)
62-
return typedArray.block().also { typedArray.recycle() }
63-
}

0 commit comments

Comments
 (0)