Skip to content

Chaintech-Network/CMPToast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Maven Central Kotlin Compose Multiplatform License

badge-android badge-ios badge-desktop badge-web

CMPToast - Toasts for Compose Multiplatform

🍞️ CMPToast is a lightweight and easy-to-use library for displaying toast messages in Compose Multiplatform projects.

Hero-image - CMP-Chart 3

✨ Features

  • Cross-platform support: Works seamlessly on Android and iOS.
  • Composable API: Designed to integrate naturally into Jetpack Compose.
  • Customizable: Customize your toast message with text, duration, and appearance.

πŸ“¦ Installation

To use CMPToast in your Compose Multiplatform project, add the following dependency to your build.gradle.kts:

commonMain.dependencies {
  implementation("network.chaintech:cmptoast:1.0.7")
}


Note : Create an Application Class and add the following line (Otherwise you might face context error):

class MyApplication : Application() {
  override fun onCreate() {
    super.onCreate()
    multiplatform.network.cmptoast.AppContext.apply { set(applicationContext) }
  }
}


Also if you are getting Manifest Merger Failed issue, add the following to your Android Manifest file:

tools:replace="android:name"
tools:replace="android:allowBackup"

πŸš€ Usage

Basic Toast

To display a simple toast:

showToast(
  message = "This is a Toast"
)

Important : Desktop and Web Support

To display a toast add the ToastHost to your main composable inside your wasmJs or jvm target: Checkout the sample app for implementation

// For jvmMain target (..jvmMain/main.kt)
fun main() = application {
    Window(
        title = "CMPToast Demo",
        state = rememberWindowState(width = 800.dp, height = 600.dp),
        onCloseRequest = ::exitApplication,
    ) {
        window.minimumSize = Dimension(150, 150)
        App()

        ToastHost() // *** Important : Include this line to show the ToastHost
    }
}

// For wasmJs target (..wasmJsMain/main.kt)
@OptIn(ExperimentalComposeUiApi::class)
fun main() {
    ComposeViewport(document.body!!) {
        App()

        ToastHost() // *** Important : Include this line to show the ToastHost
    }
}

πŸŽ₯ Demo

Watch the demo below to see CMPToast in action:

Toast Demo


🎨 Customisation

⏳ Short Toast

To show a short-duration toast:

showToast(
  message = "This is Short Toast",
  backgroundColor = Color.White,
  textColor = Color.Black,
  duration = ToastDuration.Short
)

⏱️ Long Toast

To show a long-duration toast:

showToast(
  message = "This is Long Toast",
  backgroundColor = Color.White,
  textColor = Color.Black,
  duration = ToastDuration.Long
)

πŸ“ Top Toast

To show toast at top of the screen:

showToast(
  message = "This is Top Toast",
  backgroundColor = Color.White,
  textColor = Color.Black,
  gravity = ToastGravity.Top
)

🎯 Center Toast

To show toast at center of the screen:

showToast(
  message = "This is Center Toast",
  backgroundColor = Color.White,
  textColor = Color.Black,
  gravity = ToastGravity.Center
)

🎨 Colored Toast

To show a colored toast:

showToast(
  message = "This is Colored Toast",
  backgroundColor = Color(0xFF27AD9D)
)

🎨 Icon Toast

To show a toast with icon:

val resource = Res.drawable.ic_profile // Profile Icon
val iconBitmap = resource.let { imageResource(it) }

showToast(
  message = "This is Icon Toast",
  backgroundColor = Color.White,
  textColor = Color.Black,
  gravity = ToastGravity.Center,
  icon = iconBitmap,
  iconSizeDp = 20
)

βš™οΈ Properties

Property Type Description
message String The message to be displayed in the toast.
icon ImageBitmap? Optional icon to be displayed alongside the message.
iconSizeDp Dp The size of the icon in dp.
gravity ToastGravity The position of the toast (e.g., Top, Center, Bottom).
backgroundColor Color The background color of the toast.
textColor Color The color of the message text.
duration ToastDuration The duration of the toast (Short or Long).
padding ToastPadding? Internal padding for the toast content.
textSize TextUnit The size of the text.
topPadding Int Padding between toast and top of the screen.
bottomPadding Int Padding between toast and bottom of the screen.
cornerRadius Int? For rounding the corners of the toast.

πŸ–₯️ Platforms Supported

CMPToast currently supports the following platforms:

  • Android πŸ€–
  • iOS 🍎

πŸ“– Detailed Explanation

For an in-depth guide and detailed explanation, check out our comprehensive Medium Blog Post.

Medium

Stay connected and keep up with our latest innovations! πŸ’Ό Let's innovate together!

LinkedIn


πŸ“„ License

Copyright 2023 Mobile Innovation Network

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.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published