Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add swipe to dismiss functionality and duration control #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

amir-azari
Copy link

Add Swipe-to-Dismiss and Duration Control Features

This PR introduces swipe-to-dismiss functionality and enhanced duration control for AirySnackbar, making it more interactive and customizable.

Key Features

1. Swipe-to-Dismiss Implementation

  • Added SwipeConfig data class to manage swipe behavior with configurable parameters:

    • dismissThreshold: Determines how far the user needs to swipe (default: 30% of width)
    • animationDuration: Controls dismiss/reset animation speed (default: 200ms)
    • minAlpha and maxAlpha: Controls opacity during swipe (0.8 to 1.0)
    • swipeDirection: Supports LEFT_ONLY, RIGHT_ONLY, or BOTH directions
    • alphaProgressFactor: Controls opacity change rate during swipe
  • Implemented SwipeDismissTouchListener with:

    • Smooth touch handling and state management
    • Direction-aware swipe detection
    • Configurable animation and alpha transitions
    • Proper cleanup and reset behavior

2. Duration Control Enhancement

  • Added DurationAttribute sealed class with multiple duration options:
    • Indefinite: Snackbar stays visible until dismissed
    • Short: Uses Material's LENGTH_SHORT
    • Long: Uses Material's LENGTH_LONG (new default)
    • Custom: Allows custom duration in milliseconds
  • Added toDuration() extension function for easy conversion to BaseTransientBottomBar duration values

3. Integration and Usage

  • Added SwipeAttribute sealed class for easy configuration:
    • Enable: Activates swipe with customizable parameters
    • Disable: Turns off swipe functionality
  • Updated AirySnackbar to handle swipe configuration and duration settings
  • Added example usage in SampleUsageActivity demonstrating:
    • Indefinite duration with swipe-to-dismiss
    • Custom swipe configuration
    • Direction control

Example Usage

AirySnackbar.make(
    source = source,
    type = type,
    attributes = listOf(
        // ... other attributes ...
        DurationAttribute.Indefinite, // Make snackbar stay indefinitely
        SwipeAttribute.Enable(
            dismissThreshold = 0.3f,  // 30% width threshold
            swipeDirection = SwipeConfig.SwipeDirection.BOTH,
            minAlpha = 0.5f,
            maxAlpha = 1.0f
        )
        // Or default
        // SwipeAttribute.Enable()
    )
).show()

Impact

  • Improves user interaction by adding intuitive dismiss gesture
  • Provides more control over snackbar duration
  • Maintains smooth animations and transitions
  • Follows Material Design principles

Testing

  • Tested swipe behavior in all directions
  • Verified duration controls
  • Checked animation smoothness
  • Ensured proper cleanup after dismissal

@akndmr Please review the implementation, particularly the swipe threshold and animation parameters. Let me know if you'd like any adjustments to the default values or additional configuration options.

- Add `SwipeConfig` data class to manage swipe behavior.
- Add `SwipeDismissTouchListener` to handle touch events for dismissing.
- Add `SwipeAttribute` to control enabling/disabling swipe.
- Add `DurationAttribute` to manage the duration of the snackbar.
- Implement swipe-to-dismiss in `AirySnackbar` with configurable settings.
- Add `toDuration` extension to the Duration attribute.
- Set default duration as `LENGTH_LONG`.
- Add example with indefinite duration and swipe in `SampleUsageActivity`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant