Skip to content

About

Kotlin library for Formclick. Send form submissions from Android apps with coroutines and StateFlow, for Jetpack Compose or Views. No backend needed.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Formclick for Android

Send form submissions to Formclick from Android apps written in Kotlin, with Jetpack Compose or Views.

It has the same API as @formclick/react-native and the formclick Flutter package: a stateless Formclick client, plus a FormclickSubmitter that exposes loading, success and error state as a StateFlow. It uses HttpURLConnection and Android's built-in org.json. The only dependency is kotlinx-coroutines.

Requirements

  • Android API 21+ (minSdk 21), Kotlin 1.9+, Java 17 toolchain
  • Internet permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />

Installation

// app/build.gradle.kts
dependencies {
    implementation("io.formclick:formclick-android:0.1.0")
}

Get your access key from the Formclick dashboard. Every example below uses YOUR_ACCESS_KEY.

Jetpack Compose

class ContactViewModel : ViewModel() {
    val form = FormclickSubmitter()

    fun send(name: String, email: String) = viewModelScope.launch {
        runCatching {
            form.submit("YOUR_ACCESS_KEY", mapOf("name" to name, "email" to email))
        }
    }
}

@Composable
fun ContactScreen(vm: ContactViewModel = viewModel()) {
    val state by vm.form.state.collectAsState()
    var name by remember { mutableStateOf("") }
    var email by remember { mutableStateOf("") }

    Column(Modifier.padding(16.dp)) {
        OutlinedTextField(name, { name = it }, label = { Text("Name") })
        OutlinedTextField(email, { email = it }, label = { Text("Email") })
        Button(onClick = { vm.send(name, email) }, enabled = !state.loading) {
            Text(if (state.loading) "Sending…" else "Send")
        }
        if (state.success) Text("Sent!", color = Color(0xFF16A34A))
        state.error?.let { Text(it.message.orEmpty(), color = MaterialTheme.colorScheme.error) }
    }
}

Views (Fragment or Activity)

viewLifecycleOwner.lifecycleScope.launch {
    viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
        vm.form.state.collect { state ->
            sendButton.isEnabled = !state.loading
            statusText.text = when {
                state.success -> "Sent!"
                state.error != null -> state.error.message
                else -> ""
            }
        }
    }
}

One-shot call

val result = Formclick().submit(
    formId = "YOUR_ACCESS_KEY",
    data = mapOf("name" to "Jane", "email" to "jane@example.com", "topics" to listOf("billing")),
)
println("${result.success} ${result.message}")

submit is a suspend function that runs on Dispatchers.IO, so it's safe to call from the main thread inside a coroutine. Field values can be strings, numbers, booleans, null, or lists of those.

Errors

submit throws a FormclickException:

Type When
MissingAccessKey formId is blank
Server(status, serverMessage) The server returned a non-2xx status, such as an invalid key or a spam block
Network(cause) No connection or a timeout

Configuration

val client = Formclick(
    submitBaseUrl = "https://api.formclick.io/submit", // default
    headers = mapOf("X-App-Version" to "1.2.0"),
    timeoutMillis = 15_000,
)
val form = FormclickSubmitter(
    client = client,
    onSuccess = { Log.d("Formclick", "Sent: $it") },
    onError = { Log.w("Formclick", "Failed", it) },
)

Development

gradle test   # runs against a local mock server, no network calls
gradle publishToMavenLocal

License

MIT © Formclick

About

Kotlin library for Formclick. Send form submissions from Android apps with coroutines and StateFlow, for Jetpack Compose or Views. No backend needed.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages