Commit f2c9c989 authored by laurent l's avatar laurent l

background job for ios WIP

parent 463170df
......@@ -2,29 +2,29 @@
import 'nativescript-i18n';
import * as app from 'tns-core-modules/application';
import {Database, Manager, NSHTTPClient, BackgroundJobs} from './models';
import { Database, Manager, NSHTTPClient, BackgroundJobs } from './models';
import * as config from "./config/config";
import { AccountsController } from './controllers/accounts';
import * as Frames from 'ui/frame';
function myReloadPage() {
let frame:any = Frames.topmost();
if (frame && frame._currentEntry) {
if (frame.currentPage && frame.currentPage.modal) {
frame.currentPage.modal.closeModal();
}
let currentEntry = frame._currentEntry.entry;
let newEntry = {
animated: false,
clearHistory: true,
context: currentEntry.context,
bindingContext: currentEntry.bindingContext,
create: currentEntry.create,
moduleName: currentEntry.moduleName,
backstackVisible: currentEntry.backstackVisible
};
frame.navigate(newEntry);
let frame: any = Frames.topmost();
if (frame && frame._currentEntry) {
if (frame.currentPage && frame.currentPage.modal) {
frame.currentPage.modal.closeModal();
}
let currentEntry = frame._currentEntry.entry;
let newEntry = {
animated: false,
clearHistory: true,
context: currentEntry.context,
bindingContext: currentEntry.bindingContext,
create: currentEntry.create,
moduleName: currentEntry.moduleName,
backstackVisible: currentEntry.backstackVisible
};
frame.navigate(newEntry);
}
}
global.__onLiveSyncCore = myReloadPage;
......@@ -32,9 +32,10 @@ global.__onLiveSyncCore = myReloadPage;
Database.open('prod');
new BackgroundJobs().setupScheduleJob(Database.current())
new BackgroundJobs().setupScheduleJob(app, Database.current())
let manager = new Manager(config.MANAGER_URL, new NSHTTPClient())
app.start({ moduleName: 'views/accounts/list',
bindingContext: new AccountsController( manager ) })
app.start({
moduleName: 'views/accounts/list',
bindingContext: new AccountsController(manager)
})
......@@ -4,7 +4,7 @@ import * as utils from 'utils/utils'
import * as ApplicationSettings from "tns-core-modules/application-settings"
android.app.job.JobService.extend("fr.afi_sa.MyBibApp.RefreshAccountsService", {
onStartJob: function(params:android.app.job.JobParameters):boolean {
onStartJob: function(params: android.app.job.JobParameters): boolean {
if (!ApplicationSettings.getBoolean('background_refresh_on', false)) {
android.util.Log.d('MyBibApp', 'background refresh disabled')
this.jobFinished(params, true)
......@@ -16,25 +16,25 @@ android.app.job.JobService.extend("fr.afi_sa.MyBibApp.RefreshAccountsService", {
accounts.reduce(
(promise: Promise<any>, account: Account) => {
return promise.then( ()=> {
return promise.then(() => {
android.util.Log.d('MyBibApp', 'refresh account: ' + account.label)
return portal.refresh(account).catch( (error) => {
return portal.refresh(account).catch((error) => {
android.util.Log.e('MyBibApp', error.message)
})
})
},
Promise.resolve())
.then( () => {
.then(() => {
android.util.Log.d('MyBibApp', 'refresh accounts finished')
this.jobFinished(params, true);
})
return true
},
onStopJob: function(params:android.app.job.JobParameters):boolean {
onStopJob: function(params: android.app.job.JobParameters): boolean {
this.jobFinished(params, false)
return true;
}
......@@ -42,16 +42,16 @@ android.app.job.JobService.extend("fr.afi_sa.MyBibApp.RefreshAccountsService", {
export class BackgroundJobs {
public setupScheduleJob(database: Database) {
public setupScheduleJob(application: any, database: Database) {
let context = utils.ad.getApplicationContext()
let component = new android.content.ComponentName(context, fr.afi_sa.MyBibApp.RefreshAccountsService.class)
const builder = new (<any>android.app).job.JobInfo.Builder(1, component)
builder.setPeriodic(20 * 60 * 1000)
builder.setPersisted(true)
const jobScheduler = context.getSystemService((<any>android.content.Context).JOB_SCHEDULER_SERVICE)
console.log("Job Scheduled: " + jobScheduler.schedule(builder.build()))
return this
......
import { Database } from '../models/'
export declare class BackgroundJobs {
public setupScheduleJob(database: Database): this
public setupScheduleJob(application: any, database: Database): this
}
import { Database } from '../models/'
import { Database, Portal, Account } from '../models/'
var MBAAppDelegate = (UIResponder as any).extend(
{
_timer: null,
_bg_task: null,
runOnBackground: function() {
console.log('app on background')
let portal = new Portal()
let accounts = Database.current().findAll(Account)
accounts.reduce(
(promise: Promise<any>, account: Account) => {
return promise.then(() => {
return portal.refresh(account)
})
},
Promise.resolve())
.then(() => {
console.log('refresh finished')
})
},
applicationDidEnterBackground: function(application: UIApplication) {
console.log('app did enter background')
this._bg_task = application.beginBackgroundTaskWithNameExpirationHandler("RefreshAccountsTask", () => {
this.endBackgroundTask();
})
this._timer = NSTimer.scheduledTimerWithTimeIntervalTargetSelectorUserInfoRepeats(60,
this,
"runOnBackground",
null,
true);
},
endBackgroundTask: function() {
console.log('end of background task')
UIApplication.sharedApplication.endBackgroundTask(this._bg_task);
this._bg_task = UIBackgroundTaskInvalid;
}
},
{
name: 'MBAAppDelegate',
protocols: [UIApplicationDelegate]
}
);
MBAAppDelegate.ObjCProtocols = [UIApplicationDelegate];
MBAAppDelegate.ObjCExposedMethods = {
"runOnBackground": { returns: interop.types.void }
};
export class BackgroundJobs {
public setupScheduleJob(database: Database) {
public setupScheduleJob(application: any, database: Database) {
application.ios.delegate = MBAAppDelegate
}
}
    
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment