Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 56 additions & 93 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,128 +1,91 @@
# ToastView Sample App
# ToastView

The ToastView Sample App is a simple iOS application that demonstrates the usage of a custom ToastView to display toast messages with various configurations. The ToastView allows you to present messages in a non-intrusive way, and this app provides examples of two usage scenarios: using a single shared instance and using multiple isolated instances.
ToastView is a UIKit toast component and sample app. It supports a shared queued presenter for app-wide feedback as well as isolated presenters for independent view flows.

## Features

1. **Shared Instance Usage**
- Utilizes a single shared instance of ToastView.
- Calls `ToastView.shared.showToast(...)` to display toast messages.
- Convenient for scenarios where a single global toast view is sufficient.

<!-- Responsive Image using HTML -->
<p align="center">
<img src="ToastView/Resources/shared.png" alt="shared" style="max-width: 50%; height: auto;">
<img src="ToastView/Resources/shared.png" alt="Queued shared toast example" width="300">
<img src="ToastView/Resources/isolated.png" alt="Isolated toast example" width="300">
</p>

2. **Isolated Instance Usage**
- Uses multiple instances of ToastView.
- Calls `ToastView().showToast(...)` to display toast messages.
- Useful when you need separate and independent toast views.
## What it demonstrates

<!-- Responsive Image using HTML -->
<p align="center">
<img src="ToastView/Resources/isolated.png" alt="isolated" style="max-width: 50%; height: auto;">
</p>
- Programmatic UIKit and Auto Layout
- Shared queued presentation and isolated instances
- Position-aware Core Animation transitions
- Progress and prefix-image presentation modes
- Optional notification haptics
- Dynamic light/dark blur material
- App-wide theme configuration
- Swift Package Manager distribution

3. **Customizable Themes**
- Provides a `ToastViewTheme` class for customizing the appearance of toast messages.
- Easily adjust primary and secondary text colors, surface colors, and message font.
## Requirements

## Usage
- iOS 12+
- Swift 5.10+

### Shared Instance Usage
## Installation

```swift
ToastView.shared.showToast(
message: "Your message here",
on: parentView,
position: .aboveTopAnchor,
autoHide: true,
mode: .showProgress,
haptic: nil
)
In Xcode, select **File → Add Package Dependencies** and enter:

```text
https://github.com/Alenroyfeild/ToastView.git
```

### Isolated Instance Usage
The repository does not currently have a tagged release, so select the `main` branch or pin an exact commit.

## Usage

```swift
ToastView().showToast(
message: "Your message here",
on: parentView,
position: .aboveTopAnchor,
import ToastView

ToastView.shared.showToast(
message: "Uploading…",
on: view,
position: .aboveBottomAnchor,
autoHide: true,
mode: .showProgress,
haptic: nil
haptic: .success
)
```

### Customizing Themes
Use an isolated presenter when its queue and lifetime should be independent:

```swift
// Example: Customizing the ToastView theme
ToastViewTheme.primaryTextColor = .systemBlue
ToastViewTheme.primarySurfaceColor = .systemBackground
ToastViewTheme.secondaryTextColor = .label
ToastViewTheme.secondarySurfaceColor = .systemFill
ToastViewTheme.messageFont = UIFont.systemFont(ofSize: 15, weight: .light)
```

## ToastView Functions
let toast = ToastView()

### showToast Function

```swift
func showToast(
message: String,
on uiView: UIView,
position: Position = .aboveTopAnchor,
autoHide: Bool = false,
mode: Mode = .showProgress,
animationTime: TimeInterval = 0.25,
haptic type: UINotificationFeedbackGenerator.FeedbackType?
toast.showToast(
message: "Saved",
on: view,
mode: .showPrefixImage(image: UIImage(systemName: "checkmark.circle")!),
haptic: .success
)
```

- **Parameters:**
- `message`: The message to be displayed in the toast.
- `uiView`: The UIView on which the toast will be presented.
- `position`: The position at which the toast will be displayed (default is `.aboveTopAnchor`).
- `autoHide`: Boolean indicating whether the toast should automatically hide after being displayed (default is `false`).
- `mode`: The mode of the toast, such as showing progress or displaying a prefix image (default is `.showProgress`).
- `animationTime`: The duration of the toast animation (default is `0.25` seconds).
- `haptic type`: The type of haptic feedback to be triggered (optional).
Dismiss a presenter explicitly with `closeToast()`.

- **Description:**
- The `showToast` function is responsible for presenting the toast message with the specified configuration.
## Positions

## Customizing Themes with ToastViewTheme
- `.belowTopAnchor`
- `.aboveTopAnchor`
- `.aboveBottomAnchor`
- `.aboveCenter`

### ToastViewTheme Class
## Theme

```swift
public class ToastViewTheme {
public static var primaryTextColor: UIColor = .label
public static var primarySurfaceColor: UIColor = .systemFill
public static var secondaryTextColor: UIColor = .label
public static var secondarySurfaceColor: UIColor = .systemFill

public static var messageFont: UIFont = .systemFont(ofSize: 15, weight: .light)

public init(primaryTextColor: UIColor, primarySurfaceColor: UIColor, secondaryTextColor: UIColor, secondarySurfaceColor: UIColor, messageFont: UIFont) {
Self.primaryTextColor = primaryTextColor
Self.primarySurfaceColor = primarySurfaceColor
Self.secondaryTextColor = secondaryTextColor
Self.secondarySurfaceColor = secondarySurfaceColor
Self.messageFont = messageFont
}
}
ToastViewTheme.primaryTextColor = .label
ToastViewTheme.primarySurfaceColor = .systemFill
ToastViewTheme.secondaryTextColor = .label
ToastViewTheme.secondarySurfaceColor = .secondarySystemBackground
ToastViewTheme.messageFont = .preferredFont(forTextStyle: .subheadline)
```

- **Description:**
- The `ToastViewTheme` class allows you to customize the appearance of toast messages by adjusting primary and secondary text colors, surface colors, and message font.
Theme values are global. Set them during app configuration before presenting toasts.

## License
## Tests and current limits

The package includes an XCTest target, but meaningful queue/animation tests have not yet been implemented. Presentation is UIKit-based and must run on the main thread; this API does not currently enforce that with concurrency annotations.

This sample app is provided under the MIT License. Feel free to use and modify the code as needed.
## License

**Enjoy using the ToastView Sample App!**
No license file is currently included. The code is publicly viewable, but reuse or redistribution requires the author's permission until an explicit license is added.
13 changes: 7 additions & 6 deletions ToastView/Sources/ToastView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import UIKit

class ToastView {
public final class ToastView {
private struct Task {
let message: String
let uiView: UIView
Expand All @@ -18,15 +18,15 @@ class ToastView {
let hapticType: UINotificationFeedbackGenerator.FeedbackType?
}

enum Mode {
public enum Mode {
case showProgress, showPrefixImage(image: UIImage)
}

enum type {
case shared, isolated
}

enum Position: String, CaseIterable {
public enum Position: String, CaseIterable {
case belowTopAnchor, aboveTopAnchor, aboveBottomAnchor, aboveCenter
}

Expand All @@ -45,13 +45,14 @@ class ToastView {
CGAffineTransform(scaleX: 0.9, y: 0.9).translatedBy(x: 0, y: 24)
}

static let shared = ToastView()
public static let shared = ToastView()
public init() {}
private var taskQueue: [Task] = []
private var isTaskRunning = false
private var isSharedToastVisible = false // Track if a shared toast is currently visible

/// Shows the toast message with haptic feedback
func showToast(message: String, on uiView: UIView, position: Position = .aboveTopAnchor, autoHide: Bool = false, mode: Mode = .showProgress, animationTime: TimeInterval = 0.25, haptic type: UINotificationFeedbackGenerator.FeedbackType?) {
public func showToast(message: String, on uiView: UIView, position: Position = .aboveTopAnchor, autoHide: Bool = false, mode: Mode = .showProgress, animationTime: TimeInterval = 0.25, haptic type: UINotificationFeedbackGenerator.FeedbackType?) {
showToast(message: message, on: uiView, position: position, autoHide: autoHide, mode: mode, animationTime: animationTime, haptic: type, completion: { })
}

Expand Down Expand Up @@ -149,7 +150,7 @@ class ToastView {
}
}

func closeToast() {
public func closeToast() {
if self === Self.shared {
taskQueue.removeAll()
closeToast(completion: { })
Expand Down