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

IOS-9604 Added infinite interval to crouton and snackbar for the Swif… #324

Merged
merged 6 commits into from
Dec 20, 2023

Conversation

idenjoe
Copy link
Contributor

@idenjoe idenjoe commented Dec 4, 2023

🎟️ Jira ticket

IOS-9604 Add infinite interval to Crouton/Snackbar SwiftUI versions.

πŸ₯… What's the goal?

Add the same behaviour as in UIKit for crouton/snackbar SwiftUI versions with the infinite time invernal.

🚧 How do we do it?

  1. Add a new time interval enum (fiveSeconds, tenSeconds, and infinite)
  2. Added a block to return the dismiss reason(dismiss, button or timeout)
  3. Modified the structure of Snackbar view to fit with the new close button cases.

πŸ§ͺ How can I verify this?

Launch the Snackbar SwiftUI version in the catalog and show it with different settings.

πŸ‘ AppCenter build

qr

Comment on lines 12 to 27
public enum CroutonDismissInterval {
case fiveSeconds
case tenSeconds
case infinite

var timeInterval: TimeInterval? {
switch self {
case .fiveSeconds:
return 5
case .tenSeconds:
return 10
case .infinite:
return nil
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a more generic approach to ease adding more intervals in the future?

Suggested change
public enum CroutonDismissInterval {
case fiveSeconds
case tenSeconds
case infinite
var timeInterval: TimeInterval? {
switch self {
case .fiveSeconds:
return 5
case .tenSeconds:
return 10
case .infinite:
return nil
}
}
}
public enum CroutonDismissInterval {
case seconds(TimeInterval)
case infinite
var timeInterval: TimeInterval? {
switch self {
case .seconds(let value):
return value
case .infinite:
return nil
}
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't do this because you let the user to add any interval and its definition does not allow this.

Comment on lines 201 to 218
func normalizeDismissInterval(from action: (() -> Void)?, croutonDismissInterval: CroutonDismissInterval?) -> CroutonDismissInterval {
switch croutonDismissInterval {
case .none where action != nil:
return .tenSeconds
case .fiveSeconds where action != nil:
return .tenSeconds
case .tenSeconds where action == nil:
return .fiveSeconds
case .none:
return .fiveSeconds
case .fiveSeconds:
return .fiveSeconds
case .tenSeconds:
return .tenSeconds
case .infinite:
return .infinite
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this behavior specified in any document? It's confusing create a crouton with a dismiss interval of 5 seconds that dissappear after 10 seconds...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our components have a Readme so we can include it to explain this. Good idea!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhidalgofadrique
Copy link
Contributor

In the Mistica Catalog the crouton is being shown under the tabbar
Simulator Screenshot - iPhone 14 - 2023-12-05 at 08 55 01

@@ -25,38 +25,105 @@ public enum SnackbarButtonStyle {
case short
}

public struct Snackbar<Button: View>: View {
public enum SnackbarDismissReason: Int, RawRepresentable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could it be moved to MisticaCommon to avoid having the UIKit & SwiftUI representations?

case button
case timeout

public typealias RawValue = String
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird...
what about having?

- public enum SnackbarDismissReason: Int
+ public enum SnackbarDismissReason: String

Then, you can assign its String directly

    case dismiss = "DISMISS"
    case button = "BUTTON"
    case timeout = "TIMEOUT"

you will be able to remove var rawValue and init?(rawValue: RawValue)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the reason to MisticaCommon and now we need to mark the enum as @objc (it's being used from some methods marked as @objc) so we can not use String as their type.

@@ -9,23 +9,44 @@
import Foundation
import SwiftUI

public enum CroutonDismissInterval {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repeated in UIKit, could we put it in common?

Comment on lines 201 to 218
func normalizeDismissInterval(from action: (() -> Void)?, croutonDismissInterval: CroutonDismissInterval?) -> CroutonDismissInterval {
switch croutonDismissInterval {
case .none where action != nil:
return .tenSeconds
case .fiveSeconds where action != nil:
return .tenSeconds
case .tenSeconds where action == nil:
return .fiveSeconds
case .none:
return .fiveSeconds
case .fiveSeconds:
return .fiveSeconds
case .tenSeconds:
return .tenSeconds
case .infinite:
return .infinite
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our components have a Readme so we can include it to explain this. Good idea!

Comment on lines 220 to 225
func dismiss(with reason: SnackbarDismissReason) {
DispatchQueue.main.async {
withAnimation { isVisible = false }
dismissHandlerBlock?(reason)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I THINK you can do:

Suggested change
func dismiss(with reason: SnackbarDismissReason) {
DispatchQueue.main.async {
withAnimation { isVisible = false }
dismissHandlerBlock?(reason)
}
}
@MainActor
func dismiss(with reason: SnackbarDismissReason) {
withAnimation { isVisible = false }
dismissHandlerBlock?(reason)
}

buttonAction: {},
forceDismiss: true
)
assertSnapshot(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for adding tests and avoiding generating several brands for this :)!!

@@ -18,7 +18,10 @@ struct SnackbarCatalogView: View {
@State var buttonStyles: [SnackbarButtonStyle] = [.short, .large]
@State var presentingSnackbar = false
@State var presentingCrouton = false
@State var autoDismissDelay = 3
@State var autoDismissDelay: CroutonDismissInterval = .fiveSeconds
@State var intervalStyles: [CroutonDismissInterval] = [.fiveSeconds, .tenSeconds, .infinite]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a constant so it shouldn't be @State.
You can simplify this removing it and:

- autoDismissDelay: intervalStyles[selectedIntervalStyleIndex],
+ autoDismissDelay: CroutonDismissInterval.allCases[selectedIntervalStyleIndex],

wdyt?

@amegias
Copy link
Contributor

amegias commented Dec 5, 2023

Is it working properly?

Captura de pantalla 2023-12-05 a las 11 08 22

@idenjoe
Copy link
Contributor Author

idenjoe commented Dec 5, 2023

Is it working properly?

Captura de pantalla 2023-12-05 a las 11 08 22

Yes. The close button only should be added when the the time interval is infinite AND the action is nil OR the action is not nil and the forceDismiss is enabled.

idenjoe and others added 2 commits December 12, 2023 17:51
…erval with/without an action. Added the same config to the crouton version in UIKit.
switch interval {
case .fiveSeconds:
return "5"
case .tenSeconds:
return "10"
case .infinite:
case .infinity:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

infinite sounds better than infinity

CHANGELOG.md Outdated
@@ -8,7 +8,7 @@

### BREAKING CHANGES

* **crouton:** DismissHandlerBlock has changed and now has a CroutonControllerDismissReason as return parameter
* **crouton:** DismissHandlerBlock has changed and now has a SnackbarDismissReason as return parameter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't change this, it's generated automatically ;)

@amegias
Copy link
Contributor

amegias commented Dec 12, 2023

Should we extend this https://github.com/Telefonica/mistica-ios/blob/main/Sources/Mistica/Components/Crouton/README.md ?

Maybe something like:

Config

You can use several intervals with different features:

interval Close Action ...
5" NO NO ...
10" Optional Mandatory ...
... ... ... ...

Copy link
Contributor

@amegias amegias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SwiftUI

Comment on lines -71 to 40
withText text: String,
action: ActionConfig? = nil,
config: SnackbarConfig,
style: CroutonStyle = .info,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Managing one element with all the info, instead of multiple parameters

switch interval {
case .fiveSeconds:
return "5"
case .tenSeconds:
return "10"
case .infinite:
case .infinity(_):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If parameter is not needed, then it's enough with:

case .infinity:

Copy link
Contributor

@dhidalgofadrique dhidalgofadrique left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ‘ SwiftUI Skills +1 πŸ˜‚

@idenjoe idenjoe merged commit 74fce28 into main Dec 20, 2023
2 checks passed
@idenjoe idenjoe deleted the IOS-9604_Snackbar_infinite_SwiftUI branch December 20, 2023 09:16
@tuentisre
Copy link
Collaborator

πŸŽ‰ This PR is included in version 28.0.0 πŸŽ‰

The release is available on GitHub release

Your semantic-release bot πŸ“¦πŸš€

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants