CxjToasts

1.0.9

coxijcake/CxjToasts

What's New

2025-01-04T11:09:00Z

First release

CxjToasts

Effortless toast presentation with various interaction behaviors between displayed toasts. CxjToasts provides an intuitive and highly customizable solution for managing toast notifications in your iOS app.

Static Image


Table of Contents


Features

  • Predefined Templates: Ready-to-use toast designs for common scenarios.
  • Customizable Toasts: Tailor toasts to match your app's unique style.
  • Dynamic Layouts: Adapts seamlessly to different screen configurations.
  • Easy-to-Integrate API: Quickly add toast notifications with minimal setup.
  • iOS 14+ Compatibility: Fully supports modern and older devices.
  • Swift Modern Concurrency: Harness the power of Swift's async/await for toast management.
  • Flexible Animations: Easily configurable animation options.
  • Interaction Coordination: Smooth management of toast interaction behaviors.
  • Multiple Interaction Methods: Supports swipe, tap, or timeout-based dismissals.
  • Haptic Feedback Support: Enhance user experience with tactile feedback.

Video Examples of Toast Usage

This section demonstrates various examples of toast usage through videos. Each video showcases a specific type of toast behavior or interaction.

Animated GIF Animated GIF Animated GIF Animated GIF
Animated GIF Animated GIF Animated GIF Animated GIF

Example Project

To explore the full functionality of CxjToasts, check out the example project available in this repository.

The example project demonstrates:

  • Predefined templates for various toast types.
  • Customization options for titles, icons, backgrounds, and more.
  • Dynamic animations and interaction policies.
  • Live examples of toast stacking, hiding, and interaction coordination.

Simply clone the repository and open the example project to see CxjToasts in action.

Click here to view the example project.


Swift Version Support

You can find the most up-to-date information about Swift version support for CxjToasts on Swift Package Index:

Swift Versions

Privacy

CxjToasts does not collect any data. This notice is provided to assist you in completing App Privacy Details. Additionally, privacy manifest was included to be integrated into your app if needed.

Installation

Swift Package Manager

You can use The Swift Package Manager to install CxjToasts by adding the description to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/coxijcake/CxjToasts", from: "1.0.9")
]

CocoaPods

pod "CxjToasts"

Import

After installation, import the library into your project with:

import CxjToasts

How to Work with Toasts

The main entry point for managing toasts is the CxjToastsCoordinator.shared singleton. It provides methods for showing and dismissing toasts, as well as managing active toasts.

Initialization

Before using CxjToasts, it is recommended to initialize the library by calling:

CxjToasts.start()

This ensures that the CxjToastsCoordinator singleton is initialized and begins observing the keyboard state. This is particularly important for toasts that depend on the visibility or position of the keyboard, ensuring they are displayed correctly.

Displaying a Toast

Use the showToast method to display a toast. It returns an IdentifiableToast object, which can be used for further interactions.

  • @discardableResult showToast(type: animated:) -> IdentifiableToast:
CxjToastsCoordinator.shared.showToast(type: toastType, animated: true)
let toast = CxjToastsCoordinator.shared.showToast(type: toastType, animated: true)
CxjToastsCoordinator.shared.dismissToast(toast, animated: true)
  • type: The type of toast to display. It can be either:
    • templated(template: CxjToastTemplate): Use predefined templates for common toast types like success, error, or info.
    • custom(data: CxjToastType.CustomToastData): Provide custom configurations, view settings, and content for a fully customizable toast.
  • animated: Specifies whether the toast should appear with animation. Defaults to true.

Dismissing Toasts

You can dismiss specific toasts, toasts inside a view, or all active toasts:

  • dismissToast(toast: animated:):
  • dismissToastsInsideView(_ sourceView: animated:):
  • dismissAll(animated:):

Observing Toast States

You can observe the state of toasts by using the observer methods provided in CxjToastsCoordinator. These allow you to react to specific toast events, such as when a toast is presented or dismissed.

Adding and Removing Observers

To start observing toast events, use the add(observer:) and remove(observer:) methods:

CxjToastsCoordinator.shared.add(observer: someObserver)
CxjToastsCoordinator.shared.remove(observer: someObserver)

Observer Protocol Methods

The following methods are available for observers to implement, allowing you to handle toast lifecycle events:

  • willPresent(toast:): Called just before a toast is presented.
  • didPresent(toast:): Called immediately after a toast is presented.
  • willDismiss(toast:): Called just before a toast is dismissed.
  • didDismiss(toast:): Called immediately after a toast is dismissed.

Toast Types

CxjToastType

The CxjToastType enum defines the type of toast to display and has the following cases:

  1. Templated Toasts: Use predefined templates for common toast use cases.
  2. Custom Toasts: Create custom toasts by providing specific configuration, view settings, and content.

Custom Toast Configuration

For custom toasts, you need to create a CxjToastType.CustomToastData object.

CustomToastData Structure

  • config: A CxjToastConfiguration object that defines toast behavior, such as duration.
  • viewConfig: A CxjToastViewConfiguration object that defines visual properties, such as background color and corner radius.
  • content: A custom view conforming to CxjToastContentView that defines the actual content displayed in the toast.

Toast Configuration

The core structure for defining the behavior, appearance, and interaction of a toast. It provides a flexible API to customize animations, layout, dismissal methods, and more.

CxjToastConfiguration

Parameter Description
typeId Identifier for the toast, used to manage interactions such as spam protection and coexistence policies. It also allows for closing all toasts with a specific typeId. Note: typeId does not equal CxjToast.id and is not required to be unique.
sourceView The view associated with the toast. Often used for layout calculations or interactions.
sourceBackground Optional background configuration for the toast. Supports touch interactions and actions.
layout Layout configuration for the toast, including placement and constraints.
dismissMethods Defines how the toast can be dismissed, such as by swipe, tap, or automatic timeout.
keyboardHandling Configures behavior when a keyboard is visible, such as moving the toast above it.
animations Animation settings for toast presentation and dismissal.
hapticFeedback Haptic feedback for the toast (e.g., success, error, or custom).
spamProtection Enables or disables spam protection and defines criteria for toast comparison.
coexistencePolicy Defines how the toast interacts with others when a new toast is displayed.

View Configuration

Defines the visual and structural properties of the toast’s view. This configuration ensures that the toast seamlessly integrates into your app’s design and functionality.

CxjToastViewConfiguration

Parameter Description
contentLayout Determines how the content is laid out inside the toast view (e.g., fill with insets, specific constraints).
background Specifies the background style of the toast view, such as color, blur effect, gradient, or custom view.
shadow Configures the shadow appearance for the toast view.
corners Configures the corner style of the toast view, such as rounded or capsule.
isUserInteractionEnabled Specifies whether user interactions with the toast view are enabled.

Content Configuration

Easily configure the toast content without requiring manual layout, by using pre-defined templates or providing your own custom content view to display in the toast.

CxjToastContentConfiguration

Case Description
.info(type:) Displays an informational toast with text or text and an icon.
.action(config:infoContent:) Displays a toast with an action (e.g., a button) and optional informational content.
.undoAction(config:) Displays a toast with an undo action, such as a button to revert a previous operation.
.custom(contentView:) Allows for custom content by providing a custom view implementing CxjToastContentView.

InfoContentType

Case Description
.text(config:) Displays a text-only informational toast.
.textWithIcon(iconConfig:, textConfig:) Displays informational content with both text and an icon, configured separately.

Licence

CxjToasts is available under the MIT licence. See the LICENCE for more info.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon May 12 2025 20:02:00 GMT-0900 (Hawaii-Aleutian Daylight Time)