Giter Site home page Giter Site logo

alhussein-anwar / sbquicklook Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sebastianbaar/sbquicklook

0.0 0.0 0.0 74 KB

Quickly preview local and remote files and their content using Apple's QuickLook Framework

License: MIT License

Swift 100.00%

sbquicklook's Introduction

SBQuickLook

iOS Build Swift License

Quickly preview local & external files and their content using Apple's QuickLook Framework.

This package provides a wrapper around Apple's QuickLook Framework.

  • Can preview:
    • iWork and Microsoft Office documents
    • Images
    • Live Photos
    • Text files
    • PDFs
    • Audio and video files
    • Augmented reality objects that use the USDZ file format (iOS and iPadOS only)
    • ZIP files
    • ...
  • Can be used with UIKit or SwiftUI
  • Can be used to preview one or multiple files at once.
  • Can be used with local file URLs or external URLs (it also handels downloading the files).

๐Ÿ“ฆ Installation

Add this Swift package in Xcode using its Github repository url. (File > Swift Packages > Add Package Dependency...)

๐Ÿš€ How to use

Initialize the View (SwiftUI) or ViewController (UIKit)

/// Initializes the `SBQuickLookView` or `SBQuickViewController` with the given file items and configuration.
///
/// - Parameters:
///   - fileItems: The `[SBQLFileItem]` data for populating the preview. Could be one or many items.
///   - configuration: Optional `SBQLConfiguration` configurations.
///   - completion: Optional `Result<SBQLError?, SBQLError>` completion.
///      - success: `QLPreviewController` successfully presented with at least one item. Optional `SBQLError` if some items failed to download.
///      - failure: `QLPreviewController` could not be  presented.
public init(
    fileItems: [SBQLFileItem],
    configuration: SBQLConfiguration? = nil,
    completion: ((Result<SBQLError?, SBQLError>) -> Void)? = nil) {
        self.fileItems = fileItems
        self.configuration = configuration
        self.completion = completion
    }

You have to pass the file items SBFileItem to the initializer. Either pass one item or multiple items to the View (SwiftUI) or ViewController (UIKit).

/// Initializes the file item with the given values.
///
/// - Parameters:
///   - url: `URL` of the item, choose between local file URLs or external URLs
///   - title: Optional title `String` to be displayed in the QuickLook controller.
///   - content: Optional media type `String` of the file; e.g. `"pdf"`, `"jpeg"`, ...
///   - urlRequest: Optional `URLRequest` used to download the item. The `url` is always set to `fileItem.url`. Default: `URLRequest(url: fileItem.url)`
public init(url: URL, title: String? = nil, mediaType: String? = nil, urlRequest: URLRequest? = nil) {
    self.url = url
    self.title = title
    self.mediaType = mediaType
    self.urlRequest = urlRequest
}

You can also pass an optional SBQLConfiguration to the initializer.

/// Initializes the file item with the given values.
///
/// - Parameters:
///   - urlRequest: Optional `URLSession`. Overrides the `URLSession` used by the download task. Default: `URLSession.shared`
///   - localFileDir: Optional local directory `URL`. Overrides the local directory `URL` used by the download task. Default: `FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)`
public init(session: URLSession? = nil, localFileDir: URL? = nil) {
    self.session = session
    self.localFileDir = localFileDir
}

SwiftUI

Present the SBQuickLookView (which is a UIViewControllerRepresentable wrapper for the SBQuickViewController) from your favorite View.

@State var isShown = false

let fileItems: [SBFileItem]
let configuration: SBQLConfiguration

init() {
    let localFileURL = Bundle.main.url(forResource: "sample-local-pdf", withExtension: "pdf")!
    
    fileItems = [
        SBFileItem(url: localFileURL, title: "LOCAL FILE"),
        SBFileItem(url: URL(string: "https://file-examples.com/storage/fe197d899c63f609e194cb1/2017/10/file_example_PNG_500kB.png")!, title: "Nice PNG Image", mediaType: "png")
    ]
    
    let localFileDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    configuration = SBQLConfiguration(localFileDir: localFileDir)
}

var body: some View {
    VStack {            
        Button {
            isShown.toggle()
        } label: {
            Text("Open QuickLook of files")
        }
    }
    .fullScreenCover(isPresented: $isShown, content: {
        SBQuickLookView(fileItems: fileItems, configuration: configuration) { result in
            switch result {
            case .success(let downloadError):
                if let downloadError {
                    print(downloadError)
                }
            case .failure(let error):
                switch error.type {
                case .emptyFileItems:
                    print("emptyFileItems")
                case .qlPreviewControllerError:
                    print("qlPreviewControllerError")
                case .download(let errorFileItems):
                    print("all items failed downloading")
                    print(errorFileItems)
                }
            }
        }
    })        
}

UIKit

Present the SBQuickViewController from your favorite ViewController.

let localFileURL = Bundle.main.url(forResource: "sample-local-pdf", withExtension: "pdf")!

let fileItems = [
    SBFileItem(url: localFileURL, title: "LOCAL FILE"),
    SBFileItem(url: URL(string: "https://file-examples.com/storage/fe197d899c63f609e194cb1/2017/10/file_example_PNG_500kB.png")!, title: "Nice PNG Image", mediaType: "png")
]

let localFileDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let configuration = SBQLConfiguration(localFileDir: localFileDir)

let qlController = SBQuickViewController(fileItems: fileItems, configuration: configuration)
qlController.modalPresentationStyle = .overFullScreen
present(qlController, animated: true)

๐ŸŽ“ Example

Example SwiftUI Project can be found here.

sbquicklook's People

Contributors

sebastianbaar avatar

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.