開源項目分析(SwiftHub)Rxswift + MVVM + Moya 架構分析(一)

文章目錄

開源項目分析(SwiftHub)Rxswift + MVVM + Moya 架構分析(一)

1. SwiftHub項目簡介

SwiftHub 是大神Khoren Markosyan 寫的一個完全採用Rxswift + MVVM + Moya 的架構的項目,代碼很精簡,想學習MVVM架構的認真去研究這個項目的設計,對你以後的編程思想和習慣都會有很大的幫助。(點擊這裏下載:SwiftHub源碼

SwiftHub項目簡介

2. SwiftHub項目編譯,用到的第三方庫簡介

2.1 SwiftHub項目編譯

下載源碼後,進入SwiftHub-master主目錄,先要下載安裝第三方庫,如果你cd SwiftHub-master/ 就直接執行pod install的話一般都會報錯:

pod install的報錯

分析報錯原因不難看出,已經提示我們需要先pod repo update 一下更新你本地的cocos pod庫。

pod repo update一下

可能有的小夥伴網速不太好,pod install一直更新不了,這裏提供了一份我編譯好的源碼:鏈接:https://pan.baidu.com/s/1qwkjY_ZrgV9Y5yudiyVJdQ 密碼:60t7

2.2 SwiftHub項目用到的第三方框架

  • 我只能驚歎,哇塞,怎麼用了這麼多第三方框架啊,我個人觀點是不太主張用太多第三方框架,能自己實現都自己實現,除非要實現的功能必須要用第三方框架。因爲第三方框架會大大增加我們ipa包的大小,對於ipa大小有要求的是個災難,例如之前我們有一個項目使用Realm 作爲DB框架,但是發現這個框架實在是太佔內存了足足有將近90MB,而我只是想裏面一個小小的數據庫存儲相關的代碼,後面改成WCDB.swift框架,這個框架只有2MB左右。

  • 下面我們先來看一下SwiftHub 項目用到的第三方框架吧:

# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'

use_frameworks!
inhibit_all_warnings!

target 'SwiftHub' do
    # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
    # Pods for SwiftHub

    # Networking
    pod 'Moya/RxSwift', '14.0.0-beta.2'  # https://github.com/Moya/Moya
    pod 'Apollo', '0.19.0'  # https://github.com/apollographql/apollo-ios

    # Rx Extensions
    pod 'RxDataSources', '~> 4.0'  # https://github.com/RxSwiftCommunity/RxDataSources
    pod 'RxSwiftExt', '~> 5.0'  # https://github.com/RxSwiftCommunity/RxSwiftExt
    pod 'NSObject+Rx', '~> 5.0'  # https://github.com/RxSwiftCommunity/NSObject-Rx
    pod 'RxViewController', '~> 1.0'  # https://github.com/devxoul/RxViewController
    pod 'RxGesture', '~> 3.0'  # https://github.com/RxSwiftCommunity/RxGesture
    pod 'RxOptional', '~> 4.0'  # https://github.com/RxSwiftCommunity/RxOptional
    pod 'RxTheme', '~> 4.0'  # https://github.com/RxSwiftCommunity/RxTheme
    #pod 'RxAnimated', '~> 0.4'  # https://github.com/RxSwiftCommunity/RxAnimated

    # JSON Mapping
    #pod 'ObjectMapper', :git => 'https://github.com/kajensen/ObjectMapper.git'  # https://github.com/Hearst-DD/ObjectMapper
    pod 'Moya-ObjectMapper/RxSwift', :git => 'https://github.com/khoren93/Moya-ObjectMapper.git', :branch => 'moya14'  # https://github.com/ivanbruel/Moya-ObjectMapper

    # Image
    pod 'Kingfisher', '~> 5.0'  # https://github.com/onevcat/Kingfisher

    # Date
    pod 'DateToolsSwift', '~> 4.0'  # https://github.com/MatthewYork/DateTools
    pod 'SwiftDate', '~> 6.0'  # https://github.com/malcommac/SwiftDate

    # Tools
    pod 'R.swift', '~> 5.0'  # https://github.com/mac-cain13/R.swift
    pod 'SwiftLint', '0.37.0'  # https://github.com/realm/SwiftLint

    # Keychain
    pod 'KeychainAccess', '~> 4.0'  # https://github.com/kishikawakatsumi/KeychainAccess

    # Fabric
    pod 'Fabric'
    pod 'Crashlytics'

    # UI
    pod 'NVActivityIndicatorView', '~> 4.0'  # https://github.com/ninjaprox/NVActivityIndicatorView
    pod 'ImageSlideshow/Kingfisher', '~> 1.8'  # https://github.com/zvonicek/ImageSlideshow
    pod 'DZNEmptyDataSet', '~> 1.0'  # https://github.com/dzenbot/DZNEmptyDataSet
    pod 'Hero', '~> 1.5.0'  # https://github.com/lkzhao/Hero
    pod 'Localize-Swift', '~> 3.0'  # https://github.com/marmelroy/Localize-Swift
    pod 'RAMAnimatedTabBarController', '~> 5.0'  # https://github.com/Ramotion/animated-tab-bar
    pod 'AcknowList', '~> 1.8'  # https://github.com/vtourraine/AcknowList
    pod 'KafkaRefresh', '~> 1.0'  # https://github.com/OpenFeyn/KafkaRefresh
    pod 'WhatsNewKit', '~> 1.0'  # https://github.com/SvenTiigi/WhatsNewKit
    pod 'Highlightr', '~> 2.0'  # https://github.com/raspu/Highlightr
    pod 'DropDown', '~> 2.0'  # https://github.com/AssistoLab/DropDown
    pod 'Toast-Swift', '~> 5.0'  # https://github.com/scalessec/Toast-Swift
    pod 'HMSegmentedControl', '~> 1.0'  # https://github.com/HeshamMegid/HMSegmentedControl
    pod 'FloatingPanel', '~> 1.0'  # https://github.com/SCENEE/FloatingPanel
    pod 'MessageKit', '~> 3.0'  # https://github.com/MessageKit/MessageKit
    pod 'MultiProgressView', '~> 1.0'  # https://github.com/mac-gallagher/MultiProgressView

    # Keyboard
    pod 'IQKeyboardManagerSwift', '~> 6.0'  # https://github.com/hackiftekhar/IQKeyboardManager

    # Auto Layout
    pod 'SnapKit', '~> 5.0'  # https://github.com/SnapKit/SnapKit

    # Code Quality
    pod 'FLEX', :git => 'https://github.com/khoren93/FLEX.git', :branch => 'remove_private_api'  # https://github.com/Flipboard/FLEX
    pod 'SwifterSwift', '~> 5.0'  # https://github.com/SwifterSwift/SwifterSwift
    pod 'BonMot', '~> 5.0'  # https://github.com/Rightpoint/BonMot

    # Logging
    pod 'CocoaLumberjack/Swift', '~> 3.0'  # https://github.com/CocoaLumberjack/CocoaLumberjack

    # Analytics
    # https://github.com/devxoul/Umbrella
    pod 'Umbrella/Mixpanel', '~> 0.8'
    pod 'Umbrella/Firebase'
    pod 'Mixpanel', '~> 3.0'  # https://github.com/mixpanel/mixpanel-iphone
    pod 'Firebase/Analytics'

    # Ads
    pod 'Firebase/AdMob'
    pod 'Google-Mobile-Ads-SDK', '7.52.0'
    
    target 'SwiftHubTests' do
        inherit! :search_paths
        # Pods for testing
        pod 'Quick', '~> 2.0'  # https://github.com/Quick/Quick
        pod 'Nimble', '~> 8.0'  # https://github.com/Quick/Nimble
        #pod 'RxNimble', '~> 4.0'  # https://github.com/RxSwiftCommunity/RxNimble
        pod 'RxAtomic', :modular_headers => true
        pod 'RxBlocking'  # https://github.com/ReactiveX/RxSwift
        pod 'Firebase'
    end
end

target 'SwiftHubUITests' do
    inherit! :search_paths
    # Pods for testing
end


post_install do |installer|
    # Cocoapods optimization, always clean project after pod updating
    Dir.glob(installer.sandbox.target_support_files_root + "Pods-*/*.sh").each do |script|
        flag_name = File.basename(script, ".sh") + "-Installation-Flag"
        folder = "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"
        file = File.join(folder, flag_name)
        content = File.read(script)
        content.gsub!(/set -e/, "set -e\nKG_FILE=\"#{file}\"\nif [ -f \"$KG_FILE\" ]; then exit 0; fi\nmkdir -p \"#{folder}\"\ntouch \"$KG_FILE\"")
        File.write(script, content)
    end
    
    # Enable tracing resources
    installer.pods_project.targets.each do |target|
      if target.name == 'RxSwift'
        target.build_configurations.each do |config|
          if config.name == 'Debug'
            config.build_settings['OTHER_SWIFT_FLAGS'] ||= ['-D', 'TRACE_RESOURCES']
          end
        end
      end
    end
end

  • 接下來,我們來分析這些第三方庫都是用來幹什麼,說不定哪天你的項目也可以用到呢。

2.2.1 網絡庫

用到的網絡庫

2.2.1.1 Alamfire
2.2.1.2 Rxswift
2.2.1.3 Moya
  • 源碼下載:Moya

2.2.2 數據解析庫

數據解析庫

2.2.2.1 ObjectMapper
2.2.2.2 Moya-ObjectMapper/Swift

2.2.3 Rxswift 框架和相關擴展

2.2.3.1 RxDataSources
2.2.3.2 RxSwiftExt
2.2.3.3 NSObject+Rx
2.2.3.4 RxViewController
2.2.3.5 RxGesture
2.2.3.6 RxOptional
2.2.3.7 RxTheme
2.2.3.8 RxAnimated

2.2.4 圖像處理庫

2.2.4.1 Kingfisher

2.2.5 資源文件管理庫

2.2.5.1 R.swift
2.2.5.2 SwiftLint

2.2.6 祕鑰管理庫

2.2.6.1 KeychainAccess

2.2.7 自動佈局庫

2.2.7.1 SnapKit

2.2.8 UI相關庫

2.2.8.1 NVActivityIndicatorView
2.2.8.2 ImageSlidershow/Kingfisher
2.2.8.3 DZNEmptyDataSet
2.2.8.4 Hero
  • 源碼下載:Hero
2.2.8.5 Localize-Swift
2.2.8.6 RAMAnimatedTabBarController
2.2.8.7 AcknowList
2.2.8.8 KafkaRefresh
2.2.8.9 WhatsNewKit
2.2.8.10 Highlightr
2.2.8.11 DropDown
2.2.8.12 Toast-Swift
2.2.8.13 HMSegmentedControl
2.2.8.14 FloatingPanel
2.2.8.15 MessageKit
2.2.8.16 MultiProgressView
2.2.8.17 IQKeyboardManagerSwift

2.2.9 日誌管理庫

2.2.9.1 CocoaLumberjack/Swift

2.2.10 數據埋點庫

2.2.10.1 Umbrella
2.2.10.2 Umbrella/Mixpanel
2.2.10.3 Umbrella/Firebase
2.2.10.4 Mixpanel
2.2.10.5 Firebace/Analytics

2.2.11 廣告工具點庫

2.2.11.1 Firebase/AdMob
2.2.11.2 Google-Mobile-Ads-SDK

2.2.12 性能優化相關庫

2.2.12.1 Fabric
2.2.12.2 Crashlytics

2.2.13 其他工具類庫

2.2.13.1 FLEX
  • 源碼下載: FLEX
2.2.13.2 SwifterSwift
2.2.13.3 BonMot
2.2.13.4 DateToolsSwift
2.2.13.5 SwiftDate

3. SwiftHub項目採用的架構分析

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章