Skip to content

Commit

Permalink
add a way to test windows notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Serghei Paduret committed Jul 28, 2021
1 parent 72c2333 commit 2f1b27d
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/app/components/options/options.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ <h3>Notifications</h3>
</div>
<div class="options-item">
<span>Browser</span>
<input type="checkbox" [(ngModel)]="settings.notifications.browser">
<input type="checkbox" (click)="onEnableBrowserNotifications($event)" [(ngModel)]="enableBrowserNotifications">
<i>send push notifications</i>
<span *ngIf="enableBrowserNotifications" class="test-browser-notification"
title="test browser notification" (click)="onBrowserNotificationTest()">
test
<i class="icon icon-bubble"></i></span>
</div>
<div class="options-item">
<span>Slack</span>
Expand Down
6 changes: 6 additions & 0 deletions src/app/components/options/options.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@
padding: 10px 0;
border-top: 1px dashed $border-color;
}

.test-browser-notification {
margin-left: 10px;
text-decoration: underline;
cursor: pointer;
}
35 changes: 35 additions & 0 deletions src/app/components/options/options.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {ChangeDetectorRef, Component, OnInit} from '@angular/core';
import {SlackClient} from '../../services/slackClient';
import {BackgroundService} from '../../services/background.service';
import {DisposableComponent} from '../../../core/disposable-component';
import {NotificationService} from '../../services/notification.service';

class StatusMessage {
message!: string;
Expand All @@ -24,6 +25,7 @@ export class OptionsComponent extends DisposableComponent implements OnInit {
settings: ExtensionSettings;
statusMessage?: StatusMessage;

enableBrowserNotifications: boolean;
enableSlackNotifications: boolean;
slackSettings: SlackSettings;
bitbucketSettings: BitbucketSettings;
Expand All @@ -34,11 +36,13 @@ export class OptionsComponent extends DisposableComponent implements OnInit {
private settingsService: DataService,
private bitbucketService: BitbucketService,
private backgroundService: BackgroundService,
private notificationService: NotificationService,
private cd: ChangeDetectorRef) {
super();

this.settings = settingsService.getExtensionSettings();

this.enableBrowserNotifications = !!this.settings.notifications.browser;
this.enableSlackNotifications = !!this.settings.notifications.slack;
this.slackSettings = this.settings.notifications.slack || new SlackSettings();
this.bitbucketSettings = this.settings.bitbucket || new BitbucketSettings();
Expand Down Expand Up @@ -139,5 +143,36 @@ export class OptionsComponent extends DisposableComponent implements OnInit {

chrome.permissions.request({origins: origins}, (d) => callback(d));
}

onEnableBrowserNotifications(event: MouseEvent) {
if (!this.enableBrowserNotifications) {
this.notificationService
.requestPermission()
.subscribe(permission => {
console.debug('permission:', permission);
if (permission === 'granted') {
this.settings.notifications.browser = true;
} else {
this.enableBrowserNotifications = false;
this.settings.notifications.browser = false;
}
});
} else {
this.settings.notifications.browser = false;
}
}

onBrowserNotificationTest() {
this.notificationService
.requestPermission()
.subscribe(permission => {
if (permission === 'granted') {
new Notification('test', {
icon: 'icon64.png',
body: 'hello'
});
}
});
}
}

41 changes: 37 additions & 4 deletions src/app/models/models.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
import {BitbucketCommentAction, PullRequestActivityAction, PullRequestRole, PullRequestState, PullRequestStatus} from './enums';
import {
BitbucketCommentAction,
PullRequestActivityAction,
PullRequestRole,
PullRequestState,
PullRequestStatus
} from './enums';

export class ExtensionSettings {
bitbucket?: BitbucketSettings = new BitbucketSettings(undefined);
bitbucket?: BitbucketSettings = new BitbucketSettings();
refreshIntervalInMinutes: number = 1;
notifications: { browser: boolean, slack?: SlackSettings } = {browser: true};
notifications: NotificationSettings = new NotificationSettings();

constructor(data?: ExtensionSettings) {
if (data) {
Object.assign(this, data, {
bitbucket: new BitbucketSettings(data.bitbucket)
bitbucket: new BitbucketSettings(data.bitbucket),
notifications: new NotificationSettings(data.notifications)
});
}
}
}

export class NotificationSettings {
browser = true;
slack?: SlackSettings;
events: NotificationEvents = new NotificationEvents();

constructor(data?: NotificationSettings) {
if (data) {
Object.assign(this, data, {
events: new NotificationEvents(data.events)
});
}
}
}

export class NotificationEvents {
pullRequestCreated = true;
pullRequestStateChanged = true;
commentAdded = true;

constructor(data?: NotificationEvents) {
if (data) {
Object.assign(this, data);
}
}
}

export class SlackSettings {
token!: string;
memberId!: string;
Expand Down
10 changes: 5 additions & 5 deletions src/app/services/notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Injectable} from '@angular/core';
import {DataService} from './data.service';
import {SlackClient, SlackMessageOptions} from './slackClient';
import {catchError} from 'rxjs/operators';
import {of, throwError} from 'rxjs';
import {from, Observable, of, throwError} from 'rxjs';
import {NotificationOptions, PullRequestIssue} from '../models/models';
import {BitbucketService} from './bitbucket.service';
import {PullRequestActivityAction} from '../models/enums';
Expand All @@ -14,12 +14,12 @@ export class NotificationService {
constructor(private dataService: DataService, private bitbucketService: BitbucketService) {
}

private requestPermission(): Promise<NotificationPermission> {
public requestPermission(): Observable<NotificationPermission> {
if (Notification.permission !== 'denied') {
return Notification.requestPermission();
return from(Notification.requestPermission());
}

return new Promise<NotificationPermission>(() => Notification.permission);
return of(Notification.permission);
}

sendNotification(options: NotificationOptions) {
Expand All @@ -34,7 +34,7 @@ export class NotificationService {

if (extensionSettings.notifications.browser) {
this.requestPermission()
.then(permission => {
.subscribe(permission => {
if (permission === 'granted') {
let body = GetWindowsNotificationBody(options.action);

Expand Down

0 comments on commit 2f1b27d

Please sign in to comment.