-
-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial implementation of SwitchesUi
- Loading branch information
1 parent
aa9d8a7
commit bfe66a4
Showing
12 changed files
with
255 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package com.osfans.trime.ime.bar.ui | ||
|
||
import android.content.Context | ||
import android.widget.Space | ||
import android.widget.ViewAnimator | ||
import com.osfans.trime.data.theme.Theme | ||
import com.osfans.trime.ime.bar.ui.always.switches.SwitchesUi | ||
import splitties.views.dsl.constraintlayout.centerInParent | ||
import splitties.views.dsl.constraintlayout.constraintLayout | ||
import splitties.views.dsl.constraintlayout.lParams | ||
import splitties.views.dsl.constraintlayout.matchConstraints | ||
import splitties.views.dsl.core.Ui | ||
import splitties.views.dsl.core.add | ||
import splitties.views.dsl.core.lParams | ||
import splitties.views.dsl.core.matchParent | ||
import timber.log.Timber | ||
|
||
class AlwaysUi( | ||
override val ctx: Context, | ||
private val theme: Theme, | ||
) : Ui { | ||
enum class State { | ||
Empty, | ||
Switchers, | ||
} | ||
|
||
var currentState = State.Empty | ||
private set | ||
|
||
val emptyBar = Space(ctx) | ||
|
||
val switchesUi = SwitchesUi(ctx, theme) | ||
|
||
private val animator = | ||
ViewAnimator(ctx).apply { | ||
add(emptyBar, lParams(matchParent, matchParent)) | ||
add(switchesUi.root, lParams(matchParent, matchParent)) | ||
} | ||
|
||
override val root = | ||
constraintLayout { | ||
add( | ||
animator, | ||
lParams(matchConstraints, matchParent) { | ||
centerInParent() | ||
}, | ||
) | ||
} | ||
|
||
fun updateState(state: State) { | ||
Timber.d("Switch always ui to $state") | ||
when (state) { | ||
State.Empty -> animator.displayedChild = 0 | ||
State.Switchers -> animator.displayedChild = 1 | ||
} | ||
currentState = state | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
app/src/main/java/com/osfans/trime/ime/bar/ui/always/switches/SwitchUi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package com.osfans.trime.ime.bar.ui.always.switches | ||
|
||
import android.content.Context | ||
import com.osfans.trime.data.theme.ColorManager | ||
import com.osfans.trime.data.theme.FontManager | ||
import com.osfans.trime.data.theme.Theme | ||
import splitties.views.dsl.core.Ui | ||
import splitties.views.dsl.core.add | ||
import splitties.views.dsl.core.frameLayout | ||
import splitties.views.dsl.core.lParams | ||
import splitties.views.dsl.core.textView | ||
import splitties.views.gravityCenter | ||
|
||
class SwitchUi(override val ctx: Context, private val theme: Theme) : Ui { | ||
var enabled: Int = -1 | ||
|
||
private val label = | ||
textView { | ||
textSize = theme.generalStyle.candidateTextSize.toFloat() | ||
typeface = FontManager.getTypeface("candidate_font") | ||
ColorManager.getColor("candidate_text_color")?.let { setTextColor(it) } | ||
} | ||
|
||
override val root = | ||
frameLayout { | ||
add(label, lParams { gravity = gravityCenter }) | ||
} | ||
|
||
fun setLabel(str: String) { | ||
label.text = str | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
app/src/main/java/com/osfans/trime/ime/bar/ui/always/switches/SwitchesAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package com.osfans.trime.ime.bar.ui.always.switches | ||
|
||
import android.content.Context | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.chad.library.adapter4.BaseQuickAdapter | ||
import com.osfans.trime.data.schema.Schema | ||
import com.osfans.trime.data.theme.Theme | ||
|
||
class SwitchesAdapter(private val theme: Theme) : | ||
BaseQuickAdapter<Schema.Switch, SwitchesAdapter.Holder>() { | ||
inner class Holder(val ui: SwitchUi) : RecyclerView.ViewHolder(ui.root) | ||
|
||
override fun onCreateViewHolder( | ||
context: Context, | ||
parent: ViewGroup, | ||
viewType: Int, | ||
): Holder { | ||
return Holder(SwitchUi(context, theme)) | ||
} | ||
|
||
override fun onBindViewHolder( | ||
holder: Holder, | ||
position: Int, | ||
item: Schema.Switch?, | ||
) { | ||
holder.ui.apply { | ||
val enabled = item!!.enabled | ||
setLabel(item.states!![enabled]) | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
app/src/main/java/com/osfans/trime/ime/bar/ui/always/switches/SwitchesUi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package com.osfans.trime.ime.bar.ui.always.switches | ||
|
||
import android.content.Context | ||
import android.view.ViewGroup | ||
import com.chad.library.adapter4.util.setOnDebouncedItemClick | ||
import com.osfans.trime.data.schema.Schema | ||
import com.osfans.trime.data.theme.Theme | ||
import com.osfans.trime.ime.symbol.SpacesItemDecoration | ||
import splitties.dimensions.dp | ||
import splitties.views.dsl.core.Ui | ||
import splitties.views.dsl.core.matchParent | ||
import splitties.views.dsl.recyclerview.recyclerView | ||
import splitties.views.recyclerview.horizontalLayoutManager | ||
|
||
class SwitchesUi(override val ctx: Context, val theme: Theme) : Ui { | ||
private val switchesAdapter by lazy { | ||
SwitchesAdapter(theme) | ||
} | ||
|
||
override val root = | ||
recyclerView { | ||
layoutParams = ViewGroup.LayoutParams(matchParent, matchParent) | ||
layoutManager = horizontalLayoutManager() | ||
adapter = switchesAdapter | ||
addItemDecoration(SpacesItemDecoration(dp(3))) | ||
} | ||
|
||
fun setSwitches(list: List<Schema.Switch>) { | ||
switchesAdapter.submitList(list) | ||
} | ||
|
||
fun setOnSwitchClick(listener: (Schema.Switch) -> Unit) { | ||
switchesAdapter.setOnDebouncedItemClick { adapter, _, position -> | ||
val typed = (adapter as SwitchesAdapter) | ||
if (typed.items.isEmpty()) return@setOnDebouncedItemClick | ||
val switch = typed.items[position] | ||
listener(switch) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.