Skip to content

Commit

Permalink
Merge pull request #6 from k-lpmg/bugfix-perform
Browse files Browse the repository at this point in the history
Bugfix perform
  • Loading branch information
k-lpmg authored Feb 21, 2019
2 parents 1e5c1f0 + 297a5ad commit b5e2851
Show file tree
Hide file tree
Showing 14 changed files with 318 additions and 67 deletions.
7 changes: 1 addition & 6 deletions Example/Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let window = UIWindow(frame: UIScreen.main.bounds)
window.backgroundColor = .white
window.makeKeyAndVisible()

let controller = TableViewController()
let navigationController = UINavigationController(rootViewController: controller)
window.rootViewController = navigationController
self.window = window

AppNavigator.shared.start(with: window)
return true
}

Expand Down
25 changes: 25 additions & 0 deletions Example/Sources/AppNavigator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// AppNavigator.swift
// RealmWrapperExample
//
// Created by DongHeeKang on 20/02/2019.
// Copyright © 2019 k-lpmg. All rights reserved.
//

import UIKit

final class AppNavigator {

static let shared = AppNavigator()

// MARK: - Internal methods

func start(with window: UIWindow) {
let controller = TableViewController()
let navigationController = UINavigationController(rootViewController: controller)
window.rootViewController = navigationController
window.backgroundColor = .white
window.makeKeyAndVisible()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ struct UserInMemoryRealmProxy<RealmManager: InMemoryRealmManager>: RealmProxiabl
})
}

func append(_ users: [User], isSync: Bool, completion: (() -> Void)? = nil) {
rm.transaction(isSync: isSync, writeHandler: { (realm) in
realm.add(users, update: true)
}) { (realm, error) in
completion?()
}
}

func delete(_ user: User) {
rm.transaction(writeHandler: { (realm) in
realm.delete(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ struct UserRealmProxy<RealmManager: UserRealmManager>: RealmProxiable {
})
}

func append(_ users: [User], isSync: Bool, completion: (() -> Void)? = nil) {
rm.transaction(isSync: isSync, writeHandler: { (realm) in
realm.add(users, update: true)
}) { (realm, error) in
completion?()
}
}

func delete(_ user: User) {
rm.transaction(writeHandler: { (realm) in
realm.delete(user)
Expand Down
4 changes: 0 additions & 4 deletions Example/Sources/Extension/ClassName-Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ protocol ClassName {

// Swift Objects
extension ClassName {

static var className: String {
return String(describing: self).components(separatedBy: " ").first!
}

}

// Bridge to Obj-C
extension NSObject: ClassName {

class var className: String {
return String(describing: self).components(separatedBy: " ").first!
}

}
2 changes: 0 additions & 2 deletions Example/Sources/Extension/UITableView-Extension.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import UIKit

extension UITableView {

func registerWithCellReuseIdentifier<CellClass: UITableViewCell>(_ cellClass: CellClass.Type) {
self.register(cellClass, forCellReuseIdentifier: cellClass.className)
}

}
14 changes: 14 additions & 0 deletions Example/Sources/Extension/UIViewController-Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import UIKit

extension UIViewController {
func alert(title: String? = nil, message: String? = nil, preferredStyle: UIAlertController.Style = .alert, actions: [UIAlertAction]?, completion: (() -> Void)? = nil) {
let alert = UIAlertController(title: title, message: message, preferredStyle: preferredStyle)
if let actions = actions {
actions.forEach({ (action) in
alert.addAction(action)
})
}
present(alert, animated: true, completion: completion)
}

}
161 changes: 161 additions & 0 deletions Example/Sources/ViewControllers/MultipleAddViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import UIKit

import RealmSwift
import RealmWrapper

final class MultipleAddViewController: UIViewController {

// MARK: - Properties

private var isCountValid: Bool = false {
didSet {
saveButtonItem.isEnabled = isCountValid
}
}
private let useSyncLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 15, weight: .bold)
label.text = "Use Sync"
return label
}()
private let useSyncSwitch: UISwitch = {
let uiSwitch = UISwitch()
uiSwitch.translatesAutoresizingMaskIntoConstraints = false
return uiSwitch
}()
private let useInMemoryLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 15, weight: .bold)
label.text = "Use InMemory"
return label
}()
private let useInMemorySwitch: UISwitch = {
let uiSwitch = UISwitch()
uiSwitch.translatesAutoresizingMaskIntoConstraints = false
return uiSwitch
}()
private let countLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 15, weight: .bold)
label.text = "Count"
return label
}()
private let countTextField: UITextField = {
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.keyboardType = .numberPad
textField.textAlignment = .right
textField.layer.borderColor = UIColor.gray.cgColor
textField.layer.borderWidth = 0.5
return textField
}()

private lazy var saveButtonItem: UIBarButtonItem = {
let buttonItem = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(saveButtonDidClicked))
buttonItem.isEnabled = false
return buttonItem
}()

// MARK: - Overridden: UIViewController

override func viewDidLoad() {
super.viewDidLoad()

setNavigation()
setProperties()
setSelector()
view.addSubview(useSyncLabel)
view.addSubview(useSyncSwitch)
view.addSubview(useInMemoryLabel)
view.addSubview(useInMemorySwitch)
view.addSubview(countLabel)
view.addSubview(countTextField)
layout()
}

// MARK: - Private methods

private func setNavigation() {
title = "Add multiple user"
if #available(iOS 11.0, *) {
navigationItem.largeTitleDisplayMode = .never
}
navigationItem.rightBarButtonItem = saveButtonItem
}

private func setProperties() {
view.backgroundColor = .white
}

private func setSelector() {
countTextField.addTarget(self, action: #selector(countTextFieldEditingChanged(sender:)), for: .editingChanged)
}

// MARK: - Private selector

@objc private func countTextFieldEditingChanged(sender: UITextField) {
guard let text = sender.text else {return}

isCountValid = !text.isEmpty
}

@objc private func saveButtonDidClicked() {
guard let text = countTextField.text, let count = Int(text) else {return}

let useInMemory = useInMemorySwitch.isOn
let useSync = useSyncSwitch.isOn

DispatchQueue.global().async {
var users = [User]()
(0...count).forEach({ (i) in
let user = User(name: "\(i)", age: i)
users.append(user)

print("Appended users count : \(users.count)")
})

if useInMemory {
UserInMemoryRealmProxy().append(users, isSync: useSync)
} else {
UserRealmProxy().append(users, isSync: useSync)
}
}
navigationController?.popViewController(animated: true)
}

}

// MARK: - Layout

extension MultipleAddViewController {

private func layout() {
useSyncLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true
useSyncLabel.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: 32).isActive = true
useSyncLabel.widthAnchor.constraint(equalToConstant: 120).isActive = true

useSyncSwitch.topAnchor.constraint(equalTo: useSyncLabel.topAnchor).isActive = true
useSyncSwitch.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true

useInMemoryLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true
useInMemoryLabel.topAnchor.constraint(equalTo: useSyncLabel.bottomAnchor, constant: 32).isActive = true
useInMemoryLabel.widthAnchor.constraint(equalToConstant: 120).isActive = true

useInMemorySwitch.topAnchor.constraint(equalTo: useInMemoryLabel.topAnchor).isActive = true
useInMemorySwitch.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true

countLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true
countLabel.topAnchor.constraint(equalTo: useInMemoryLabel.bottomAnchor, constant: 32).isActive = true
countLabel.widthAnchor.constraint(equalToConstant: 120).isActive = true

countTextField.topAnchor.constraint(equalTo: countLabel.topAnchor).isActive = true
countTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true
countTextField.widthAnchor.constraint(equalToConstant: 85).isActive = true
countTextField.heightAnchor.constraint(equalToConstant: 32).isActive = true
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import UIKit
import RealmSwift
import RealmWrapper

final class AddViewController: UIViewController {
final class SingleAddViewController: UIViewController {

// MARK: - Properties

Expand Down Expand Up @@ -40,6 +40,9 @@ final class AddViewController: UIViewController {
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.placeholder = "Please enter user's name."
textField.textAlignment = .right
textField.layer.borderColor = UIColor.gray.cgColor
textField.layer.borderWidth = 0.5
return textField
}()
private let ageLabel: UILabel = {
Expand All @@ -54,6 +57,9 @@ final class AddViewController: UIViewController {
textField.translatesAutoresizingMaskIntoConstraints = false
textField.placeholder = "Please enter user's age."
textField.keyboardType = .numberPad
textField.textAlignment = .right
textField.layer.borderColor = UIColor.gray.cgColor
textField.layer.borderWidth = 0.5
return textField
}()

Expand Down Expand Up @@ -83,7 +89,7 @@ final class AddViewController: UIViewController {
// MARK: - Private methods

private func setNavigation() {
title = "Add User"
title = "Add single user"
if #available(iOS 11.0, *) {
navigationItem.largeTitleDisplayMode = .never
}
Expand Down Expand Up @@ -129,31 +135,33 @@ final class AddViewController: UIViewController {

// MARK: - Layout

extension AddViewController {
extension SingleAddViewController {

private func layout() {
useInMemoryLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 32).isActive = true
useInMemoryLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true
useInMemoryLabel.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: 32).isActive = true
useInMemoryLabel.widthAnchor.constraint(equalToConstant: 120).isActive = true

useInMemorySwitch.topAnchor.constraint(equalTo: useInMemoryLabel.topAnchor).isActive = true
useInMemorySwitch.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -32).isActive = true
useInMemorySwitch.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true

nameLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 32).isActive = true
nameLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true
nameLabel.topAnchor.constraint(equalTo: useInMemoryLabel.bottomAnchor, constant: 32).isActive = true
nameLabel.widthAnchor.constraint(equalToConstant: 120).isActive = true

nameTextField.leadingAnchor.constraint(equalTo: nameLabel.trailingAnchor, constant: 16).isActive = true
nameTextField.leadingAnchor.constraint(equalTo: nameLabel.trailingAnchor, constant: 8).isActive = true
nameTextField.topAnchor.constraint(equalTo: nameLabel.topAnchor).isActive = true
nameTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -32).isActive = true
nameTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true
nameTextField.heightAnchor.constraint(equalToConstant: 32).isActive = true

ageLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 32).isActive = true
ageLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true
ageLabel.topAnchor.constraint(equalTo: nameLabel.bottomAnchor, constant: 32).isActive = true
ageLabel.widthAnchor.constraint(equalToConstant: 120).isActive = true

ageTextField.leadingAnchor.constraint(equalTo: ageLabel.trailingAnchor, constant: 16).isActive = true
ageTextField.leadingAnchor.constraint(equalTo: ageLabel.trailingAnchor, constant: 8).isActive = true
ageTextField.topAnchor.constraint(equalTo: ageLabel.topAnchor).isActive = true
ageTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -32).isActive = true
ageTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true
ageTextField.heightAnchor.constraint(equalToConstant: 32).isActive = true
}

}
Expand Down
Loading

0 comments on commit b5e2851

Please sign in to comment.