InteractiveCapsuleOverlay

1.2.1

A Swift Package for presenting an interactive capsule over all other content (including sheets).
Lontronix/Interactive-Capsule-Overlay

What's New

1.2.1

2024-03-10T21:41:37Z

Change Log

  • Adds callback for when the dismiss button is pressed

Interactive Capsule Overlay

A Swift Package for presenting a configurable interactive capsule over all other content (including modal sheets).

A Demo of this package

Usage

Installation

Interactive Capsule Overlay is installed like any other Swift Package, just add this to the dependencies section of your Package.swift file

.package(url: "https://github.com/lontronix/Interactive-Capsule-Overlay", from: "1.1.0")

Add the showsInteractiveCapsuleOverlay View Modifier to the root view of your app.

import InteractiveCapsuleOverlay

@main
struct ExampleApp: App {
    var body: some Scene {
        WindowGroup {
            RootView()
              .showsInteractiveCapsuleOverlay()
        }
    }
}

Create a Custom Configuration

Create a custom CapsuleOverlayConfiguration

CapsuleOverlayConfiguration:

Parameter Type Description
title String Prominent text displayed in the capsule
timeoutInterval TimeInterval How long the capsule is displayed before being automatically dismissed
primaryAction ActionConfiguration The action that is invoked by tapping anywhere on the capsule but the secondary action button
secondaryAction ActionConfiguration the action that is invoked by tapping the secondary button
onDismissButtonPressed (() -> Void)? A closure that is called when the user taps the dismiss button

ActionConfiguration

case: disabled

The action is disable and hidden

case: enabled

The action is enabled and visible

Parameter Type Description
iconIdentifier String The icon used to represent the action (an SF Symbol name)
onPressed () -> Void A callback this library calls when the action is invoked by the user

PresentationMode

case: top

The overlay is presented from the top of the screen.

case: bottom

The overlay is presented from the bottom of the screen.

Parameter Type Description
yOffset CGFloat How far the capsule is offset from the top or bottom of the screen (based on the mode).

Note: The specified value is automatically negated for the bottom mode. (i.e. 20 means move the capsule 20 points up)

CapsuleOverlayConfiguration:

import InteractiveCapsuleOverlay

extension CapsuleOverlayConfiguration { 

    static func taskCompleted(accentColor: Color) -> CapsuleOverlayConfiguration {
        return CapsuleOverlayConfiguration(
            title: "Task Completed",
            primaryAction: .enabled(iconIdentifier: "slider.horizontal.3", onPressed: {
                // Do something
            }),
            timeoutInterval: 5,
            secondaryAction: .enabled(iconIdentifier: "arrow.uturn.backward.circle.fill", onPressed: {
                // Do something
            }),
            accentColor: accentColor
        )
    }

}

Display the Overlay

Use the showOverlay environment value to make the capsule appear with the desired configuration.

import InteractiveCapsuleOverlay

struct ContentView: View { 
    
    @Environment(\.showOverlay) private var showOverlay
    
    var body: some View {
        Button("Complete Task") {
            showOverlay(.taskCompleted(.blue))
        }
    }

}

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Fri Oct 18 2024 12:33:00 GMT-0900 (Hawaii-Aleutian Daylight Time)