Skip to content

Commit

Permalink
Merge pull request #2 from didi/swift
Browse files Browse the repository at this point in the history
sync
  • Loading branch information
lixiang1994 authored May 28, 2020
2 parents 5d61342 + b6a5d17 commit 0f845a4
Show file tree
Hide file tree
Showing 18 changed files with 254 additions and 136 deletions.
34 changes: 34 additions & 0 deletions DoraemonKit-Swift.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Make sure to run `pod lib lint DoraemonKit.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
s.name = 'DoraemonKit-Swift'
s.version = '0.0.1'
s.summary = 'iOS各式各样的工具集合'
s.description = <<-DESC
iOS各式各样的工具集合 Desc
DESC

s.homepage = 'https://github.com/didi/DoraemonKit'
s.license = { :type => 'Apache-2.0', :file => 'LICENSE' }
s.author = { 'yixiang' => '[email protected]' }
s.source = { :git => 'https://github.com/didi/DoraemonKit.git', :tag => s.version.to_s }
s.ios.deployment_target = '9.0'

s.swift_versions = ['5', '5.1', '5.2']


s.default_subspec = 'Core'

s.subspec 'Core' do |ss|
ss.source_files = 'iOS/Swift/DoKitSwift/Src/**/*{.swift}'
ss.resource_bundles = {
'DoKitSwift' => 'iOS/DoraemonKit/Resource/**/*'
}
end
end

59 changes: 16 additions & 43 deletions iOS/Swift/DoKitSwift/Src/DoKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,39 @@
//

public class DoKit {
public var pluginArray: Array<Dictionary<String, Any>>
public static let shared = DoKit()
public var isShowDoKit: Bool {
get {
return !entryWindow.isHidden
}
}

var pluginMap = [String: [Plugin]]()
var modules = [String]()
var entryWindow:DoKitEntryWindow
var originalPluginArray: Array<DoKitPluginModel>
private init() {
originalPluginArray = [DoKitPluginModel]()
pluginArray = [Dictionary<String, Any>]()

let startPoint = CGPoint(x: 0, y: kScreenHeight/3)
entryWindow = DoKitEntryWindow(frame: CGRect(x: startPoint.x, y: startPoint.y, width: 58, height: 58))
entryWindow.show()
}

public func install() {
self.addPlugin(title: DoKitLocalizedString("应用设置"), icon: "doraemon_setting", plugin: "DoKitAppSettingPlugin", module: DoKitLocalizedString("常用工具"))
self.addPlugin(title: DoKitLocalizedString("清理缓存"), icon: "doraemon_qingchu", plugin: "DoKitDelSanboxPlugin", module: DoKitLocalizedString("常用工具"))

var modules = Array<String>()
for plugin in originalPluginArray {
let module = plugin.module
if !modules.contains(module!) {
modules.append(module!)
}
}

for module in modules {
var moduleDic = [String: Any]()
moduleDic["module"] = module
var plugins = [DoKitPluginModel]()
for plugin in originalPluginArray {
if module == plugin.module {
plugins.append(plugin)
}
}
moduleDic["pluginArray"] = plugins
pluginArray.append(moduleDic)
self.addPlugin(plugin: DoKitAppSettingPlugin())
self.addPlugin(plugin: DoKitDelSanboxPlugin())
self.addPlugin(plugin: DoKitMainThreadCheckerPlugin())
}

public func addPlugin(plugin:Plugin){
if pluginMap[plugin.module] != nil {
pluginMap[plugin.module]?.append(plugin)
}else{
self.modules.append(plugin.module)
pluginMap[plugin.module] = [plugin]
}
}

public func addPlugin(title: String, icon: String, plugin: String, module: String) {
let pluginModel = DoKitPluginModel()
pluginModel.title = title
pluginModel.icon = icon
pluginModel.plugin = plugin
pluginModel.module = module
originalPluginArray.append(pluginModel)
public func addPlugin(module: String,title: String, icon: UIImage?,didLoad:@escaping ()->Void){
let plugin = DefaultPlugin.init(module: module, title: title, icon: icon, callBack: didLoad)
self.addPlugin(plugin: plugin)
}

public func showDoKit() {
Expand All @@ -74,11 +55,3 @@ public class DoKit {
}

}

public class DoKitPluginModel {
var title: String!
var icon: String!
var plugin: String!
var module: String!
}

4 changes: 2 additions & 2 deletions iOS/Swift/DoKitSwift/Src/Entry/Home/Cell/DoKitHomeCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class DoKitHomeCell: UICollectionViewCell {
fatalError("init(coder:) has not been implemented")
}

func update(name:String, icon:String) {
iconView.image = UIImage.dokitImageNamed(name: icon)
func update(name:String?, icon:UIImage?) {
iconView.image = icon
nameLabel.text = name

}
Expand Down
61 changes: 17 additions & 44 deletions iOS/Swift/DoKitSwift/Src/Entry/Home/DoKitHomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import UIKit

class DoKitHomeViewController: DoKitBaseViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

var pluginArray: Array<Dictionary<String, Any>> = DoKit.shared.pluginArray
var collectionView: UICollectionView!
let DoKitHomeCellID = "DoKitHomeCellID"
let DoKitHomeCloseCellID = "DoKitHomeCloseCellID"
Expand All @@ -35,28 +34,23 @@ class DoKitHomeViewController: DoKitBaseViewController, UICollectionViewDelegate
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
return pluginArray.count+1
return DoKit.shared.modules.count + 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section < pluginArray.count {
let moduleDic: Dictionary<String, Any> = pluginArray[section]
let plugins: Array<DoKitPluginModel> = moduleDic["pluginArray"] as! Array<DoKitPluginModel>
return plugins.count
}else{
return 1
if section < DoKit.shared.modules.count {
return DoKit.shared.pluginMap[DoKit.shared.modules[section]]?.count ?? 0
}
return 1
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let row = indexPath.row
let section = indexPath.section
if section < pluginArray.count {
if section < DoKit.shared.modules.count {
let homeCell: DoKitHomeCell = collectionView.dequeueReusableCell(withReuseIdentifier: DoKitHomeCellID, for: indexPath) as! DoKitHomeCell
let moduleDic: Dictionary<String, Any> = pluginArray[section]
let plugins: Array<DoKitPluginModel> = moduleDic["pluginArray"] as! Array<DoKitPluginModel>
let pluginModel: DoKitPluginModel = plugins[row]
homeCell.update(name: pluginModel.title, icon: pluginModel.icon)
let plugin = DoKit.shared.pluginMap[DoKit.shared.modules[section]]?[row]
homeCell.update(name: plugin?.title, icon: plugin?.icon)
return homeCell
}else{
let closeCell: DoKitHomeCloseCell = collectionView.dequeueReusableCell(withReuseIdentifier: DoKitHomeCloseCellID, for: indexPath) as! DoKitHomeCloseCell
Expand All @@ -69,17 +63,15 @@ class DoKitHomeViewController: DoKitBaseViewController, UICollectionViewDelegate
if kind == UICollectionView.elementKindSectionHeader {
let headView: DoKitHomeHeadView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: DoKitHomeHeadCellID, for: indexPath) as! DoKitHomeHeadView
let section = indexPath.section
if section < pluginArray.count {
let moduleDic: Dictionary<String, Any> = pluginArray[section]
let title = moduleDic["module"]
headView.renderUI(title: title as! String)
if section < DoKit.shared.modules.count {
headView.renderUI(title: DoKit.shared.modules[section])
}
view = headView

}else if kind == UICollectionView.elementKindSectionFooter {
let footerView: DoKitHomeFooterView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: DoKitHomeFootCellID, for: indexPath) as! DoKitHomeFooterView
let section = indexPath.section
if section >= pluginArray.count {
if section >= DoKit.shared.modules.count {
footerView.titleLabel.text = "\(DoKitLocalizedString("当前版本")): \(DoKitVersion)"
footerView.titleLabel.textColor = UIColor.hexColor(0x999999)
footerView.titleLabel.textAlignment = .center
Expand All @@ -95,31 +87,31 @@ class DoKitHomeViewController: DoKitBaseViewController, UICollectionViewDelegate
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.section < pluginArray.count {
if indexPath.section < DoKit.shared.modules.count {
return CGSize(width: kSizeFrom750_Landscape(160), height: kSizeFrom750_Landscape(128))
}else{
return CGSize(width: kScreenWidth, height: kSizeFrom750_Landscape(100))
}
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
if section < pluginArray.count {
if section < DoKit.shared.modules.count {
return CGSize(width: kScreenWidth, height: kSizeFrom750_Landscape(88))
}else{
return CGSize(width: kScreenWidth, height: 0)
}
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
if section < pluginArray.count {
if section < DoKit.shared.modules.count {
return CGSize(width: kScreenWidth, height: kSizeFrom750_Landscape(24))
}else{
return CGSize(width: kScreenWidth, height: kSizeFrom750_Landscape(80))
}
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
if section < pluginArray.count {
if section < DoKit.shared.modules.count {
return UIEdgeInsets(top: 0, left: kSizeFrom750_Landscape(24), bottom: kSizeFrom750_Landscape(24), right: kSizeFrom750_Landscape(24))
}else{
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
Expand All @@ -129,28 +121,9 @@ class DoKitHomeViewController: DoKitBaseViewController, UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let section = indexPath.section
let row = indexPath.row
if section < pluginArray.count {
let moduleDic: Dictionary<String, Any> = pluginArray[section]
let plugins: Array<DoKitPluginModel> = moduleDic["pluginArray"] as! Array<DoKitPluginModel>
let pluginModel: DoKitPluginModel = plugins[row]
let plugin = pluginModel.plugin
if let plugin = plugin {
guard let pluginClass = getClass(className: plugin) as? DoKitBasePlugin.Type else {
print("pluginClass not found")
return
}
let pluginInstance = pluginClass.init()
pluginInstance.pluginDidLoad()
}
}
}

// swift 类名有需要添加前缀
func getClass(className: String) -> AnyClass? {
guard let prefix = NSStringFromClass(type(of: self)).split(separator: ".").first else {
return nil
if section < DoKit.shared.modules.count {
let plugin = DoKit.shared.pluginMap[DoKit.shared.modules[section]]?[row]
plugin?.didLoad()
}
return NSClassFromString(prefix + ".\(className)")
}

}
17 changes: 0 additions & 17 deletions iOS/Swift/DoKitSwift/Src/Plugins/Base/DoKitBasePlugin.swift

This file was deleted.

26 changes: 26 additions & 0 deletions iOS/Swift/DoKitSwift/Src/Plugins/Base/Plugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// DoKitBasePlugin.swift
// AFNetworking
//
// Created by didi on 2020/5/26.
//

import UIKit

public protocol Plugin {
var module: String{get}
var title: String{get}
var icon: UIImage?{get}
func didLoad()
}

struct DefaultPlugin: Plugin {
var module: String
var title: String
var icon: UIImage?
var callBack:()->Void

func didLoad() {
self.callBack()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@
import Foundation


class DoKitAppSettingPlugin: DoKitBasePlugin{
override func pluginDidLoad() {
struct DoKitAppSettingPlugin: Plugin{
var module: String {
return DoKitLocalizedString("常用工具")
}

var title: String {
return DoKitLocalizedString("应用设置")
}

var icon: UIImage? {
return UIImage.dokitImageNamed(name: "doraemon_setting")
}

func didLoad() {
DoKitUtil.openAppSetting()
DoKitHomeWindow.shared.hide()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@

import UIKit

class DoKitDelSanboxPlugin: DoKitBasePlugin {
override func pluginDidLoad() {
struct DoKitDelSanboxPlugin: Plugin{
var module: String {
return DoKitLocalizedString("常用工具")
}

var title: String {
return DoKitLocalizedString("清理缓存")
}

var icon: UIImage? {
return UIImage.dokitImageNamed(name: "doraemon_qingchu")
}

func didLoad() {
let vc = DoKitDelSanboxViewController()
DoKitHomeWindow.shared.openPlugin(vc: vc)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// DoKitMainThreadCheckerPlugin.swift
// DoraemonKit
//
// Created by 邓锋 on 2020/5/28.
//

import Foundation

struct DoKitMainThreadCheckerPlugin: Plugin{
var module: String {
return DoKitLocalizedString("常用工具")
}

var title: String {
return DoKitLocalizedString("子线程UI")
}

var icon: UIImage? {
return UIImage.dokitImageNamed(name: "doraemon_ui")
}

func didLoad() {
let vc = DoKitMainThreadCheckerViewController()
DoKitHomeWindow.shared.openPlugin(vc: vc)
}
}
Loading

0 comments on commit 0f845a4

Please sign in to comment.