Giter Site home page Giter Site logo

jonasgessner / jgprogresshud-swiftui Goto Github PK

View Code? Open in Web Editor NEW
113.0 4.0 12.0 36 KB

Easily show HUDs with SwiftUI! Lightweight SwiftUI wrapper for JGProgressHUD for iOS, tvOS, Catalyst.

Home Page: https://jonasgessner.com

License: MIT License

Swift 100.00%
ios swift swiftui hud progresshud tvos maccatalyst

jgprogresshud-swiftui's Introduction

JGProgressHUD-SwiftUI

This is a lightweight and easy-to-use SwiftUI wrapper for JGProgressHUD, giving you access to the large and proven feature set of JGProgressHUD. Supports iOS, macCatalyst and tvOS 14.0+.

GitHub license SwiftPM compatible Mac Catalyst compatible

Installation

JGProgressHUD-SwiftUI can only be used with the Swift Package Manager.

In Xcode, use the menu File > Swift Packages > Add Package Dependency... and enter the package URL https://github.com/JonasGessner/JGProgressHUD-SwiftUI.git.

Usage

This package builds on top of the UIKit-based JGProgressHUD and provides a trivial way of showing HUDs in SwiftUI code.

You use the view JGProgressHUDPresenter as a canvas, which can display HUDs over its contents. Generally you want to use a single JGProgressHUDPresenter view as the root view of your application, such that full screen HUDs can be displayed.

To show a HUD from any contained view, define the environment object of type JGProgressHUDCoordinator in your view struct. This object is automatically available in the environment via JGProgressHUDPresenter. Call its showHUD(constructor:handleExistingHUD:) method, providing a constructor closure that returns the HUD to show. The returned HUD will be shown automatically, hence you do not need to call the show() method on the HUD. You may already schedule the HUD to disappear using the dismiss(afterDelay:) method.

The handleExistingHUD closure is only called in case a HUD is already presented. You can, for example, dismiss the current HUD and return true to continue showing the new HUD. By returning false (default), you prevent the new HUD from being shown.

You may furthermore access, modify or dismiss a HUD that is already presented by accessing the presentedHUD property of the JGProgressHUDCoordinator environment object. After a HUD dismisses, this property is automatically reset to nil.

Example

Also see the Example Project.

import SwiftUI
import JGProgressHUD_SwiftUI
 
struct NavigationBody: View {
    // This environment object is automatically set by JGProgressHUDPresenter.
    @EnvironmentObject var hudCoordinator: JGProgressHUDCoordinator

    var body: some View {
        Button("Press Me") {
            // Simply call showHUD and return your HUD!
            hudCoordinator.showHUD {
                let hud = JGProgressHUD()
                hud.textLabel.text = "Hello!"
                hud.dismiss(afterDelay: 3)

                return hud
            }
        }
    }
}

struct ContentView: View {
    var body: some View {
        // This presenter can present a fullscreen HUD.
        JGProgressHUDPresenter {
            NavigationView {
                NavigationBody()
            }
        }
    }
}

Requirements

  • Deployment target of iOS/tvOS/macCatalyst 14.0 or higher.

Detailed Documentation

Please see the JGProgressHUD project for documentation on JGProgressHUD itself.

See the doc strings in JGProgressHUD_SwiftUI.swift for more info.

License

MIT License.
© 2020, Jonas Gessner.

Credits

Created and maintained by Jonas Gessner, © 2020.

jgprogresshud-swiftui's People

Contributors

jonasgessner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jgprogresshud-swiftui's Issues

How to show in .sheet pages?

The ContentView is wrapped with JGProgressHUDPresenter and showing some HUDs, presenting a .sheet page (eg.: some PreferencesView) which is also wrapped with another JGProgressHUDPresenter. Calling show() in the sheet page only shows the HUD in the content view, behind the sheet view itself. (observed from Xcode view hierarchy)
Passing the hudCoordinator from ContentView to .sheet(), instead of wrapping .sheet with JGProgressHUDPresenter, does not help.
Any ideas on this? Or is there another (correct) way to show the HUD? Thanks!
(Falling back to use objc JGProgressHUD now)

Reacting to state changes

As I see the current state of art, I need to do dismiss(afterDelay: ) with the hud. What about dismissing it with when there is a change in a property of a view model. Let's say I want to hide after fetching is completed. I am doing it this way, I don't know whether it is a good approach or not.

           hudCoordinator.showHUD {
                    let hud = JGProgressHUD()
                    hud.textLabel.text = "Fetching Profile Details....."
                    hud.textLabel.font = UIFont(name: FontConstants.poppinsMedium, size: 12)
                    hud.backgroundColor = UIColor(white: 0, alpha: 0.4)
                    viewModel.$fetchedCompleted.sink { completed in
                        if completed {
                            hud.dismiss()
                        }
                    }.store(in: &viewModel.subscription)
                    return hud
                }

Can anybody guide me with this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.