Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
1u0n committed May 29, 2017
1 parent 67d1987 commit 3f4746f
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 207 deletions.
10 changes: 0 additions & 10 deletions app/App_Resources/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> -->
<uses-permission android:name="android.permission.VIBRATE" />

<application
Expand Down Expand Up @@ -46,15 +45,6 @@
android:exported="false" />

<receiver android:name="com.tns.broadcastreceivers.EventReceiver" />
<!--in case in the future we want to add functionality for resetting the alarm under some events, we can use another BroadcastReceiver
<receiver android:name="com.tns.broadcastreceivers.AlarmRestarter">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.TIME_SET" />
</intent-filter>
</receiver>
-->

</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,6 @@
],
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "Juan Lopez"
}
}
2 changes: 1 addition & 1 deletion app/App_Resources/iOS/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "Juan Lopez"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,6 @@
],
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "Juan Lopez"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
],
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "Juan Lopez"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
],
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "Juan Lopez"
}
}
1 change: 1 addition & 0 deletions app/alarm-setup.android.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var applicationSettings = require("application-settings");

function setupAlarm(context) {

var alarmIntent = getIntent(context);
var alarmManager = context.getSystemService(android.content.Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(android.app.AlarmManager.RTC,
Expand Down
41 changes: 6 additions & 35 deletions app/app.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,21 @@
var application = require("application");
var applicationSettings = require("application-settings");


function setRunning() {
applicationSettings.setString("app.state", "running");
console.log("app state changed to Running");
}

function setStopped() {
applicationSettings.setString("app.state", "stopped");
console.log("app state changed to Stopped");
}

application.on(application.launchEvent, setLaunch);
application.on(application.resumeEvent, setResume);

application.on(application.suspendEvent, setSuspend);
application.on(application.exitEvent, setExit);
application.on(application.uncaughtErrorEvent, setError);

function setLaunch() {
applicationSettings.setString("app.state", "running");
applicationSettings.setBoolean("app.launched", true);
console.log("JUAN: LAUNCH EVENT");
}

function setResume() {
applicationSettings.setString("app.state", "running");
console.log("JUAN: RESUME EVENT");
}

function setSuspend() {
applicationSettings.setString("app.state", "stopped");
console.log("JUAN: SUSPEND EVENT");
}

function setExit() {
applicationSettings.setString("app.state", "stopped");
applicationSettings.setBoolean("app.launched", false);
console.log("JUAN: EXIT EVENT");
}
application.on(application.launchEvent, setRunning);
application.on(application.resumeEvent, setRunning);

function setError() {
applicationSettings.setString("app.state", "stopped");
applicationSettings.setBoolean("app.launched", false);
console.log("JUAN: ERROR EVENT");
}
application.on(application.suspendEvent, setStopped);
application.on(application.exitEvent, setStopped);
application.on(application.uncaughtErrorEvent, setStopped);



Expand Down
7 changes: 0 additions & 7 deletions app/broadcastreceivers/AlarmRestarter.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
android.content.BroadcastReceiver.extend("com.tns.broadcastreceivers.EventReceiver", {
onReceive: function(context, intent) {

var action = intent.getAction();
var serviceIntent = null;
if ("ACTION_START_NOTIFICATION_SERVICE" == action) {
console.log("onReceive from alarm, starting notification service! thread: " + java.lang.Thread.currentThread().getName());
serviceIntent = createIntentStartNotificationService(context);
} else if ("ACTION_DELETE_NOTIFICATION" == action) {
console.log("onReceive delete notification action, starting notification service to handle delete");
serviceIntent = createIntentDeleteNotification(context);
}

Expand Down
51 changes: 9 additions & 42 deletions app/notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,6 @@ var LocalNotifications = require("nativescript-local-notifications");
var applicationSettings = require("application-settings");


/*
function createNotification(title, text) {
if (applicationSettings.getBoolean("user.showNotifications", true))
return;
var context = utils.ad.getApplicationContext();
var builder = new android.app.Notification.Builder(context);
builder.setContentTitle(title)
.setAutoCancel(true)
.setContentText(text)
//.setVibrate([100, 200, 100])
.setSmallIcon(android.R.drawable.btn_star_big_on)
.setDefaults(android.app.Notification.DEFAULT_ALL); // .setDefaults( DEFAULT_SOUND | DEFAULT_VIBRATE | DEFAULT_LIGHTS )
// will open main NativeScript activity when the notification is pressed
var mainIntent = new android.content.Intent(context, com.tns.NativeScriptActivity.class);
var pendingIntent = android.app.PendingIntent.getActivity(context,
1,
mainIntent,
android.app.PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setDeleteIntent(getDeleteIntent(context));
var manager = context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
}
function getDeleteIntent(context) {
var intent = new android.content.Intent(context, com.tns.broadcastreceivers.EventReceiver.class);
intent.setAction("ACTION_DELETE_NOTIFICATION");
return android.app.PendingIntent.getBroadcast(context, 0, intent, android.app.PendingIntent.FLAG_UPDATE_CURRENT);
}
*/


function createNotification(title, text) {

if (!applicationSettings.getBoolean("user.showNotifications", true))
Expand All @@ -47,12 +10,16 @@ function createNotification(title, text) {
LocalNotifications.schedule([{
title: title,
body: text
}]).then(() => {
console.log("Notification scheduled");
}, (error) => {
console.log("ERROR", error);
}]).then((error) => {
console.error("Error creating notification: ", error);
});
}


module.exports.createNotification = createNotification;
function clearNotifications() {
LocalNotifications.cancelAll();
}


module.exports.createNotification = createNotification;
module.exports.clearNotifications = clearNotifications;
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,21 @@ com.pip3r4o.android.app.IntentService.extend("com.tns.notifications.BackgroundSe
function doService() {

var text;
console.log("Starting background service");

//if user doesn't want notifications, there's no reason to fetch them
if (!applicationSettings.getBoolean("user.showNotifications", true))
return;

//if app is running and on foreground, no need to do anything
if (applicationSettings.getString("app.state") == "running") {
console.log("Our app is on foreground, no need to do anything here");
if (applicationSettings.getString("app.state") == "running")
return;
}

var connectionType = connectivity.getConnectionType();
if (connectionType == connectivity.connectionType.none) {
console.log("No connectivity, nothing done in the service");
if (connectionType == connectivity.connectionType.none)
return;
}

if (applicationSettings.getString("server.ip", "") === "" || applicationSettings.getString("user.password", "") === "" || applicationSettings.getString("user.name", "") === "") {
console.log("Not configured yet, exiting background service");
if (applicationSettings.getString("server.ip", "") === "" || applicationSettings.getString("user.password", "") === "" || applicationSettings.getString("user.name", "") === "")
return;
}

http.request({
url: "http://" + applicationSettings.getString("server.ip") + ":" + applicationSettings.getString("server.port", "80") + "/sse/check",
Expand All @@ -48,10 +41,9 @@ function doService() {
content: JSON.stringify({ user: "myUserId", pass: "myPassword" })
}).then(function(response) {
respObj = response.content.toJSON();
console.log("Got a result from the server: " + response.content);
if (respObj.newNotifications > 0)
notifier.createNotification("Notifier", "you've got " + respObj.newNotifications + " new notifications!");
}, function(e) {
console.log("Error occurred while checking the server for updates: " + e);
console.error("Error occurred while checking the server for updates: " + e);
});
}
21 changes: 10 additions & 11 deletions app/views/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ var Toast = require("nativescript-toast");
var alarm = require("~/alarm-setup");


var page;
var configData;


exports.onNavigatingTo = function() {
console.log("PAGE onNavigatingTo");
};

exports.onLoaded = (args) => {
console.log("PAGE onLoaded");

exports.onNavigatingTo = (args) => {
configData = new Observable();
configData.userName = applicationSettings.getString("user.name", "");
configData.password = applicationSettings.getString("user.password", "");
Expand All @@ -25,17 +19,21 @@ exports.onLoaded = (args) => {
configData.port = applicationSettings.getString("server.port", "80");
configData.frequency = applicationSettings.getString("server.frequency", "10");

page = args.object;
page.bindingContext = configData;
args.object.bindingContext = configData;
}

exports.save = function() {

if (configData.showNotifications && !applicationSettings.getBoolean("user.showNotifications", true))
exports.save = function() {
if (configData.showNotifications && (!applicationSettings.getBoolean("user.showNotifications", true) || configData.frequency != applicationSettings.getString("server.frequency", "10")))
alarm.setupAlarm(utils.ad.getApplicationContext());
else if (!configData.showNotifications && applicationSettings.getBoolean("user.showNotifications", true))
alarm.cancelAlarm(utils.ad.getApplicationContext());

if (applicationSettings.getString("server.ip", "") != configData.ip || applicationSettings.getString("server.port", "80") != configData.port ||
applicationSettings.getString("user.password", "") != configData.password || applicationSettings.getString("user.name", "") != configData.userName) {
applicationSettings.setBoolean("config.changed", true);
}

applicationSettings.setString("user.name", configData.userName);
applicationSettings.setString("user.password", configData.password);
applicationSettings.setBoolean("user.showNotifications", configData.showNotifications);
Expand All @@ -47,6 +45,7 @@ exports.save = function() {
frameModule.topmost().goBack();
};


exports.goBack = function() {
frameModule.topmost().goBack();
}
2 changes: 1 addition & 1 deletion app/views/config/config.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Page xmlns="http://schemas.nativescript.org/tns.xsd" xmlns:myXml="common/xml" navigatingTo="onNavigatingTo" loaded="onLoaded">
<Page xmlns="http://schemas.nativescript.org/tns.xsd" xmlns:myXml="common/xml" navigatingTo="onNavigatingTo">

<Page.actionBar>
<ActionBar title="Configuration" style="padding-right:10">
Expand Down
8 changes: 4 additions & 4 deletions app/views/main/main-page.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
text-align: center;
margin-top: 5px;
margin-bottom: 1px;
padding: 10 0;
padding: 15 0;
border-bottom-color: #DDD;
border-bottom-width: 1;
}
Expand Down Expand Up @@ -52,7 +52,7 @@ ActionBar {
}

.clearButton {
margin: 15px 1px;
margin: 20px 1px 15px 1px;
font-size: 14;
font-weight: bold;
padding: 0 1;
Expand All @@ -70,11 +70,11 @@ ActionBar {
.listEntry1 {
/*border-bottom-width: 1;
border-bottom-color: #EEE;*/
padding: 7 13;
padding: 10 13;
}

.listEntry2 {
padding: 7 13;
padding: 10 13;
background-color: #FBFBFF
}

Expand Down
Loading

0 comments on commit 3f4746f

Please sign in to comment.