Calf is a library that allows you to easily create adaptive UIs for your Compose Multiplatform apps.
Calf stands for Compose Adaptive Look & Feel
The main focus for now is Android and iOS, but more Desktop components are coming that allows you to create adaptive UIs for Desktop as well (Windows, macOS, Linux)
Add the following dependency to your module build.gradle.kts
file:
// For Adaptive UI components
api("com.mohamedrejeb.calf:calf-ui:0.1.1")
// For Adaptive FilePicker
implementation("com.mohamedrejeb.calf:calf-file-picker:0.1.1")
If you are using calf-ui
artifact, make sure to export it to binaries:
...
kotlin {
...
targets
.filterIsInstance<KotlinNativeTarget>()
.filter { it.konanTarget.family == Family.IOS }
.forEach {
it.binaries.framework {
...
export("com.mohamedrejeb.calf:calf-ui:0.1.1")
}
}
...
}
...
...
kotlin {
...
cocoapods {
...
framework {
...
export("com.mohamedrejeb.calf:calf-ui:0.1.1")
}
}
...
}
...
Important: Exporting
calf-ui
to binaries is required to make it work on iOS.
AdaptiveAlertDialog
is a dialog that adapts to the platform it is running on. It is a wrapper around AlertDialog
on Android and UIAlertController
on iOS.
Android | iOS |
---|---|
var showDialog by remember { mutableStateOf(false) }
Button(
onClick = { showDialog = true },
) {
Text("Show Alert Dialog")
}
if (showDialog) {
AdaptiveAlertDialog(
onConfirm = { showDialog = false },
onDismiss = { showDialog = false },
confirmText = "Ok",
dismissText = "Cancel",
title = "Alert Dialog",
text = "This is a native alert dialog from Calf",
)
}
AdaptiveBottomSheet
is a bottom sheet that adapts to the platform it is running on. It is a wrapper around ModalBottomSheet
on Android and UIModalPresentationPopover
on iOS.
Android | iOS |
---|---|
val scope = rememberCoroutineScope()
val sheetState = rememberAdaptiveSheetState()
var openBottomSheet by remember { mutableStateOf(false) }
Box(
modifier = Modifier.fillMaxSize()
) {
Button(
onClick = { openBottomSheet = true },
) {
Text("Show Bottom Sheet")
}
if (openBottomSheet) {
AdaptiveBottomSheet(
onDismissRequest = { openBottomSheet = false },
adaptiveSheetState = sheetState,
) {
Button(
onClick = {
scope.launch { sheetState.hide() }.invokeOnCompletion {
if (!sheetState.isVisible) {
openBottomSheet = false
}
}
}
) {
Text("Close")
}
}
}
}
AdaptiveCircularProgressIndicator
is a circular progress indicator that adapts to the platform it is running on. It is a wrapper around CircularProgressIndicator
on Android, and it implements similar look to UIActivityIndicatorView
on iOS.
Android | iOS |
---|---|
AdaptiveCircularProgressIndicator(
modifier = Modifier.size(50.dp),
color = Color.Red,
)
AdaptiveDatePicker
is a date picker that adapts to the platform it is running on. It is a wrapper around DatePicker
on Android and UIDatePicker
on iOS.
Android | iOS |
---|---|
val state = rememberAdaptiveDatePickerState()
LaunchedEffect(state.selectedDateMillis) {
// Do something with the selected date
}
AdaptiveDatePicker(
state = state,
)
AdaptiveTimePicker
is a time picker that adapts to the platform it is running on. It is a wrapper around TimePicker
on Android and UIDatePicker
on iOS.
Android | iOS |
---|---|
val state = rememberAdaptiveTimePickerState()
LaunchedEffect(state.hour, state.minute) {
// Do something with the selected time
}
AdaptiveTimePicker(
state = state,
modifier = Modifier
)
WebView
is a view that adapts to the platform it is running on. It is a wrapper around WebView
on Android and WKWebView
on iOS.
Android | iOS |
---|---|
val state = rememberWebViewState(
url = "https://github.com/MohamedRejeb"
)
LaunchedEffect(state.isLoading) {
// Get the current loading state
}
WebView(
state = state,
modifier = Modifier
.fillMaxSize()
)
Calf File Picker allows you to pick files from the device storage.
Android | iOS |
---|---|
val pickerLauncher = rememberFilePickerLauncher(
type = FilePickerType.Image,
selectionMode = FilePickerSelectionMode.Single,
onResult = { files ->
files.firstOrNull()?.let { file ->
// Do something with the selected file
// You can get the ByteArray of the file
file.readByteArray()
}
}
)
Button(
onClick = {
pickerLauncher.launch()
},
modifier = Modifier.padding(16.dp)
) {
Text("Open File Picker")
}
If you've found an error in this sample, please file an issue.
Feel free to help out by sending a pull request ❤️.
Support it by joining stargazers for this repository. ⭐
Also, follow me on GitHub for more libraries! 🤩
Copyright 2023 Mohamed Rejeb
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.