Announce 1.1

Announce 1.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Nov 2017
SwiftSwift Version 4.0
SPMSupports SPM

Maintained by Vitor Travain.



A simple framework to display alert and notification messages in Swift

Example image

Requirements

  • iOS 8.0+
  • Xcode 8.0+

Usage

//Fire a quick message with a theme!
let message = Message(message: "A simple message", theme: .success)
announce(message, on: .view(aView), withMode: .timed(5.0))

//Fire a message with a title and customized appearance
let appearance = MessageWithTitleAppearance(foregroundColor: .white, backgroundColor: .red)
let message = MessageWithTitle(title: "A title", message: "A message", appearance: appearance)
let token = announce(message, on: .viewController(aViewController), withMode: .indefinite)

token.dismiss()

Can I use custom views?

Yes, you can! You just need those views to conform to the Announcement protocol and provide them a configurable Appearance and they should work fine. Also make sure the view does not break under the usage of leading and trailing NSLayoutConstraints as the default Presenter use them.

struct MyCustomAnnouncementAppearance: Appearance {
    let backgroundColor: UIColor
    
    static func defaultAppearance() -> MyCustomAnnouncementAppearance {
        return MyCustomAnnouncementAppearance(backgroundColor: .black)
    }
}

final class MyCustomAnnouncement: UIView, Announcement {
    let appearance: MyCustomAnnouncementAppearance
    
    init(appearance: MyCustomAnnouncementAppearance? = nil) {
        self.appearance = appearance ?? MyCustomAnnouncementAppearance.defaultAppearance()
    }
}

I don't like the default animations, can I write a custom one?

Yes, you can! Write a custom Presenter by conforming to the protocol. You can also ignore the default behavior that installs constraints if your custom view does not support it.

struct MyCustomPresenter: Presenter {
    let viewToDisplayReference: UIView

    @discardableResult func present<T: Announcement>(announcement: T) -> DismissalToken where T : UIView {
        // Install the view on the view to display, install constraints and create your own animation
        
        return DismissalToken {
            // Run the animations to dismiss the view, remove it from its context and etc
        }
    }
}

After creating your own presenter you can just call it by using:

let myCustomAnnouncement = MyCustomAnnouncement()
let myPresenter = MyCustomPresenter(viewToDisplayReference: UIView())

let token = announce(myCustomAnnouncement, withCustomPresenter: myPresenter)

token.dismiss()

I want to support the default themes in my view, how can I do it?

Just create an extension on Theme returning your appearance and create a convenience init on your view.

extension Theme {
    func appearanceForMyCustomView() -> MyCustomAppearance {
        switch self {
        case .success, .info, .warning, .danger:
            return MyCustomAppearance.defaultAppearance()        
        }
    }
}

final class MyCustomView: UIView, Announcement {
    //default implementation goes here
    
    convenience init(theme: Theme) {
        self.init(appearance: theme.appearanceForMyCustomView())
    }
}

Installation

Swift Package Manager

To use Announce as a Swift Package Manager package just add the following in your Package.swift file.

import PackageDescription

let package = Package(
    name: "HelloAnnounce",
    dependencies: [
        .Package(url: "https://github.com/corujautx/Announce.git", "1.0")
    ]
)

Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate Announce into your project manually.

Git Submodules

  • Open up Terminal, cd into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:
$ git init
  • Add Announce as a git submodule by running the following command:
$ git submodule add https://github.com/corujautx/Announce.git
$ git submodule update --init --recursive
  • Open the new Announce folder, and drag the Announce.xcodeproj into the Project Navigator of your application's Xcode project.

    It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.

  • Select the Announce.xcodeproj in the Project Navigator and verify the deployment target matches that of your application target.

  • Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.

  • In the tab bar at the top of that window, open the "General" panel.

  • Click on the + button under the "Embedded Binaries" section.

  • You will see two different Announce.xcodeproj folders each with two different versions of the Announce.framework nested inside a Products folder.

    It does not matter which Products folder you choose from.

  • Select the Announce.framework.

  • And that's it!

The Announce.framework is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.

Embeded Binaries

  • Download the latest release from https://github.com/corujautx/Announce/releases
  • Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
  • In the tab bar at the top of that window, open the "General" panel.
  • Click on the + button under the "Embedded Binaries" section.
  • Add the downloaded Announce.framework.
  • And that's it!

Acknowledgments

Helio Costa for helping me with the color palette for themes

License

Announce is released under the MIT license. See LICENSE for details.