Skip to content

Commit

Permalink
cycle camera logic and torch
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao Leonardo Pereira committed Jul 21, 2024
1 parent 3e19e9c commit 189c524
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
19 changes: 18 additions & 1 deletion src/app/home/home.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,24 @@
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<ngx-scanner-qrcode *ngIf="isScanActive;else nonScan" #qrscanner="scanner" (event)="onQRCodeScanned($event, qrscanner)" [config]="qrScannerOpts"></ngx-scanner-qrcode>
<ng-container *ngIf="isScanActive;else nonScan">
<ngx-scanner-qrcode #qrscanner="scanner" (event)="onQRCodeScanned($event, qrscanner)" [config]="qrScannerOpts"></ngx-scanner-qrcode>
<ion-grid>
<ion-row>
<ion-col size="auto">
<ion-button size="large" (click)="toggleTorch()">
<ion-icon slot="icon-only" name="flashlight-outline"></ion-icon>
</ion-button>
</ion-col>
<ion-col></ion-col>
<ion-col size="auto">
<ion-button size="large" (click)="cycleCamera()">
<ion-icon slot="icon-only" name="sync-outline"></ion-icon>
</ion-button>
</ion-col>
</ion-row>
</ion-grid>
</ng-container>
<ng-template #nonScan>
<ng-container *ngIf="!manualInput;else manualForm">
<ion-button expand="block" (click)="scanCode()">
Expand Down
19 changes: 17 additions & 2 deletions src/app/home/home.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class HomePage implements OnInit {
isAddAccountModalOpen: boolean = false
isScanActive: boolean = false
validations_form: FormGroup;

private currentCameraIndex = 0
constructor(
private authService: AuthenticationService,
private accountsService: Account2faService,
Expand Down Expand Up @@ -194,7 +194,6 @@ export class HomePage implements OnInit {
}

async createAccount(formValues: any) {

console.log({formValues})
const logo = this.draftLogoURL
await this.closeAddAccountModal()
Expand Down Expand Up @@ -247,6 +246,22 @@ export class HomePage implements OnInit {
this.processQRCode(evt && evt[0]?.value || '')
}

async cycleCamera() {
console.log("cycle camera")
const current = this.qrscanner.deviceIndexActive
console.log({current})
const devices = await firstValueFrom(this.qrscanner.devices)
console.log({devices})
const next = (current + 1) % devices.length
const nextDevice = devices[next]
this.qrscanner.playDevice(nextDevice.deviceId)
}

async toggleTorch() {
const currentState = await firstValueFrom(this.qrscanner.torcher())
this.qrscanner.isTorch = !currentState
}

private processQRCode(evt: string) {
// otpauth://totp/Google:My%20Account?secret=JBSWY3D&issuer=Google&algorithm=SHA1&digits=6&period=30
const account = Account2FA.fromOTPAuthURL(evt)
Expand Down
8 changes: 3 additions & 5 deletions src/app/services/account2fa.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ export class Account2faService {
const accountCollection = collection(this.firestore, `accounts2fa/${userId}/accounts`)

// by default, order by added date
const q = query(accountCollection, orderBy('added', 'desc'))
this.accounts$ = collectionData(q)
.pipe(map(accounts => {
console.log({accounts})
const q = query(accountCollection, orderBy('added', 'asc'))
this.accounts$ = collectionData(q).pipe(map(accounts => {
return accounts.map(account => Account2FA.fromDictionary(account as IAccount2FA))
}))
}))
}

public getAccounts(): Observable<Account2FA[]> {
Expand Down
4 changes: 3 additions & 1 deletion src/app/services/authentication.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { inject, Injectable } from '@angular/core';
import { Auth, signInWithEmailAndPassword, signOut } from '@angular/fire/auth';
import { NavController } from '@ionic/angular';
import { LocalStorageService } from './local-storage.service';

@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
private afAuth: Auth = inject(Auth)
constructor(private navCtrl: NavController) { }
constructor(private navCtrl: NavController, private localStorage: LocalStorageService) { }

async canActivate(): Promise<boolean> {
await this.afAuth.authStateReady()
Expand All @@ -26,6 +27,7 @@ export class AuthenticationService {

public async logout() {
await signOut(this.afAuth)
await this.localStorage.clearStorage()
console.log("logged out")
}

Expand Down
4 changes: 4 additions & 0 deletions src/app/services/local-storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ export class LocalStorageService {
currentArray.unshift(value)
return this.set(key, currentArray)
}

async clearStorage(): Promise<void> {
return this.storage.clear()
}
}

0 comments on commit 189c524

Please sign in to comment.