Skip to content
This repository has been archived by the owner on Jan 1, 2023. It is now read-only.

Commit

Permalink
Add updater
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamazaki93 committed Oct 20, 2019
1 parent 6387e00 commit 8aacc7b
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
<div class="min-w-0 flex-shrink w-full">
<h1 class="text-xl">Servers</h1>
</div>
<div class="cursor-pointer hover:text-primary p-1" (click)="adding = true;" title="Add new server">
<div class="cursor-pointer hover:text-primary p-1 mr-2" (click)="adding = true;" title="Add new server">
<h1 class="text-2xl flex align-center"><i class="ion-md-add flex"></i></h1>
</div>
<div class="cursor-pointer hover:text-primary p-1" (click)="openSettings()" title="Settings">
<h1 class="text-2xl flex align-center"><i class="ion-md-settings flex"></i></h1>
</div>
</div>
<div class="flex-shrink-0 p-3 raised-2" *ngIf="adding">
<app-websocket-server-config (Closing)="cancelAdd()"></app-websocket-server-config>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { trigger, transition, animate, keyframes, style, query } from '@angular/animations';
import { ServersService } from '../servers.service';
import { SubscriptionComponent } from 'src/app/helpers/subscription-component';
import { ElectronService } from 'src/app/messaging/electron.service';
import { AddOrUpdateConfigCommand } from '../../../../../servers/messages';
import * as uuid from 'uuid';
import { IServerConfig } from '../../../../../servers/i-server-config';
import { WebSocketServerConfigMemento } from '../../../../../websocket-servers/i-websocket-server-config';
import { WorkspaceService } from 'src/app/workspace/workspace.service';
import { SettingsPageComponent } from 'src/app/settings/settings-page/settings-page.component';

@Component({
selector: 'app-server-list',
Expand All @@ -31,7 +30,8 @@ export class ServerListComponent extends SubscriptionComponent implements OnInit

constructor(
private servers: ServersService,
private electron: ElectronService
private electron: ElectronService,
private workspace: WorkspaceService
) {
super();
this.recordSubscription(servers.Servers.subscribe(s => {
Expand All @@ -46,4 +46,8 @@ export class ServerListComponent extends SubscriptionComponent implements OnInit
private cancelAdd() {
this.adding = false;
}
private openSettings() {
let t = this.workspace.OpenNewTab(SettingsPageComponent);
this.workspace.SetTabTitle(t.id, 'Settings');
}
}
2 changes: 2 additions & 0 deletions app/frontend/src/app/servers/servers.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { FormsModule } from '@angular/forms';
import { WorkspaceModule } from '../workspace/workspace.module';
import { ControlsModule } from '../controls/controls.module';
import { WebsocketServersModule } from '../websocket-servers/websocket-servers.module';
import { SettingsModule } from '../settings/settings.module';

@NgModule({
declarations: [
Expand All @@ -23,6 +24,7 @@ import { WebsocketServersModule } from '../websocket-servers/websocket-servers.m
FormsModule,
ControlsModule,
WorkspaceModule,
SettingsModule,
WebsocketServersModule
],
providers: [
Expand Down
2 changes: 2 additions & 0 deletions app/frontend/src/app/servers/servers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export class ServersService {
if (opened) {
let cfg = this.servers.value.find(_ => _.ID === opened.configID);
this.currentServerType.next(cfg.GetMemento().TypeID);
} else {
this.currentServerType.next('');
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="text-white flex flex-col">
<div class="p-2">
<h3 class="text-xl">Updates</h3>
</div>
<div class="p-2 flex flex-col">
<h3 class="text-xl">About</h3>
<p>Version 0.1.0</p>
<p>License MIT</p>
</div>
</div>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-settings-page',
templateUrl: './settings-page.component.html',
styleUrls: ['./settings-page.component.scss']
})
export class SettingsPageComponent implements OnInit {

constructor() { }

ngOnInit() {
}

}
17 changes: 17 additions & 0 deletions app/frontend/src/app/settings/settings.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SettingsPageComponent } from './settings-page/settings-page.component';
import { FormsModule } from '@angular/forms';
import { WorkspaceModule } from '../workspace/workspace.module';

@NgModule({
declarations: [SettingsPageComponent],
exports: [SettingsPageComponent],
entryComponents: [SettingsPageComponent],
imports: [
CommonModule,
FormsModule,
WorkspaceModule
]
})
export class SettingsModule { }
10 changes: 10 additions & 0 deletions app/frontend/src/app/updater/updater.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

@NgModule({
declarations: [],
imports: [
CommonModule
]
})
export class UpdaterModule { }
66 changes: 66 additions & 0 deletions app/frontend/src/app/updater/updater.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Injectable, EventEmitter } from '@angular/core';
import { Observable, BehaviorSubject } from 'rxjs';
import { CalledOnce } from '../helpers/called-once.decorator';
import { ElectronService } from '../messaging/electron.service';
import { UpdaterEvent, UpdaterCommand } from '../../../../updater/messages';

@Injectable()
export class UpdaterService {

updateVersion: Observable<string> = new Observable<string>();
updaterStatus: Observable<UpdaterStatus> = new Observable<UpdaterStatus>();
updateDownloadProgress: EventEmitter<number> = new EventEmitter<number>();

private statusSubject: BehaviorSubject<UpdaterStatus> = new BehaviorSubject<UpdaterStatus>(UpdaterStatus.NoUpdateAvailable);
private versionSubject: BehaviorSubject<string> = new BehaviorSubject<string>('');
constructor(
private electron: ElectronService,
) {
this.updaterStatus = this.statusSubject.asObservable();
this.updateVersion = this.versionSubject.asObservable();
}
@CalledOnce
init() {
this.electron.Receive('UpdaterEvent', (evt: UpdaterEvent) => {
if (evt.Event === 'update-available' &&
(this.statusSubject.getValue() === UpdaterStatus.NoUpdateAvailable || this.statusSubject.getValue() === UpdaterStatus.CheckingUpdate)) {
this.versionSubject.next(evt.Arg.version);
this.statusSubject.next(UpdaterStatus.UpdateAvailable);
} else if (evt.Event === 'update-not-available') {
this.statusSubject.next(UpdaterStatus.NoUpdateAvailable);
} else if (evt.Event === 'downloading-update') {
if (this.statusSubject.getValue() !== UpdaterStatus.DownloadingUpdate) {
this.statusSubject.next(UpdaterStatus.DownloadingUpdate);
}
this.updateDownloadProgress.emit(Math.floor(evt.Arg.percentage));
} else if (evt.Event === 'download-complete') {
this.statusSubject.next(UpdaterStatus.InstallingUpdate);
let that = this;
setTimeout(() => {
that.electron.Send(new UpdaterCommand('commence-install-update'));
}, 7 * 1000);
} else if (evt.Event === 'checking') {
if (evt.Arg.inProgress) {
this.statusSubject.next(UpdaterStatus.CheckingUpdate);
}
}
});
}
checkUpdate() {
this.electron.Send(new UpdaterCommand('check-for-update'));
}
installUpdate() {
this.statusSubject.next(UpdaterStatus.DownloadingUpdate);
this.electron.Send(new UpdaterCommand('commence-download'));
}

}


export enum UpdaterStatus {
NoUpdateAvailable,
CheckingUpdate,
UpdateAvailable,
DownloadingUpdate,
InstallingUpdate,
}
6 changes: 6 additions & 0 deletions app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { RequestRepository } from "./requests/request-repository";
import { RequestRepositoryMessagingAdapter } from "./requests/request-repository-messaging-adapter";
import { IMessenger } from "./messaging/i-messenger";
import { LoadRequestsCommand } from "./requests/messages";
import { UpdaterService } from "./updater/updater";
let homeDir = os.homedir();
let appDir = homeDir + "/ReqUI/";
let mainWindow: BrowserWindow;
Expand Down Expand Up @@ -140,6 +141,7 @@ function initializeComponents() {
initializePersistence();
initializeServers(messenger, logger, persistence);
initializeRequests(messenger, logger, persistence);
initializeUpdater(messenger);
}

function initializeMessaging(log: ILogger) {
Expand Down Expand Up @@ -178,3 +180,7 @@ function registerApplicationMessages() {
messenger.Send(new LoadRequestsCommand());
});
}

function initializeUpdater(message: IMessenger) {
let updater = new UpdaterService(message);
}
21 changes: 21 additions & 0 deletions app/updater/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IMessage } from "../messaging/i-message";

// tslint:disable:max-classes-per-file

export class UpdaterCommand implements IMessage {
public ID = 'UpdaterCommand' ;
public Command: string;
public constructor(command: string) {
this.Command = command;
}
}

export class UpdaterEvent implements IMessage {
public ID = 'UpdaterEvent' ;
public Event: string;
public Arg: any;
public constructor(evt: string, arg?: any) {
this.Event = evt;
this.Arg = arg;
}
}
61 changes: 61 additions & 0 deletions app/updater/updater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { BrowserWindow, app } from "electron";
import { autoUpdater } from "electron-updater";
import * as electronLog from "electron-log";
import { IMessenger } from "../messaging/i-messenger";
import { UpdaterCommand, UpdaterEvent } from "./messages";

autoUpdater.autoDownload = false;
autoUpdater.logger = electronLog;

// beta channel for testing
autoUpdater.channel = 'beta';

export class UpdaterService {
private messenger: IMessenger;

constructor(messenger: IMessenger) {
this.messenger = messenger;
this.messenger.Receive('UpdaterCommand', (arg: UpdaterCommand) => {
if (arg.Command === 'commence-install-update') {
setImmediate(() => {
app.removeAllListeners("window-all-closed");
let browserWindows = BrowserWindow.getAllWindows();
browserWindows.forEach((browserWindow) => {
browserWindow.close();
});
autoUpdater.quitAndInstall();
});
} else if (arg.Command === 'commence-download') {
autoUpdater.downloadUpdate();
} else if (arg.Command === 'check-for-update') {
this.checkUpdate();
}
});
autoUpdater.on('update-available', (info) => {
this.messenger.Send(new UpdaterEvent('checking', false));
this.messenger.Send(new UpdaterEvent('update-available', { version: info.version }));
});
autoUpdater.on('update-not-available', () => {
this.messenger.Send(new UpdaterEvent('checking', false));
this.messenger.Send(new UpdaterEvent('update-not-available'));
});
autoUpdater.on('download-progress', (progress) => {
this.messenger.Send(new UpdaterEvent('downloading-update', { percentage: progress.percent }));
});
autoUpdater.on('update-downloaded', () => {
this.messenger.Send(new UpdaterEvent('download-complete'));
});
autoUpdater.on('checking-for-update', () => {
this.messenger.Send(new UpdaterEvent('checking', true));
});
setInterval(() => {
this.checkUpdate();
}, 15 * 60 * 1000);
setTimeout(() => {
this.checkUpdate();
}, 10 * 1000);
}
public checkUpdate() {
autoUpdater.checkForUpdates();
}
}

0 comments on commit 8aacc7b

Please sign in to comment.