Skip to content

Commit

Permalink
Added macOS support!
Browse files Browse the repository at this point in the history
  • Loading branch information
arturdev committed Mar 19, 2020
1 parent c65f3f9 commit c4f5856
Show file tree
Hide file tree
Showing 16 changed files with 1,486 additions and 107 deletions.
30 changes: 30 additions & 0 deletions Example/Example with Mac/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// AppDelegate.swift
// Example with Mac
//
// Created by Artur Mkrtchyan on 3/20/20.
// Copyright © 2020 CocoaPods. All rights reserved.
//

import Cocoa
import Unrealm

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

var windowController: WindowController!

func applicationDidFinishLaunching(_ aNotification: Notification) {

Realm.registerRealmables(ToDoItem.self)

windowController = NSStoryboard(name: "Main", bundle: nil).instantiateController(withIdentifier: "WindowController") as? WindowController
windowController.showWindow(self)
windowController.window?.makeKey()
}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"images" : [
{
"idiom" : "mac",
"size" : "16x16",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "16x16",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "32x32",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "32x32",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "128x128",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "128x128",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "256x256",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "256x256",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "512x512",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "512x512",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
6 changes: 6 additions & 0 deletions Example/Example with Mac/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
932 changes: 932 additions & 0 deletions Example/Example with Mac/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions Example/Example with Mac/Example_with_Mac.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
</dict>
</plist>
36 changes: 36 additions & 0 deletions Example/Example with Mac/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2020 CocoaPods. All rights reserved.</string>
<key>NSMainStoryboardFile</key>
<string>Main</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>NSSupportsAutomaticTermination</key>
<true/>
<key>NSSupportsSuddenTermination</key>
<true/>
</dict>
</plist>
22 changes: 22 additions & 0 deletions Example/Example with Mac/TableCellViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// TableCellViewModel.swift
// Example with Mac
//
// Created by Artur Mkrtchyan on 3/20/20.
// Copyright © 2020 CocoaPods. All rights reserved.
//

import Foundation

class TableCellViewModel: NSObject {
@objc dynamic var id: String
@objc dynamic var text: String
@objc dynamic var isCompleted: Bool

init(item: ToDoItem) {
self.id = item.id
self.text = item.text
self.isCompleted = item.isCompleted
super.init()
}
}
43 changes: 43 additions & 0 deletions Example/Example with Mac/ViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// ViewController.swift
// Example with Mac
//
// Created by Artur Mkrtchyan on 3/20/20.
// Copyright © 2020 arturdev. All rights reserved.
//

import Cocoa
import Unrealm

class ViewController: NSViewController {

@objc dynamic var items: [TableCellViewModel] = []

private var results: Unrealm.Results<ToDoItem>?
private var resultsToken: NotificationToken?

@IBOutlet weak var tableView: NSTableView!

override func viewDidLoad() {
super.viewDidLoad()

results = ToDoItem.all().sorted(byKeyPath: "id")
resultsToken = results?.observe { [weak self] _ in
self?.regenerateCellVMs()
}
}

private func regenerateCellVMs() {
guard let results = results else { return }
items = Array(results).map(TableCellViewModel.init)
}

@IBAction func completeActions(_ sender: NSButton) {
guard let results = results else { return }
let row = tableView.row(for: sender)
var item = results[row]
item.toggleCompleted()
}

}

30 changes: 30 additions & 0 deletions Example/Example with Mac/WindowController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// WindowController.swift
// Example with Mac
//
// Created by Artur Mkrtchyan on 3/20/20.
// Copyright © 2020 arturdev. All rights reserved.
//

import Cocoa

class WindowController: NSWindowController {

@IBAction func addTapped(_ sender: Any) {
let alert = NSAlert()
alert.messageText = "Please enter a value"
alert.addButton(withTitle: "Save")
alert.addButton(withTitle: "Cancel")

let inputTextField = NSTextField(frame: NSRect(x: 0, y: 0, width: 300, height: 24))
inputTextField.placeholderString = "Enter text"
alert.accessoryView = inputTextField
alert.beginSheetModal(for: NSApp.keyWindow!) { (modalResponse) in
if modalResponse == .alertFirstButtonReturn {
let text = inputTextField.stringValue
ToDoItem.add(text: text)
}
}
}

}
20 changes: 6 additions & 14 deletions Example/Podfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
platform :ios, '9.0'
use_frameworks!

target 'Unrealm_Example' do
platform :ios, '9.0'
pod 'Unrealm', :path => '../'

target 'Unrealm_Tests' do
Expand All @@ -10,7 +10,7 @@ target 'Unrealm_Example' do
end

target 'Example with Abstraction' do
platform :ios, '9.0'
target 'RealmStorage' do
inherit! :search_paths

Expand All @@ -19,18 +19,10 @@ target 'Example with Abstraction' do

end

post_install do |installer|
installer.pods_project.targets.each do |target|

#Fixing compile warning caused by CRuntime dependency
if target.name == 'CRuntime'
source_files = target.source_build_phase.files
dummy = source_files.find do |file|
file.file_ref.name == 'module.modulemap'
end
source_files.delete dummy
puts "Deleting source file #{dummy.inspect} from target #{target.inspect}."
end
end
target 'Example with Mac' do
platform :osx, '10.12'
pod 'Unrealm', :path => '../'

end

Loading

0 comments on commit c4f5856

Please sign in to comment.