Skip to content
Open
Show file tree
Hide file tree
Changes from 120 commits
Commits
Show all changes
132 commits
Select commit Hold shift + click to select a range
5480ea8
Add basic Mighty chain implementation
Vylantze Aug 23, 2025
c359a88
Use the rearranged cards as the baseline
Vylantze Aug 23, 2025
fb9e316
Minor refactor to simplify it
Vylantze Aug 23, 2025
7a8fc72
Fix bug where justRearranged was not calling the correct function (ca…
Vylantze Aug 24, 2025
e88f215
Fix not checking for NP in the first card check. Add unit tests
Vylantze Aug 25, 2025
05f1524
Fix rearrange for mighty chain and add tests.
Vylantze Aug 26, 2025
56885bc
Add handling for if third card is invalid and it is not an NP use (it…
Vylantze Aug 26, 2025
fb2bef6
Add recommended fix by copilot for easier readability
Vylantze Aug 26, 2025
4f158c9
Rename and add more comments test names to make them clearer
Vylantze Aug 26, 2025
553985c
Add np matcher test code
Vylantze Aug 28, 2025
9aa0036
Update images used for checking.
Vylantze Aug 28, 2025
44125fb
Refactor Mighty Chain logic to different file
Vylantze Aug 28, 2025
690d56d
Add more unit tests
Vylantze Aug 28, 2025
6826203
Remove unnecessary additions
Vylantze Aug 28, 2025
f83166e
Update minimum similarity score to 0.5 (since 0.9 is the matching)
Vylantze Aug 28, 2025
120f774
Refactor: Move ApplyMightyChains to `attack` package
Vylantze Aug 29, 2025
322970c
Test: Update `MightyChainTest` to use `AttackLineUps` and clarify tes…
Vylantze Aug 29, 2025
9ca62de
Test: Extract card lineups for attack tests
Vylantze Aug 29, 2025
728252d
Make MightyChainTest use AttackLineUps for all test cases
Vylantze Aug 29, 2025
b69203e
Refactor test file location
Vylantze Aug 29, 2025
e876b05
Merge branch 'refactor-mighty' into basic-mighty-chain
Vylantze Aug 29, 2025
e5eaea6
Add chain priority toggle
Vylantze Aug 29, 2025
0f75876
Add rendering for Chain Priority
Vylantze Aug 29, 2025
29e0f28
Change Brave Chain text to reflect when it is being used as Chain Pri…
Vylantze Aug 29, 2025
5581ad6
Add ChainPrioritySelector
Vylantze Aug 29, 2025
d507a52
Add chainPriority to functions
Vylantze Aug 29, 2025
357e3f0
Go back to using Brave Chain to determine if Brave Chains should be u…
Vylantze Aug 30, 2025
cb5b70e
Make changes/fixes recommended by Copilot
Vylantze Aug 30, 2025
6342e7e
Apply the NP rearrange logic to the generic rearrange function instea…
Vylantze Aug 30, 2025
48eeb94
Split tests so that MightyChainTest can have its own proper unit test…
Vylantze Aug 30, 2025
44597da
Merge branch 'basic-mighty-chain' into create-chain-priority-selector
Vylantze Aug 30, 2025
9da4321
Add 'Avoid' in ChainType. Add automatic 'disabling' of Chain types wh…
Vylantze Aug 30, 2025
02a50c8
Remove MightyChain from BraveChain and add new Attack handling system
Vylantze Aug 31, 2025
9f81a22
Remove WithNPMighty option from BraveChainEnum
Vylantze Aug 31, 2025
0cd8dc1
Add localization for ChainTypeEnums
Vylantze Aug 31, 2025
22b5952
Remove unnecessary imports from Render
Vylantze Sep 1, 2025
3c8191e
Refactor MightyChainTest to have more consistent asserts
Vylantze Sep 1, 2025
19a92e8
Move some functions out of BraveChainHandler into Utils. Write tests …
Vylantze Sep 1, 2025
2bff158
Add CardType mapping function in Utils
Vylantze Sep 1, 2025
1399922
Add class and tests for avoiding chains.
Vylantze Sep 1, 2025
3acaab7
Filter out unknown cards, since they will be generally unable to be r…
Vylantze Sep 1, 2025
ba6fc7a
Refactor names of classes
Vylantze Sep 1, 2025
29771a4
Remove AttackChainInterface since all chains have really specific req…
Vylantze Sep 2, 2025
ba12b90
Add Always for Brave Chain. Refactor to pass the heavier utility mapp…
Vylantze Sep 2, 2025
05a177b
Add ColorChain handling. Make sure logic falls to BraveChain if Avoid…
Vylantze Sep 2, 2025
8e3ee9c
Add handling for how to treat 'Avoid'
Vylantze Sep 2, 2025
cf30d3d
Account for Avoid index being 0 just in case
Vylantze Sep 2, 2025
79f1955
Return the result of the new cards
Vylantze Sep 2, 2025
41a67a9
Fix bug in BraveChain where if there was 2 Brave Chains and one of th…
Vylantze Sep 2, 2025
4c7331a
Merge branch 'create-chain-priority-selector' into basic-mighty-chain
Vylantze Sep 2, 2025
48f9d2e
Clean up unused variables. Make sure that Always uses the correct loc…
Vylantze Sep 3, 2025
0ec0101
Hide 'Always' unless Chain Priority is on
Vylantze Sep 3, 2025
390fa42
Add caching for MightChain checks
Vylantze Sep 3, 2025
50a1ab0
Change logic of AvoidChain to attempt to make the consecutive cards f…
Vylantze Sep 3, 2025
9a46c34
Add handling for Avoid in ColorChainHandler
Vylantze Sep 3, 2025
0b65233
Extract AvoidBraveChain logic to Utils file
Vylantze Sep 3, 2025
106f5f6
Add AvoidBraveChain to MightyChainHandler
Vylantze Sep 3, 2025
812960b
Prevent the forcing of BraveChains if BraveChainEnum is set to None
Vylantze Sep 3, 2025
1d746a7
Change the behavior when dealing with forcedBraveChain and BraveChain…
Vylantze Sep 3, 2025
20bf2d3
Add new tests. Fix bug in getBraveChainFieldSlot
Vylantze Sep 3, 2025
c515a9d
Add more test cases for Utils
Vylantze Sep 3, 2025
9a60da9
Fix edge case for getCardsForAvoidBraveChain to maintain card order. …
Vylantze Sep 3, 2025
f4b5c7f
Rename Utils to AttackUtils
Vylantze Sep 4, 2025
53bcc60
Remove the unnecessary inject by converting it to a normal class cons…
Vylantze Sep 4, 2025
c16e1a7
Add more tests. Refactor some tests
Vylantze Sep 4, 2025
dd14ba7
Make BraveChainHandler use util function for getting BraveChains. Add…
Vylantze Sep 4, 2025
29957fb
Add active selection of cards for BraveChainEnum.Avoid in BraveChainH…
Vylantze Sep 4, 2025
e312044
Give variable more appropriate name
Vylantze Sep 4, 2025
766f49d
Refactor to make more easy to read
Vylantze Sep 4, 2025
9452750
Add special function for handling AvoidBraveChain with MightyChain. A…
Vylantze Sep 4, 2025
6ba7f47
Move function to ColorChainHandler. Move tests. Redo getCardsForAvoi…
Vylantze Sep 4, 2025
bed2d25
Revert "Remove the unnecessary inject by converting it to a normal cl…
Vylantze Sep 4, 2025
51a7824
Fix bug with build. Clean up unnecessary imports
Vylantze Sep 4, 2025
fb114c3
Refactor ColorChainTests to make them more compact.
Vylantze Sep 4, 2025
8f89892
Merge branch 'master' into basic-mighty-chain
Vylantze Sep 4, 2025
140818c
Add more handling for Unknown cards
Vylantze Sep 6, 2025
8b4a3ac
Add CardTypeEnum.Unknown tests
Vylantze Sep 6, 2025
0fcc419
Use more appropriate name for unknown filtered cards
Vylantze Sep 6, 2025
153e008
Ensure that 5 cards are always returned (even if there is Unknown)
Vylantze Sep 6, 2025
728f839
Add tests for AvoidChain and BraveChain
Vylantze Sep 6, 2025
fd9e89d
Add tests for ColorChain and MightyChain for Unknown handling
Vylantze Sep 6, 2025
7dfb83a
Add tests for all BraveChainEnums in ColorChainTest.
Vylantze Sep 6, 2025
86e4706
Fix bug where ColorChain was not handling Avoid properly
Vylantze Sep 6, 2025
8832a50
Change AttackUtils to object
Vylantze Sep 7, 2025
28f4a5a
Change AvoidChainHandler to object
Vylantze Sep 7, 2025
6c5033b
Change BraveChainHandler to object
Vylantze Sep 7, 2025
12e0562
Change ColorChainHandler to object
Vylantze Sep 7, 2025
886ff8d
Change MightyChainHandler to object
Vylantze Sep 7, 2025
fcbb00d
Clean up unused imports and styling warnings
Vylantze Sep 7, 2025
15995dc
Change CardChainPriorityHandler to an object as well
Vylantze Sep 7, 2025
4db66bb
Change AttackPriorityHandler to object
Vylantze Sep 7, 2025
d79125f
Clean up unused imports
Vylantze Sep 7, 2025
d7c39be
Change ApplyBraveChains to object
Vylantze Sep 7, 2025
f70cc78
Merge branch 'convert-classes-to-objects' into basic-mighty-chain
Vylantze Sep 7, 2025
11b03db
Add NP detection system using faces
Vylantze Sep 7, 2025
a9992fa
Add None as an option in ChainTypeEnum, to replace Avoid's role as a …
Vylantze Sep 8, 2025
8748533
Add fix for old builds that do not have the type
Vylantze Sep 8, 2025
99a397a
Change color of disabled text
Vylantze Sep 8, 2025
bfeffd8
Fix bug with text color. Set better text color for disabled
Vylantze Sep 8, 2025
87c0222
Make the disabled text color more visible
Vylantze Sep 9, 2025
5f935e5
Split detection function in preparation for future feature
Vylantze Sep 9, 2025
11fcdb9
If ChainPriority is on, use the NP automatic detection for clicking a…
Vylantze Sep 9, 2025
3c69e92
Fix bug where npTypes were not detected for spam NPs
Vylantze Sep 9, 2025
42977d4
Revert "If ChainPriority is on, use the NP automatic detection for cl…
Vylantze Sep 9, 2025
677dfc6
Add additional checks so that it won't immediately fail if it cannot …
Vylantze Sep 9, 2025
be28269
Only use detected exclusively for spamNPs.
Vylantze Sep 10, 2025
aece22b
Revert the additional checks
Vylantze Sep 10, 2025
49795ef
Remove forceBraveChain option from subhandlers. Put the forceBraveCha…
Vylantze Sep 12, 2025
47c017e
Change AvoidChainHandler's behaviour since it is now included in the …
Vylantze Sep 12, 2025
51971a6
Add tests for all BraveChainEnums for AvoidChainHandler.
Vylantze Sep 12, 2025
3779ec0
Add new checks and new tests for AvoidChainHandler.
Vylantze Sep 12, 2025
8b8ea2f
Update documentation (part 1)
Vylantze Sep 17, 2025
093d753
Update images with transparency
Vylantze Sep 17, 2025
8ab1541
Update documentation (part 2)
Vylantze Sep 17, 2025
08fe2fd
Readd forceBraveChain so that in the case where there is no Brave Cha…
Vylantze Sep 17, 2025
0cd49f0
Add encasing box around advanced note
Vylantze Sep 17, 2025
81ac38c
Update advanced note
Vylantze Sep 17, 2025
b1a0302
Update documentation part 3
Vylantze Sep 18, 2025
e417a8e
Update documentation part 4
Vylantze Sep 18, 2025
d1852b8
Fix bug with single card before NP + 1 NP usage. Add test case for it.
Vylantze Sep 21, 2025
b3a7dcc
Add more tests.
Vylantze Sep 21, 2025
485deae
Add handling for when support Servant cannot be detected due to too m…
Vylantze Oct 6, 2025
329e295
Remove black color for Mighty Chain documentation
Vylantze Oct 6, 2025
a60d5f4
Add the waitBeforeTurn duration after a orderChange action to attempt…
Vylantze Oct 6, 2025
a41b7f6
Update app/src/main/res/values-b+zh+CN/localized.xml
Vylantze Oct 6, 2025
d9c5876
Add new variables for Fine Tune.
Vylantze Oct 6, 2025
160b720
Update with a comment on the redundancy of the defaultOrder.
Vylantze Oct 7, 2025
8c3d185
Force rebuild
Vylantze Dec 5, 2025
4704264
Merge branch 'master' into basic-mighty-chain
Vylantze Dec 22, 2025
e4f7a7a
Fix merge conflicts
Vylantze Dec 22, 2025
a235832
Merge remote-tracking branch 'Fate-Grand-Automata/master' into basic-…
Vylantze Dec 25, 2025
ae70eea
Merge branch 'master' into basic-mighty-chain
ArthurKun21 Mar 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added app/src/main/assets/En/np_art.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/assets/En/np_buster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/assets/En/np_quick.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.github.fate_grand_automata.scripts.models.AutoSkillCommand
import io.github.fate_grand_automata.scripts.models.CardPriorityPerWave
import io.github.fate_grand_automata.scripts.models.ServantPriorityPerWave
import io.github.fate_grand_automata.scripts.models.SpamConfigPerTeamSlot
import io.github.fate_grand_automata.scripts.models.battle.ChainPriorityPerWave
import io.github.fate_grand_automata.scripts.prefs.IBattleConfig
import io.github.fate_grand_automata.scripts.prefs.IPreferences
import io.github.fate_grand_automata.scripts.prefs.ISupportPreferences
Expand Down Expand Up @@ -50,4 +51,9 @@ class PreferencesModule {
@Provides
fun provideServantPriority(battleConfig: IBattleConfig): ServantPriorityPerWave? =
if (battleConfig.useServantPriority) battleConfig.servantPriority else null

@ScriptScope
@Provides
fun provideChainPriority(battleConfig: IBattleConfig): ChainPriorityPerWave? =
if (battleConfig.useChainPriority) battleConfig.chainPriority else null
}
4 changes: 4 additions & 0 deletions app/src/main/java/io/github/fate_grand_automata/ui/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ private val DarkColors = darkColorScheme(
fun FGAListItemColors() = ListItemDefaults.colors(
containerColor = MaterialTheme.colorScheme.surfaceVariant
)
@Composable
fun FGAListItemColorsDisabled() = ListItemDefaults.colors(
containerColor = Color.Gray
)

@Composable
fun FGATheme(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package io.github.fate_grand_automata.ui.card_priority

import androidx.compose.runtime.MutableState
import io.github.fate_grand_automata.scripts.enums.BraveChainEnum
import io.github.fate_grand_automata.scripts.enums.ChainTypeEnum
import io.github.fate_grand_automata.scripts.models.CardScore
import io.github.fate_grand_automata.scripts.models.TeamSlot

data class CardPriorityListItem(
val scores: MutableList<CardScore>,
val servantPriority: MutableList<TeamSlot>,
val chainPriority: MutableList<ChainTypeEnum>,
var rearrangeCards: MutableState<Boolean>,
var braveChains: MutableState<BraveChainEnum>
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ fun CardPriorityScreen(
) {
CardPriorityView(
items = vm.cardPriorityItems,
useServantPriority = vm.useServantPriority
useServantPriority = vm.useServantPriority,
useChainPriority = vm.useChainPriority
)

DisposableEffect(vm) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import kotlinx.coroutines.launch
@Composable
fun CardPriorityView(
items: SnapshotStateList<CardPriorityListItem>,
useServantPriority: Pref<Boolean>
useServantPriority: Pref<Boolean>,
useChainPriority: Pref<Boolean>
) {
val pagerState = rememberPagerState(pageCount = {items.size},)
val scope = rememberCoroutineScope()
Expand All @@ -50,6 +51,13 @@ fun CardPriorityView(
val servantPriority by useServantPriority.remember()
useServantPriority.SwitchPreference(
title = stringResource(R.string.p_battle_config_use_servant_priority),
modifier = Modifier
.padding(bottom = 4.dp)
)

val chainPriority by useChainPriority.remember()
useChainPriority.SwitchPreference(
title = stringResource(R.string.p_battle_config_use_chain_priority),
modifier = Modifier
.padding(bottom = 16.dp)
)
Expand Down Expand Up @@ -81,7 +89,8 @@ fun CardPriorityView(
}

items.getOrNull(it)?.Render(
useServantPriority = servantPriority
useServantPriority = servantPriority,
useChainPriority = chainPriority
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.github.fate_grand_automata.scripts.enums.BraveChainEnum
import io.github.fate_grand_automata.scripts.models.CardPriority
import io.github.fate_grand_automata.scripts.models.CardPriorityPerWave
import io.github.fate_grand_automata.scripts.models.ServantPriorityPerWave
import io.github.fate_grand_automata.scripts.models.battle.ChainPriorityPerWave
import javax.inject.Inject

@HiltViewModel
Expand All @@ -21,6 +22,7 @@ class CardPriorityViewModel @Inject constructor(
val cardPriorityItems: SnapshotStateList<CardPriorityListItem> by lazy {
val cardPriority = battleConfig.cardPriority.get()
val servantPriority = battleConfig.servantPriority.get()
val chainPriority = battleConfig.chainPriority.get()

val rearrangeCards = battleConfig.rearrangeCards.get()
val braveChains = battleConfig.braveChains.get()
Expand All @@ -33,6 +35,7 @@ class CardPriorityViewModel @Inject constructor(
CardPriorityListItem(
it.value,
servantPriority.atWave(it.index).toMutableList(),
chainPriority.atWave(it.index).toMutableList(),
mutableStateOf(rearrangeCards.getOrElse(it.index) { false }),
mutableStateOf(braveChains.getOrElse(it.index) { BraveChainEnum.None })
)
Expand All @@ -41,6 +44,7 @@ class CardPriorityViewModel @Inject constructor(
}

val useServantPriority = battleConfig.useServantPriority
val useChainPriority = battleConfig.useChainPriority

fun save() {
battleConfig.cardPriority.set(
Expand All @@ -55,6 +59,12 @@ class CardPriorityViewModel @Inject constructor(
)
)

battleConfig.chainPriority.set(
ChainPriorityPerWave.from(
cardPriorityItems.map { it.chainPriority }
)
)

battleConfig.rearrangeCards.set(cardPriorityItems.map { it.rearrangeCards.value })
battleConfig.braveChains.set(cardPriorityItems.map { it.braveChains.value })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ fun CardPriorityWaveSelector(
CardPriorityListItem(
items[0].scores.toMutableList(),
items[0].servantPriority.toMutableList(),
items[0].chainPriority.toMutableList(),
mutableStateOf(false),
mutableStateOf(BraveChainEnum.None)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import io.github.fate_grand_automata.R
import io.github.fate_grand_automata.scripts.enums.BraveChainEnum
import io.github.fate_grand_automata.scripts.enums.ChainTypeEnum
import io.github.fate_grand_automata.scripts.models.TeamSlot
import io.github.fate_grand_automata.ui.FGAListItemColors
import io.github.fate_grand_automata.ui.drag_sort.DragSort
Expand All @@ -30,7 +31,8 @@ import io.github.fate_grand_automata.util.stringRes

@Composable
fun CardPriorityListItem.Render(
useServantPriority: Boolean
useServantPriority: Boolean,
useChainPriority: Boolean
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally
Expand All @@ -53,15 +55,19 @@ fun CardPriorityListItem.Render(
val braveChainDialog = listDialog(
selected = braveChains,
onSelectedChange = { braveChains = it },
entries = BraveChainEnum.entries
entries = BraveChainEnum.entries.filter {
useChainPriority || it != BraveChainEnum.Always
}
.associateWith { stringResource(it.stringRes) },
title = stringResource(R.string.p_brave_chains)
)

ListItem(
modifier = Modifier
.weight(1f)
.clickable { braveChainDialog.show() },
.clickable {
braveChainDialog.show()
},
headlineContent = { Text(stringResource(R.string.p_brave_chains)) },
supportingContent = { Text(stringResource(braveChains.stringRes)) },
colors = FGAListItemColors()
Expand All @@ -85,6 +91,12 @@ fun CardPriorityListItem.Render(
}
}

if (useChainPriority) {
ChainPriority(
priorities = chainPriority
)
}

if (useServantPriority) {
ServantPriority(
priorities = servantPriority
Expand All @@ -98,7 +110,7 @@ private fun ServantPriority(
priorities: MutableList<TeamSlot>
) {
Text(
"Servant Priority".uppercase(),
stringResource(R.string.p_servant_priority).uppercase(),
modifier = Modifier
.padding(bottom = 5.dp, top = 16.dp)
)
Expand All @@ -122,4 +134,59 @@ private fun ServantPriority(
)
}
)
}
}

@Composable
private fun ChainPriority(
priorities: MutableList<ChainTypeEnum>
) {
Text(
stringResource(R.string.p_chain_priority).uppercase(),
modifier = Modifier
.padding(bottom = 10.dp, top = 10.dp)
)

val context = LocalContext.current

val getForegroundColor = fun (enum: ChainTypeEnum): Int {
return (if (priorities.isAfterCutoff(enum)) context.getColor(R.color.colorDisabledText)
else when (enum) {
ChainTypeEnum.None -> Color.BLACK
else -> Color.WHITE
})
}
val getBackgroundColor = fun (enum: ChainTypeEnum): Int {
return (if (priorities.isAfterCutoff(enum)) R.color.colorDisabledBackground
else when (enum) {
ChainTypeEnum.Arts -> R.color.colorArts
ChainTypeEnum.Quick -> R.color.colorQuick
ChainTypeEnum.Buster -> R.color.colorBuster
ChainTypeEnum.Mighty -> R.color.colorPrimaryDark
ChainTypeEnum.Avoid -> R.color.colorAvoid
ChainTypeEnum.None -> R.color.colorDisabledBackground
}
).let { res -> context.getColor(res) }
}
val localizedStringMap: MutableMap<ChainTypeEnum, String> = mutableMapOf()
ChainTypeEnum.entries.forEach {
localizedStringMap[it] = if (it == ChainTypeEnum.None) "│" else stringResource(it.stringRes)
}
DragSort(
items = priorities,
viewConfigGrabber = {
DragSortAdapter.ItemViewConfig(
foregroundColor = getForegroundColor(it),
backgroundColor = getBackgroundColor(it),
text = localizedStringMap.getOrElse(it) { it.name },
)
},
updateBackgroundColorOnMove = true
)
}

fun MutableList<ChainTypeEnum>.isAfterCutoff(chainTypeEnum: ChainTypeEnum, indexOfAvoid: Int? = null): Boolean {
val avoidIndex = indexOfAvoid ?: this.indexOf(ChainTypeEnum.Cutoff)
val enumIndex = this.indexOf(chainTypeEnum)
if (avoidIndex < 0) return false
return enumIndex > avoidIndex
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import io.github.fate_grand_automata.util.ItemTouchHelperCallback
@Composable
fun <T> DragSort(
items: MutableList<T>,
viewConfigGrabber: (T) -> DragSortAdapter.ItemViewConfig
viewConfigGrabber: (T) -> DragSortAdapter.ItemViewConfig,
updateBackgroundColorOnMove: Boolean = false
) {
AndroidView(
factory = { context ->
Expand All @@ -23,9 +24,12 @@ fun <T> DragSort(
update = {
it.adapter = DragSortAdapter(
items,
viewConfigGrabber
viewConfigGrabber,
updateBackgroundColorOnMove
).also { adapter ->
val callback = ItemTouchHelperCallback(adapter)
val callback = ItemTouchHelperCallback(
adapter = adapter
)
val itemTouchHelper = ItemTouchHelper(callback)
itemTouchHelper.attachToRecyclerView(it)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import io.github.fate_grand_automata.util.IItemTouchHelperViewHolder

class DragSortAdapter<T>(
private val items: MutableList<T>,
private val viewConfigGrabber: (T) -> ItemViewConfig
private val viewConfigGrabber: (T) -> ItemViewConfig,
val updateBackgroundColorOnMove: Boolean = false,
) : RecyclerView.Adapter<DragSortAdapter.ViewHolder>(), IItemTouchHelperAdapter {
class ItemViewConfig(
@ColorInt val foregroundColor: Int,
Expand Down Expand Up @@ -59,11 +60,24 @@ class DragSortAdapter<T>(
holder.textView.setTextColor(viewConfig.foregroundColor)
}

override fun onItemMove(From: Int, To: Int) {
val temp = items[From]
items[From] = items[To]
items[To] = temp
override fun onItemMove(from: Int, to: Int, origin: View?, target: View?) {
val temp = items[from]
items[from] = items[to]
items[to] = temp

notifyItemMoved(From, To)
// Swap views since the items also swapped
val views = listOf(target, origin)
for ((index, pos) in listOf(from, to).withIndex()) {
val currentItem = items[pos]
val viewConfig = viewConfigGrabber(currentItem)
val view = views[index]
if (updateBackgroundColorOnMove && view != null) {
view.setBackgroundColor(viewConfig.backgroundColor)
val textView: TextView = view.findViewById(R.id.drag_sort_text)
textView.setTextColor(viewConfig.foregroundColor)
}
}

notifyItemMoved(from, to)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@ package io.github.fate_grand_automata.util
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView

class ItemTouchHelperCallback(private val Adapter: IItemTouchHelperAdapter,
private val DragFlags: Int = ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) : ItemTouchHelper.Callback() {
class ItemTouchHelperCallback(
private val adapter: IItemTouchHelperAdapter,
private val dragFlags: Int = ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT,
) : ItemTouchHelper.Callback() {

override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
return makeMovementFlags(DragFlags, 0)
return makeMovementFlags(dragFlags, 0)
}

override fun isLongPressDragEnabled() = true

override fun isItemViewSwipeEnabled() = false

override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
Adapter.onItemMove(viewHolder.bindingAdapterPosition, target.bindingAdapterPosition)
adapter.onItemMove(
viewHolder.bindingAdapterPosition,
target.bindingAdapterPosition,
viewHolder.itemView,
target.itemView
)
return true
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.fate_grand_automata.util

import android.view.View

interface IItemTouchHelperAdapter {
fun onItemMove(From: Int, To: Int)
fun onItemMove(from: Int, to: Int, origin: View? = null, target: View? = null)
}

interface IItemTouchHelperViewHolder {
Expand Down
Loading