eBay / NMessenger

A fast, lightweight messenger component built on AsyncDisplaykit and written in Swift
Other
2.42k stars 272 forks source link

Camera alerts not localized (actually, hard coded) #152

Open dodikk opened 7 years ago

dodikk commented 7 years ago

I'm talking about messages like this : "Allow access to your camera to start taking photos and uploading photos from your library with N1"

P.S. What does N1 mean?

dodikk commented 7 years ago

So far the approach is :

  1. Extract message literals to constants
  2. Extract a protocol for ModalAlertUtilities
  3. Replace static ModalAlertUtilities class with injected variable in NMessengerBarView
class NMessengerBarView:
{
...
...

open var alertUtils: IModalAlertUtilities = ModalAlertUtilities()
    {
        didSet
        {
            self.cameraVcProto.alertUtils = self.alertUtils
        }
    }

...
...
}
import Foundation
import UIKit

public protocol IModalAlertUtilities
{
    func postGenericErrorModal(fromController controller: UIViewController)

    func postGoToSettingToEnableCameraAndLibraryModal(
        fromController controller: UIViewController)

    func postGoToSettingToEnableCameraModal(fromController controller: UIViewController)

    func postGoToSettingToEnableLibraryModal(fromController controller: UIViewController)
}

//MARK: ModalAlertUtilities class
/**
 Custom alerts for NMessenger
 */
public struct ModalAlertUtilities: IModalAlertUtilities
{
    public let errorAlertTitle        : String
    public let errorAlertMessage      : String
    public let errorAlertDismissButton: String
    public let cameraAndLibraryMessage: String
    public let cameraOnlyMessage      : String
    public let libraryOnlyMessage     : String
    public let cancelButton           : String
    public let openSettingsMessage    : String

    public init()
    {
        self.errorAlertTitle   = "Error"
        self.errorAlertMessage = "An error occurred. Please try again later"
        self.errorAlertDismissButton = "Okay"

        self.cameraAndLibraryMessage =
        "Allow access to your camera & photo library to start uploading photos with N1"

        self.cameraOnlyMessage =
        "Allow access to your camera to start taking photos and uploading photos from your library with N1"

        self.libraryOnlyMessage =
        "Allow access to your photo library to start uploading photos from you library with N1"

        self.cancelButton = "Cancel"
        self.openSettingsMessage = "Go to Settings"
    }

    public init(
        errorAlertTitle: String,
        errorAlertMessage: String,
        errorAlertDismissButton: String,
        cameraAndLibraryMessage: String,
        cameraOnlyMessage: String,
        libraryOnlyMessage: String,
        cancelButton: String,
        openSettingsMessage: String)
    {
        self.errorAlertTitle         = errorAlertTitle
        self.errorAlertMessage       = errorAlertMessage
        self.errorAlertDismissButton = errorAlertDismissButton
        self.cameraAndLibraryMessage = cameraAndLibraryMessage
        self.cameraOnlyMessage       = cameraOnlyMessage
        self.libraryOnlyMessage      = libraryOnlyMessage
        self.cancelButton            = cancelButton
        self.openSettingsMessage     = openSettingsMessage
    }

    /**
     General error alert message
     - parameter controller: Must be UIViewController. Where to present to alert.
     */
    public func postGenericErrorModal(fromController controller: UIViewController)
    {
        let alert =
            UIAlertController(
                title: self.errorAlertTitle,
                message: self.errorAlertMessage,
                preferredStyle: .alert)

        let cancelAction =
            UIAlertAction(
            title: self.errorAlertDismissButton,
            style: .cancel,
            handler: nil)

        alert.addAction(cancelAction)
        DispatchQueue.main.async
        {
            controller.present(alert, animated: true, completion: nil)
        }
    }

    /**
     Camera permission alert message
     - parameter controller: Must be UIViewController. Where to present to alert.
     Alert tells user to go into setting to enable permission for both camera and photo library
     */
    public func postGoToSettingToEnableCameraAndLibraryModal(
                                        fromController controller: UIViewController)
    {
        // TODO: should be localized or injected from app
        //
        let message = self.cameraAndLibraryMessage

        let alert =
            UIAlertController(
                title: "",
                message: message,
                preferredStyle: .alert)

        let cancelAction = UIAlertAction(title: self.cancelButton, style: .destructive)
        {
            (action) in
            alert.dismiss(animated: true, completion: nil)
        }

        let settingsAction = UIAlertAction(title: self.openSettingsMessage, style: .default)
        {
            (alertAction) in

            if let appSettings = URL(string: UIApplicationOpenSettingsURLString)
            {
                UIApplication.shared.openURL(appSettings)
            }
        }

        alert.addAction(settingsAction)
        alert.addAction(cancelAction)

        DispatchQueue.main.async
        {
            controller.present(alert, animated: true, completion: nil)
        }
    }
    /**
     Camera permission alert message
     - parameter controller: Must be UIViewController. Where to present to alert.
     Alert tells user to go into setting to enable permission for camera
     */
    public func postGoToSettingToEnableCameraModal(fromController controller: UIViewController)
    {
        let alert =
            UIAlertController(
                title: "",
                message: self.cameraOnlyMessage,
                preferredStyle: .alert)

        let cancelAction =
            UIAlertAction(
                title: self.cancelButton,
                style: .destructive,
                handler: nil)

        let settingsAction = UIAlertAction(title: self.openSettingsMessage, style: .default)
        {
            (alertAction) in

            if let appSettings = URL(string: UIApplicationOpenSettingsURLString)
            {
                UIApplication.shared.openURL(appSettings)
            }
        }
        alert.addAction(settingsAction)
        alert.addAction(cancelAction)

        DispatchQueue.main.async
        {
            controller.present(alert, animated: true, completion: nil)
        }
    }
    /**
     Camera permission alert message
     - parameter controller: Must be UIViewController. Where to present to alert.
     Alert tells user to go into setting to enable permission for photo library
     */
    public func postGoToSettingToEnableLibraryModal(fromController controller: UIViewController)
    {
        let alert =
            UIAlertController(
                title: "",
                message: self.libraryOnlyMessage,
                preferredStyle: .alert)

        let cancelAction =
            UIAlertAction(
                title: self.cancelButton,
                style: .destructive,
                handler: nil)

        let settingsAction = UIAlertAction(title: self.openSettingsMessage, style: .default)
        {
            (alertAction) in

            if let appSettings = URL(string: UIApplicationOpenSettingsURLString)
            {
                UIApplication.shared.openURL(appSettings)
            }
        }
        alert.addAction(settingsAction)
        alert.addAction(cancelAction)

        DispatchQueue.main.async
        {
            controller.present(alert, animated: true, completion: nil)
        }
    }
}
dodikk commented 7 years ago

The messages can be configured from application's code :

public class ChatRoomView: NMessengerViewController
{

    public override func getInputBar() -> InputBarView
    {
           let result = NMessengerBarView(controller: self)

           ...
           ...

                   result.alertUtils =
            ModalAlertUtilities(
                errorAlertTitle:
                    NSLocalizedString(
                        "Chat.PhotoPicker.Alerts.Error.Title",
                        comment: "Error"),

                errorAlertMessage:
                    NSLocalizedString(
                        "Chat.PhotoPicker.Alerts.Error.Message",
                        comment: "An error occurred. Please try again later"),

                errorAlertDismissButton:
                    NSLocalizedString(
                        "Chat.PhotoPicker.Alerts.Error.DismissButton",
                        comment: "Okay"),

                cameraAndLibraryMessage:
                    NSLocalizedString(
                        "Chat.PhotoPicker.Alerts.Permission.CameraAndLibrary",
                        comment: "Allow access to your camera & photo library to start uploading photos with N1"),

                cameraOnlyMessage:
                    NSLocalizedString(
                        "Chat.PhotoPicker.Alerts.Permission.CameraOnly",
                        comment: "Allow access to your camera to start taking photos and uploading photos from your library with N1"),

                libraryOnlyMessage:
                    NSLocalizedString(
                        "Chat.PhotoPicker.Alerts.Permission.LibraryOnly",
                        comment: "Allow access to your photo library to start uploading photos from you library with N1"),

                cancelButton:
                    NSLocalizedString(
                        "Chat.PhotoPicker.Alerts.Permission.DismissButton",
                        comment: "Cancel"),

                openSettingsMessage:
                    NSLocalizedString(
                        "Chat.PhotoPicker.Alerts.Permission.SettingsButton",
                        comment: "Go to Settings")
            )

        return result
    }
}
dodikk commented 7 years ago

The Pull Request will follow soon.