The library module is the core component of the SweetMeSoft KMP Library. It provides a robust architectural foundation and a comprehensive set of UI components, utilities, and network helpers for Kotlin Multiplatform development. It is designed to accelerate cross-platform application development by abstracting common patterns and boilerplate code.
Add the dependency to your version catalog or build file.
[versions]
sweetmesoft = "2.0.1"
[libraries]
sweetmesoft-library = { module = "com.sweetmesoft:library", version.ref = "sweetmesoft" }
implementation(libs.sweetmesoft.library)
This library promotes a consistent architecture using a suite of BaseScreen composables and BaseViewModel.
BaseScreen provides a standardized screen structure with built-in support for:
@Composable
fun HomeScreen() {
BaseScreen(
title = "Home",
showTop = true,
fabAction = { /* Handle FAB click */ }
) { paddingValues ->
// Screen content
Column(modifier = Modifier.padding(paddingValues)) {
Text("Welcome to the Home Screen")
}
}
}
In addition to BaseScreen, the library offers specialized screen layouts:
Manages a screen with a bottom navigation bar. It requires a list of BaseTab items.
@Composable
fun MainScreen() {
val homeTab = object : BaseTab {
override val baseOptions = defaultBaseTabOptions(
title = "Home",
icon = rememberVectorPainter(Icons.Default.Home),
showTop = true
)
@Composable
override fun Content() {
// Tab content
}
}
BaseBottomBarScreen(
tabs = listOf(homeTab, profileTab)
)
}
Manages a screen with a side navigation drawer. Similar to BaseBottomBarScreen, it uses BaseTab for navigation items.
@Composable
fun MainScreen() {
BaseDrawerScreen(
tabs = listOf(homeTab, settingsTab),
headerView = {
// Drawer header content
},
logoutAction = {
// Handle logout
}
)
}
A wrapper for screens displayed within a bottom sheet. It includes a standard top bar with a close button.
BaseBottomSheetScreen(
title = "Details",
showTop = true
) {
// Bottom sheet content
}
BaseViewModel serves as the base class for ViewModels, offering:
class HomeViewModel : BaseViewModel() {
fun checkCameraPermission() {
screenModelScope.launch {
val granted = requestPermission(Permission.CAMERA)
if (granted) {
// Permission granted
}
}
}
}
The library includes common screen implementations to speed up development.
A configurable splash screen with logo animation and automatic navigation after a delay.
SplashContent(
logo = painterResource(Res.drawable.logo),
waitMillis = 2000,
title = "My App",
action = {
// Navigate to next screen
navigator.replace(HomeScreen())
}
)
An “About” screen displaying app version, logo, and links.
AboutContent(
logo = painterResource(Res.drawable.logo),
appName = "My App",
appId = "com.example.myapp"
)
The library provides LocalList/RemoteList for linear lists and LocalGridList/RemoteGridList for grid layouts.
For displaying static or locally available data.
LocalList(
list = myDataList,
title = "My Items",
addClick = { /* Handle add */ }
) { index, item ->
Text(text = item.name)
}
LocalGridList(
list = myDataList,
columns = 2
) { index, item ->
MyGridItem(item)
}
Automatically handles data fetching, loading states, and pull-to-refresh from a URL.
RemoteList<MyDataItem>(
url = "https://api.example.com/items",
title = "Remote Items",
bearer = "token",
refresh = shouldRefresh
) { item ->
Text(text = item.name)
}
LocalDropDown and RemoteDropDown simplify selection interfaces.
RemoteDropDown<MyDataItem>(
url = "https://api.example.com/options",
title = "Select Option",
value = selectedOptionName,
selectValue = { item ->
// Handle selection
}
) { item ->
Text(text = item.name)
}
A standard row for settings screens with an icon, title, and description.
SettingsItem(
icon = Icons.Default.Settings,
title = "General",
description = "App preferences"
) {
// Handle click
}
A circular image component that handles loading from a URL and caching.
ProfilePhoto(
urlImage = "https://example.com/avatar.jpg",
radius = 100.dp,
onClick = { /* Handle click */ }
)
Input controls for numeric values.
DoublePicker(
title = "Amount",
value = amountStr,
onValueChange = { newValue ->
// Update value
}
)
CalculatorPopup(
visible = showCalculator,
onDismissRequest = { showCalculator = false },
onResult = { result ->
amount = result
showCalculator = false
}
)
The PopupHandler allows displaying global alerts from anywhere in your code (e.g., ViewModels).
// Display a simple alert
PopupHandler.displayAlert(
title = "Success",
message = "Operation completed successfully"
)
// Display a confirmation dialog
PopupHandler.displayConfirm(
title = "Delete Item",
message = "Are you sure?",
onConfirm = {
// Handle confirmation
}
)
// Display a progress dialog with updates
val job = scope.launch {
PopupHandler.displayProgress(
title = "Downloading",
cancelText = "Cancel",
progress = 0f
) {
// Handle cancellation
}
for (i in 1..100) {
delay(50)
PopupHandler.updateProgress(i / 100f)
}
PopupHandler.hideProgress()
}
NetworkUtils provides simplified HTTP methods that integrate with the global loading state.
// GET request
val result = NetworkUtils.get<MyResponseData>(
url = "https://api.example.com/data",
showLoading = true, // specific loading indicator control
bearer = "token"
)
result.onSuccess { response ->
// Handle success
}
result.onFailure { error ->
// Handle error
}
// POST request
val result = NetworkUtils.post<MyResponseData>(
url = "https://api.example.com/data",
body = myRequestObject
)
Helper functions to interact with the platform.
val platform = getPlatform() // Returns PlatformType.ANDROID or PlatformType.IOS
val version = getAppVersion()
openAppStore("com.example.app") // Opens the store page
// String extensions
val isValid = "test@example.com".isEmail()
val passwordValid = isValidPassword("StrongPass1!")
// Browser
openUrl("https://www.google.com")