Skip to content

Commit

Permalink
add function
Browse files Browse the repository at this point in the history
  • Loading branch information
DamonHu committed Jul 16, 2021
1 parent 4764b9d commit e56cf9e
Show file tree
Hide file tree
Showing 16 changed files with 397 additions and 50 deletions.
9 changes: 9 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ZXKit-code/core

Copyright 2021 ZXKit and contributors.
All rights reserved.

This product includes software developed at
ZXKit (https://zxkit.com/).

This distribution has a binary dependency on zxkit/core, which is available under the Apache-2.0 License. The source code of zxkit/core can be found at https://github.com/zxkit-code/core.
61 changes: 60 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,61 @@
# ZXUserDefaultManager
UserDefault Manager

[中文文档](./README_zh.md)

iOS UserDefault data management, iOS UserDefault数据管理

This project is a built-in function plug-in of [ZXKitSwift](https://github.com/ZXKitCode/ZXKitSwift), and it can also be integrated separately and used as an independent function.

**If you have integrated `ZXKitSwift`, the file browser will be automatically displayed in the plug-in list, so there is no need to repeat the integration.**


## Independent integration

cocoapods

```ruby
pod 'ZXUserDefaultManager'
```

### Use

```swift
ZXUserDefaultManager.shared.start()
```

## Support `ZXKit`

**If you have integrated `ZXKitSwift`, the file browser will be automatically displayed in the plug-in list, so there is no need to repeat the integration.**

If you need to support `ZXKit`, you can use cocoapods to quickly integrate it

```ruby
pod 'ZXUserDefaultManager/zxkit'
```

then register to `ZXKit` in `AppDelegate`

```swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

ZXKit.regist(plugin: ZXUserDefaultManager.shared)

return true
}
```

## Preview

|File List|File Type Icon|
|----|----|
|![](./preview/demo2.png)|![](./preview/demo1.png)|

Function example

![](./preview/preview.gif)

## License

![](https://camo.githubusercontent.com/eb9066a6d8e0950066f3757c420e3a607c0929583b48ebda6fd9a6f50ccfc8f1/68747470733a2f2f7777772e6170616368652e6f72672f696d672f41534632307468416e6e69766572736172792e6a7067)

Base on Apache-2.0 License
64 changes: 64 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# ZXUserDefaultManager

iOS UserDefault data management, iOS UserDefault数据管理

该项目是[ZXKitSwift](https://github.com/ZXKitCode/ZXKitSwift)的内置功能插件,也可以单独集成当做独立功能使用。

**如果您已经集成了`ZXKitSwift`,插件列表中会自动显示该数据管理浏览器,不需要再重复集成。**


## 独立集成

cocoapods快速集成

```ruby
pod 'ZXUserDefaultManager'
```

### 使用

```swift
//显示管理器
ZXUserDefaultManager.shared.start()
```

## `ZXKit`个性化集成

**如果您已经集成了`ZXKitSwift`,插件列表中会自动显示该文件浏览器,不需要再重复集成。**

如果需要支持`ZXKit`,可以使用cocoapods快速集成

```ruby
pod 'ZXUserDefaultManager/zxkit'
```

之后可在`AppDelegate`的启动函数中注册到`ZXKit`即可

```swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

//ZXKit注册
ZXKit.regist(plugin: ZXUserDefaultManager.shared)

return true
}

```

## 预览


|文件列表|文件类型|
|----|----|
|![](./preview/demo2.png)|![](./preview/demo1.png)|

功能示例

![](./preview/preview.gif)


## License

![](https://camo.githubusercontent.com/eb9066a6d8e0950066f3757c420e3a607c0929583b48ebda6fd9a6f50ccfc8f1/68747470733a2f2f7777772e6170616368652e6f72672f696d672f41534632307468416e6e69766572736172792e6a7067)

Base on Apache-2.0 License
27 changes: 27 additions & 0 deletions ZXUserDefaultManager.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Pod::Spec.new do |s|
s.name = 'ZXUserDefaultManager'
s.swift_version = '5.0'
s.version = '0.0.1'
s.license= { :type => "Apache-2.0", :file => "LICENSE" }
s.summary = 'iOS UserDefault data management, iOS UserDefault数据管理'
s.homepage = 'https://github.com/ZXKitCode/ZXUserDefaultManager'
s.authors = { 'ZXKitCode' => '[email protected]' }
s.source = { :git => "https://github.com/ZXKitCode/ZXUserDefaultManager.git", :tag => s.version}
s.requires_arc = true
s.ios.deployment_target = '11.0'
s.subspec 'core' do |cs|
cs.resource_bundles = {
'ZXUserDefaultManager' => ['pod/assets/**/*']
}
cs.source_files = "pod/*.swift", "pod/view/*.swift", "pod/vc/*.swift", "pod/model/*.swift"
cs.dependency 'ZXKitUtil'
cs.dependency 'SnapKit'
end
s.subspec 'zxkit' do |cs|
cs.dependency 'ZXUserDefaultManager/core'
cs.dependency 'ZXKitCore/core'
cs.source_files = "pod/zxkit/*.swift"
end
s.default_subspecs = "core"
s.documentation_url = 'https://blog.hudongdong.com/ios/1169.html'
end
4 changes: 4 additions & 0 deletions example/ZXUserDefaultManager.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
E4292D5826A02F3A00D5F6E2 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = E4292D5526A02F3A00D5F6E2 /* Localizable.strings */; };
E4292D5A26A030EE00D5F6E2 /* ZXUserDefaultManager.png in Resources */ = {isa = PBXBuildFile; fileRef = E4292D5926A030EE00D5F6E2 /* ZXUserDefaultManager.png */; };
E4292D5D26A0476700D5F6E2 /* ZXDataCellModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4292D5C26A0476700D5F6E2 /* ZXDataCellModel.swift */; };
E4292D5F26A1392100D5F6E2 /* ZXUserDefaultEditVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4292D5E26A1392100D5F6E2 /* ZXUserDefaultEditVC.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -41,6 +42,7 @@
E4292D5726A02F3A00D5F6E2 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
E4292D5926A030EE00D5F6E2 /* ZXUserDefaultManager.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = ZXUserDefaultManager.png; sourceTree = "<group>"; };
E4292D5C26A0476700D5F6E2 /* ZXDataCellModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZXDataCellModel.swift; sourceTree = "<group>"; };
E4292D5E26A1392100D5F6E2 /* ZXUserDefaultEditVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZXUserDefaultEditVC.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -122,6 +124,7 @@
isa = PBXGroup;
children = (
E4292D4926A028C800D5F6E2 /* ZXUserDefaultVC.swift */,
E4292D5E26A1392100D5F6E2 /* ZXUserDefaultEditVC.swift */,
);
path = vc;
sourceTree = "<group>";
Expand Down Expand Up @@ -285,6 +288,7 @@
buildActionMask = 2147483647;
files = (
E4292D4A26A028C800D5F6E2 /* ZXUserDefaultVC.swift in Sources */,
E4292D5F26A1392100D5F6E2 /* ZXUserDefaultEditVC.swift in Sources */,
E4292D3826A01D6900D5F6E2 /* ViewController.swift in Sources */,
E4292D5D26A0476700D5F6E2 /* ZXDataCellModel.swift in Sources */,
E4292D4C26A028DB00D5F6E2 /* ZXUserDefaultManager.swift in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "446E0044-AD46-45DD-8583-8AF7C7C70535"
type = "0"
version = "2.0">
</Bucket>
1 change: 1 addition & 0 deletions pod/assets/localizable/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
"move here" = "move here";
"UserDefaultManager" = "UserDefaultManager";
"Hash value of the file" = "Hash value of the file";
"save" = "save";
1 change: 1 addition & 0 deletions pod/assets/localizable/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
"move here" = "移动到此处";
"UserDefaultManager" = "UserDefault管理";
"Hash value of the file" = "文件的hash值";
"save" = "保存";
3 changes: 2 additions & 1 deletion pod/model/ZXDataCellModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
import UIKit

class ZXDataCellModel: NSObject {

var key = ""
var value: Any = ""
}
148 changes: 148 additions & 0 deletions pod/vc/ZXUserDefaultEditVC.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//
// ZXUserDefaultEditVC.swift
// ZXUserDefaultManager
//
// Created by Damon on 2021/7/16.
//

import UIKit

class ZXUserDefaultEditVC: UIViewController {
private var model: ZXDataCellModel

init(model: ZXDataCellModel) {
self.model = model
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}


override func viewDidLoad() {
super.viewDidLoad()
let rightBarItem = UIBarButtonItem(title: "save".ZXLocaleString, style: .plain, target: self, action: #selector(_rightBarItemClick))
self.navigationItem.rightBarButtonItem = rightBarItem
self._createUI()
self._loadData()
}

//MARK: UI
lazy var mTitleLabel: UILabel = {
let tLabel = UILabel()
tLabel.textAlignment = .center
tLabel.numberOfLines = 2
tLabel.font = .systemFont(ofSize: 16, weight: .medium)
tLabel.textColor = UIColor.zx.color(hexValue: 0x333333)
return tLabel
}()

lazy var mSegment: UISegmentedControl = {
let segment = UISegmentedControl(items: ["Bool", "Number", "String", "Object"])
segment.addTarget(self, action: #selector(_changeType), for: .valueChanged)
return segment
}()
lazy var mTextView: UITextView = {
let textView = UITextView()
textView.font = .systemFont(ofSize: 14)
textView.layer.cornerRadius = 10
textView.layer.borderWidth = 1.0
textView.layer.borderColor = UIColor.zx.color(hexValue: 0xeeeeee).cgColor
return textView
}()
}

extension ZXUserDefaultEditVC {

@objc func _rightBarItemClick() {
switch self.mSegment.selectedSegmentIndex {
case 0:
UserDefaults.standard.set(mTextView.text.boolValue, forKey: model.key)
case 1:
UserDefaults.standard.set(Double(String(mTextView.text)) ?? 0, forKey: model.key)
case 2:
UserDefaults.standard.set(mTextView.text, forKey: model.key)
default:
if let jsonData = mTextView.text.data(using: .utf8), let object = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) {
UserDefaults.standard.set(object, forKey: model.key)
} else {
UserDefaults.standard.set(mTextView.text, forKey: model.key)
}
}
self.navigationController?.popViewController(animated: true)
}

func _createUI() {
self.view.backgroundColor = UIColor.zx.color(hexValue: 0xffffff)
self.view.addSubview(mTitleLabel)
mTitleLabel.snp.makeConstraints { make in
make.left.equalToSuperview().offset(10)
make.right.equalToSuperview().offset(-10)
make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(20)
}

self.view.addSubview(mSegment)
mSegment.snp.makeConstraints { make in
make.left.right.equalTo(self.mTitleLabel)
make.top.equalTo(mTitleLabel.snp.bottom).offset(30)
make.height.equalTo(34)
}

self.view.addSubview(mTextView)
mTextView.snp.makeConstraints { make in
make.left.equalToSuperview().offset(10)
make.right.equalToSuperview().offset(-10)
make.top.equalTo(mSegment.snp.bottom).offset(10)
make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom).offset(-20)
}
}

func _loadData() {
mTitleLabel.text = model.key
if model.value is Bool {
mSegment.selectedSegmentIndex = 0
} else if model.value is NSNumber {
mSegment.selectedSegmentIndex = 1
} else if model.value is String {
mSegment.selectedSegmentIndex = 2
} else if (model.value is NSArray || model.value is NSDictionary) {
mSegment.selectedSegmentIndex = 3
}
if JSONSerialization.isValidJSONObject(model.value), let jsonData = try? JSONSerialization.data(withJSONObject: model.value, options: .prettyPrinted) {
mTextView.text = String(data: jsonData, encoding: .utf8)
} else {
mTextView.text = "\(model.value)"
}
}

@objc func _changeType() {
switch self.mSegment.selectedSegmentIndex {
case 0:
mTextView.text = "\(model.value)".boolValue ? "true" : "false"
case 1:
if model.value is NSNumber {
mTextView.text = "\(model.value)"
} else {
mTextView.text = "\(Double("\(model.value)") ?? 0)"
}
default:
if JSONSerialization.isValidJSONObject(model.value), let jsonData = try? JSONSerialization.data(withJSONObject: model.value, options: .prettyPrinted) {
mTextView.text = String(data: jsonData, encoding: .utf8)
} else {
mTextView.text = "\(model.value)"
}
}
}
}

extension String {
var boolValue: Bool {
switch self.lowercased() {
case "false", "no", "0":
return false
default:
return !self.isEmpty
}
}
}
Loading

0 comments on commit e56cf9e

Please sign in to comment.