-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PWA service and prompt component
- Loading branch information
1 parent
664f554
commit 3dab9f1
Showing
5 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<mat-toolbar *ngIf="data.mobileType === 'android'" color="primary" class="android-prompt"> | ||
<button mat-button (click)="installPwa()"> | ||
App installieren | ||
</button> | ||
<button mat-icon-button (click)="close()" class="css-util position top-right"> | ||
<mat-icon svgIcon="close" color="warn"></mat-icon> | ||
</button> | ||
</mat-toolbar> | ||
|
||
<div *ngIf="data.mobileType === 'ios'" class="ios-prompt"> | ||
<button mat-icon-button (click)="close()" class="css-util position top-right"> | ||
<mat-icon svgIcon="close" color="warn"></mat-icon> | ||
</button> | ||
<div class="css-util text-align center"> | ||
<p class="css-util m-32"> | ||
Um diese App auf deinem Gerät zu installieren, tippe auf das "Teilen"-Menü und dann auf "Zum | ||
Home-Bildschirm". | ||
</p> | ||
<mat-icon svgIcon="ios_share"></mat-icon> | ||
<mat-icon svgIcon="navigate_next" color="accent"></mat-icon> | ||
<mat-icon svgIcon="add_box"></mat-icon> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Component, Inject } from '@angular/core'; | ||
import { MAT_BOTTOM_SHEET_DATA, MatBottomSheetModule, MatBottomSheetRef } from '@angular/material/bottom-sheet'; | ||
import { MatButtonModule } from '@angular/material/button'; | ||
import { IconsClass } from '../icons.class'; | ||
import { NgIf } from '@angular/common'; | ||
import { MatToolbarModule } from '@angular/material/toolbar'; | ||
import { MatIconModule } from '@angular/material/icon'; | ||
|
||
@Component({ | ||
selector: 'app-prompt', | ||
standalone: true, | ||
imports: [MatToolbarModule, MatButtonModule, MatBottomSheetModule, MatIconModule, NgIf], | ||
templateUrl: './prompt.component.html', | ||
providers: [IconsClass], | ||
}) | ||
export class PromptComponent { | ||
|
||
constructor( | ||
@Inject(MAT_BOTTOM_SHEET_DATA) public data: { mobileType: 'ios' | 'android', promptEvent?: any }, | ||
private bottomSheetRef: MatBottomSheetRef<PromptComponent> | ||
) {} | ||
|
||
public installPwa(): void { | ||
this.data.promptEvent.prompt(); | ||
this.close(); | ||
} | ||
|
||
public close() { | ||
this.bottomSheetRef.dismiss(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Platform } from '@angular/cdk/platform'; | ||
import { MatBottomSheet } from '@angular/material/bottom-sheet'; | ||
import { take, timer } from 'rxjs'; | ||
import { PromptComponent } from './prompt/prompt.component'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class PwaService { | ||
private promptEvent: any; | ||
|
||
constructor( | ||
private bottomSheet: MatBottomSheet, | ||
private platform: Platform | ||
) { } | ||
|
||
public initPwaPrompt() { | ||
if (this.platform.ANDROID) { | ||
window.addEventListener('beforeinstallprompt', (event: any) => { | ||
event.preventDefault(); | ||
this.promptEvent = event; | ||
this.openPromptComponent('android'); | ||
}); | ||
} | ||
if (this.platform.IOS) { | ||
const isInStandaloneMode = ('standalone' in window.navigator) && (window.navigator['standalone']); | ||
if (!isInStandaloneMode) { | ||
this.openPromptComponent('ios'); | ||
} | ||
} | ||
} | ||
|
||
private openPromptComponent(mobileType: 'ios' | 'android') { | ||
timer(3000) | ||
.pipe(take(1)) | ||
.subscribe(() => this.bottomSheet.open(PromptComponent, { data: { mobileType, promptEvent: this.promptEvent } })); | ||
} | ||
} |