iosswiftmoya

Use any Activity Indicator library with Moya


I am using Moya Networking in my iOS project. I made a class which has below method:

class UserAdapter{

    static func getUsers(amount:Int=2, gender:Gender = .Male, success successCallback: @escaping ([UserModelMain]) -> Void, error errorCallback: @escaping (Swift.Error) -> Void, failure failureCallback: @escaping (MoyaError) -> Void) {

        let endpointClosure = { (target: UserTarget) -> Endpoint<UserTarget> in
            let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: target)
            return defaultEndpoint.adding(newHTTPHeaderFields: ["": ""])
        }

        let provider = MoyaProvider<UserTarget>(endpointClosure: endpointClosure, plugins: [NetworkLoggerPlugin(verbose: true), NetworkActivityPlugin(networkActivityClosure: { (activity, target) in
            switch activity
            {
            case .began:
                print("Network Activity began")
                UIApplication.shared.isNetworkActivityIndicatorVisible = true
            case .ended:
                print("Network Activity ended")
                UIApplication.shared.isNetworkActivityIndicatorVisible = false
            }
        })])

        provider.request(.getUsers(amount: amount, gender: gender)) { (result) in
            switch result {
            case .success(let response):
                 //TODO

            }
        }
    } }

I am calling this from my view controller:

UserAdapter.getUsers(amount: 8, gender: .Female, success: { (response) in

    }, error: { (error) in
        // show error from server
    }, failure: { (error) in
        // show Moya error
    })

Instead of UIApplication.shared.isNetworkActivityIndicatorVisible I need to use any custom loading library like, NVActivityIndicatorView. How to do this?


Solution

  •     static func getUsers(amount:Int=2, 
    gender:Gender = .Male, 
    success successCallback: @escaping ([UserModelMain]) -> Void, 
    error errorCallback: @escaping (Swift.Error) -> Void, 
    failure failureCallback: @escaping (Moya.MoyaError) -> Void, 
    progress progressCallback: ProgressBlock?, 
    activity networkActivityCallback: @escaping (_ change: NetworkActivityChangeType) -> Void) {
    
                let userProvider = MoyaProvider<UserTarget>(endpointClosure: { (target: UserTarget) -> Endpoint<UserTarget> in
                    let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: target)
                    return defaultEndpoint.adding(newHTTPHeaderFields: ["": ""])
                }, plugins: [NetworkLoggerPlugin(verbose: true), NetworkActivityPlugin(networkActivityClosure: { (activity, target) in
                    UIApplication.shared.isNetworkActivityIndicatorVisible = (activity == .began)
                    networkActivityCallback(activity)
                    switch activity
                    {
                    case .began:
                        print("Network Activity began")
                    case .ended:
                        print("Network Activity ended")
                    }
                })])
        //Other statements
    
        }