Skip to content

Usage with Jetpack Compose? #23

Description

@Myzel394

I don't know whether this is a mistake on my side, but I don't know how create a State with Jetpack Compose.

So I got a audio recording app and I want the user to be able to detect what state it is currently running.

@TaskerInputRoot
class AudioRecorderInput(
    @field:TaskerInputField(VAR_STATE, labelResIdName = "ui_audioRecorder_recordingState_label") var state: String? = null
) {
    companion object {
        const val VAR_STATE = "audioRecorderState"
    }
}

@TaskerOutputObject
class AudioRecorderOutput(
    @get:TaskerOutputVariable(VAR_STATE, labelResIdName = "ui_audioRecorder_recordingState_label") val state: String = RecorderState.IDLE.name
) {
    companion object {
        const val VAR_STATE = "audioRecorderState"
    }
}

Then, in the runner I check whether the given state by the user matches the current state:

class AudioRecorderRunner : TaskerPluginRunnerConditionState<AudioRecorderInput, AudioRecorderOutput>() {
    override fun getSatisfiedCondition(context: Context, input: TaskerInput<AudioRecorderInput>, update: Unit?): TaskerPluginResultCondition<AudioRecorderOutput> {
        return if (state.name == input.regular.state)
            TaskerPluginResultConditionSatisfied(context)
        else TaskerPluginResultConditionUnsatisfied()
    }

    companion object {
        var state: RecorderState = RecorderState.IDLE
        fun change(context: Context, newState: RecorderState) {
            state = newState
            AudioRecorderActivity::class.java.requestQuery(context)
        }
    }
}

This is the helper:

class AudioRecorderHelper(config: TaskerPluginConfig<AudioRecorderInput>) : TaskerPluginConfigHelper<AudioRecorderInput, AudioRecorderOutput, AudioRecorderRunner>(config) {
    override val runnerClass = AudioRecorderRunner::class.java
    override val inputClass = AudioRecorderInput::class.java
    override val outputClass = AudioRecorderOutput::class.java
}

and in the activity I show a dropdown for the user to select from - but how do I pass that information back to Tasker?

class AudioRecorderActivity : AppCompatActivity(), TaskerPluginConfig<AudioRecorderInput> {
    override val inputForTasker: TaskerInput<AudioRecorderInput>
        get() = TaskerInput(AudioRecorderInput(selectedState.name))

    override val context: Context
        get() = applicationContext

    override fun assignFromInput(input: TaskerInput<AudioRecorderInput>) {
        // println("test") // Does not seem to be called
        input.regular.run {
            selectedState = RecorderState.valueOf(state ?: RecorderState.IDLE.name)
        }
    }

    var selectedState by mutableStateOf(RecorderState.IDLE)

    override fun onStart() {
        super.onStart()

        WindowCompat.setDecorFitsSystemWindows(window, false)

        setContent {
            var selectedState by remember { mutableStateOf(RecorderState.IDLE) }

            AlibiTheme {
                Column(
                    modifier = Modifier
                        .fillMaxSize()
                        .background(MaterialTheme.colorScheme.background)
                        .padding(horizontal = 16.dp, vertical = 32.dp),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.SpaceBetween,
                ) {
                    Box {}
                    Text(
                        "Configure Audio Recorder State",
                        style = MaterialTheme.typography.headlineLarge,
                        color = MaterialTheme.colorScheme.onBackground,
                        textAlign = TextAlign.Center,
                    )
                    Box {}

                    var opened by remember { mutableStateOf(false) }
                    Column {
                        Box {
                            Button(
                                onClick = {
                                    opened = true
                                },
                                colors = ButtonDefaults.filledTonalButtonColors(
                                    containerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(
                                        4.dp
                                    ),
                                ),
                                shape = MaterialTheme.shapes.small,
                            ) {
                                Text(
                                    getString(ENUM_LABEL_MAP[selectedState]!!)
                                )
                            }
                            DropdownMenu(
                                expanded = opened,
                                onDismissRequest = { opened = false },
                            ) {
                                RecorderState.values().forEach { state ->
                                    DropdownMenuItem(
                                        onClick = {
                                            opened = false

                                            selectedState = state
                                        },
                                        text = {
                                            val resourceId = ENUM_LABEL_MAP[state]!!
                                            val text = getString(resourceId)

                                            Text(text)
                                        }
                                    )
                                }
                            }
                        }
                    }

                    Button(
                        onClick = {
                            val helper = AudioRecorderHelper(this@AudioRecorderActivity)

                            helper.finishForTasker()
                        },
                    ) {
                        Text(
                            stringResource(id = R.string.dialog_close_neutral_label)
                        )
                    }
                }
            }
        }
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions