Skip to content

Instantly share code, notes, and snippets.

@ivanbuhov
Last active Jan 4, 2017
Embed
What would you like to do?
Proof of concept NativeScript app whcih is using HomeKit API
.title {
font-size: 30;
horizontal-align: center;
margin:20;
}
button {
font-size: 42;
horizontal-align: center;
}
.message {
font-size: 20;
color: #284848;
horizontal-align: center;
}
var application = require("application");
application.mainModule = "main-page";
application.cssFile = "./app.css";
application.start();
var vmModule = require("./main-view-model");
function pageLoaded(args) {
var page = args.object;
page.bindingContext = vmModule.mainViewModel;
}
exports.pageLoaded = pageLoaded;
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
<StackLayout>
<TextField width="300px" text="{{homeName}}" hint="Enter home name"/>
<Button text="Add it" tap="{{ addHome }}" />
<Button text="Remove it" tap="{{ removeHome }}" />
<ListView items="{{homes}}">
<ListView.itemTemplate>
<Label text="{{name}}" class="title" />
</ListView.itemTemplate>
</ListView>
</StackLayout>
</Page>
var observable = require("data/observable");
var MyHomeManagerDelegate = NSObject.extend({
get homesModel() {
return this._homesModel;
},
set homesModel(homesModel) {
this._homesModel = homesModel;
},
initWithHomesModel: function(homesModel) {
var self = this.super.init();
if (self) {
this.homesModel = homesModel;
}
return self;
},
homeManagerDidUpdateHomes : function(manager) {
this.homesModel.updateModel();
},
}, {
name: "MyHomeManagerDelegate",
protocols: [HMHomeManagerDelegate]
});
var HomesPageModel = (function (_super) {
__extends(HomesPageModel, _super);
function HomesPageModel() {
_super.call(this);
this.homeManagerDelegate = MyHomeManagerDelegate.alloc().initWithHomesModel(this);
this.homeManager = new HMHomeManager();
this.homeManager.delegate = this.homeManagerDelegate;
}
HomesPageModel.prototype.addHome = function () {
var newHomeName = this.get("homeName");
var homesPageModel = this;
this.homeManager.addHomeWithNameCompletionHandler(newHomeName, function(home, error) {
homesPageModel.updateModel();
if(error) {
homesPageModel.showError(error);
}
});
};
HomesPageModel.prototype.removeHome = function () {
var homeNameToRemove = this.get("homeName");
var homesPageModel = this;
var homeToRemove = null;
for(var i = 0; i < this.homeManager.homes.count; i++) {
var currentHome = this.homeManager.homes.objectAtIndex(i);
if(currentHome.name == homeNameToRemove) {
homeToRemove = currentHome;
break;
}
}
if(!homeToRemove) {
this.showErrorWithTitleAndMessage("Error", "There is no home with name " + homeNameToRemove);
return;
}
this.homeManager.removeHomeCompletionHandler(homeToRemove, function(error) {
homesPageModel.updateModel();
if(error) {
homesPageModel.showError(error);
}
});
};
HomesPageModel.prototype.showError = function (error) {
this.showErrorWithTitleAndMessage("Error", error.localizedDescription);
};
HomesPageModel.prototype.showErrorWithTitleAndMessage = function (title, errorMessage) {
var alertWindow = new UIAlertView();
alertWindow.title = title;
alertWindow.message = errorMessage;
alertWindow.addButtonWithTitle("OK");
alertWindow.show();
};
HomesPageModel.prototype.updateModel = function () {
var newHomes = [];
for(var i = 0; i < this.homeManager.homes.count; i++) {
var home = this.homeManager.homes.objectAtIndex(i);
newHomes.push({ name : home.name, primary: home.primary });
}
this.set("homes", newHomes);
}
return HomesPageModel;
})(observable.Observable);
exports.MyHomeManagerDelegate = MyHomeManagerDelegate;
exports.HomesPageModel = HomesPageModel;
exports.mainViewModel = new HomesPageModel();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment