Skip to content

Instantly share code, notes, and snippets.

View GameViewModel.kt
@HiltViewModel
class GameViewModel @Inject constructor() : ViewModel() {
private val field = Field()
private val stateFlow = MutableStateFlow(field.getActualField())
val state : StateFlow<Array<IntArray>> = stateFlow
init {
viewModelScope.launch {
while (!field.isGameEnded) {
View GameActivity.kt
val state : State<Array<IntArray>> = gameViewModel.state.collectAsState()
Canvas(
modifier = Modifier
.fillMaxHeight(0.8f)
.aspectRatio(0.5f)
.border(1.dp, Color.Black)
) {
val squareDim = size.height / state.value.size
state.value.forEachIndexed { y, it ->
it.forEachIndexed { x, value ->
View GameActivity.kt
fun DrawScope.drawGameElement(
squareColor : Color,
borderColor : Color,
offset : Offset,
size : Size
) {
drawRect(
squareColor,
topLeft = offset,
size = size
View GameActivity.kt
@Preview
@Composable
fun DrawSquare() {
Canvas(
modifier = Modifier
.height(100.dp)
.width(200.dp)
) {
drawRect(
color = Color.Red,
View GameActivity.kt
@Preview
@Composable
fun DrawSquare() {
Canvas(
modifier = Modifier
.height(100.dp)
.width(200.dp)
) {
drawRect(
color = Color.Red,
View GameActivity.kt
@Preview
@Composable
fun DrawSquare() {
Canvas(modifier = Modifier.size(200.dp)) {
drawRect(
color = Color.Red,
size = size
)
}
}
View DrawScope.kt
fun drawLine(
color: Color,
start: Offset,
end: Offset,
strokeWidth: Float = Stroke.HairlineWidth,
cap: StrokeCap = Stroke.DefaultCap,
pathEffect: PathEffect? = null,
/*FloatRange(from = 0.0, to = 1.0)*/
alpha: Float = 1.0f,
colorFilter: ColorFilter? = null,
View ExpandableStackViewAdapter.kt
class ExpandableStackViewAdapter(
private val models: List<String>,
private val context: Context
) : BaseAdapter() {
override fun getCount(): Int = models.size
override fun getItem(position: Int): String = models[position]
override fun getItemId(position: Int): Long = position.toLong()
View ExpandableStackView.kt
fun setAdapter(adapter: BaseAdapter) {
val scene = MotionScene(this)
val startSetId = View.generateViewId()
val startSet = ConstraintSet()
startSet.clone(this)
val endSetId = View.generateViewId()
val endSet = ConstraintSet()
View ConstraintSetExtension.kt
fun ConstraintSet.setCardElevation(cardView: CardView, elevation: Float) {
getConstraint(cardView.id)?.let {
it.mCustomConstraints["CardElevation"] = ConstraintAttribute(
"CardElevation",
ConstraintAttribute.AttributeType.DIMENSION_TYPE,
elevation
)
}
}