iT邦幫忙

2021 iThome 鐵人賽

0
Mobile Development

Flutter - 從 Packages & Plugins 掌握原生系列 第 28

Day28 Plugin 從零開始到上架 - iOS總結

  • 分享至 

  • xImage
  •  

InstagramManager


class InstagramManager {
    private let userUpdated: ((UserInfoResponse) -> Void)
    private let mediasUpdated: (([[String : Any]]) -> Void)
    private let albumDetailUpdated: (([[String : Any]]) -> Void)
    private let mediaItemUpdated: (([String : Any]) -> Void)
    private let errorUpdated: ((String) -> Void)

    init(userUpdated: @escaping (UserInfoResponse) -> Void,
         mediasUpdated: @escaping ([[String : Any]]) -> Void,
         albumDetailUpdated: @escaping ([[String : Any]]) -> Void,
         mediaItemUpdated: @escaping ([String : Any]) -> Void,
         errorUpdated: @escaping (String) -> Void) {
        self.userUpdated = userUpdated
        self.mediasUpdated = mediasUpdated
        self.albumDetailUpdated = albumDetailUpdated
        self.mediaItemUpdated = mediaItemUpdated
        self.errorUpdated = errorUpdated
    }
    
    func getUserInfo() {
        do {
            try AccessTokenRepository.shared.getUserInfo { response in
                self.userUpdated(response)
            }
        }catch(let error) {
            if(error as? InstagramErrors == InstagramErrors.tokenEmpty){
                self.errorUpdated("TOKEN_EMPTY")
                return
            }
            if(error as? InstagramErrors == InstagramErrors.tokenExpired){
                self.errorUpdated("TOKEN_EXPIRED")
                return
            }
            print(error.localizedDescription)
            self.errorUpdated("UNKNOWN_EXCEPTION")
        }
    }
    
    func getMedias() {
        do {
            try AccessTokenRepository.shared.getMedias { response in
                self.mediasUpdated(response)
            }
        }catch(let error) {
            if(error as? InstagramErrors == InstagramErrors.tokenEmpty){
                self.errorUpdated("TOKEN_EMPTY")
                return
            }
            if(error as? InstagramErrors == InstagramErrors.tokenExpired){
                self.errorUpdated("TOKEN_EXPIRED")
                return
            }
            print(error.localizedDescription)
            self.errorUpdated("UNKNOWN_EXCEPTION")
        }
    }
    
    func getAlbumDetail(albumId: String) {
        do {
            try AccessTokenRepository.shared.getAlbumDetail(albumId: albumId, completionHandler: { response in
                self.albumDetailUpdated(response)
            })
        }catch(let error) {
            if(error as? InstagramErrors == InstagramErrors.tokenEmpty){
                self.errorUpdated("TOKEN_EMPTY")
                return
            }
            if(error as? InstagramErrors == InstagramErrors.tokenExpired){
                self.errorUpdated("TOKEN_EXPIRED")
                return
            }
            print(error.localizedDescription)
            self.errorUpdated("UNKNOWN_EXCEPTION")
        }
    }
    
    func getMediaItem(mediaId: String) {
        do {
            try AccessTokenRepository.shared.getMediaItem(mediaId: mediaId, completionHandler: { response in
                self.mediaItemUpdated(response)
            })
        }catch(let error) {
            if(error as? InstagramErrors == InstagramErrors.tokenEmpty){
                self.errorUpdated("TOKEN_EMPTY")
                return
            }
            if(error as? InstagramErrors == InstagramErrors.tokenExpired){
                self.errorUpdated("TOKEN_EXPIRED")
                return
            }
            print(error.localizedDescription)
            self.errorUpdated("UNKNOWN_EXCEPTION")
        }
    }
    
    func logout(){
        AccessTokenRepository.shared.logout()
        self.userUpdated(UserInfoResponse(id: "", username: "", accountType: ""))
    }
    
    func hasFoundInstagramClient() -> Bool{
        guard Bundle.main.object(forInfoDictionaryKey: "INSTAGRAM_CLIENT_ID") as? String != nil,
              Bundle.main.object(forInfoDictionaryKey: "INSTAGRAM_CLIENT_SECRET") as? String != nil,
              Bundle.main.object(forInfoDictionaryKey: "REDIRECT_URI") as? String != nil else {
            self.errorUpdated("NOT_FOUND_IG_CLIENT")
            return false
        }
        return true
    }
}

SwiftInstagramBasicDisplayApiPlugin 總結

iOS 端,SwiftInstagramBasicDisplayApiPlugin在收到指令時,會由handleFlutterMethodCall處理傳來的事件,而方法等API都放至在InstagramManager,且在InstagramManager設置listeniing callback供SwiftInstagramBasicDisplayApiPlugin監聽,在執行完方法後回傳結果callback 給SwiftInstagramBasicDisplayApiPlugin,最後在透過MethodChannel回傳結果至Flutter 端


上一篇
Day27 Plugin 從零開始到上架 - iOS instagram APIs
下一篇
Day29 Plugin 從零開始到上架 - 上架
系列文
Flutter - 從 Packages & Plugins 掌握原生30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言