Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ionic resume pause event prevent from fire on file browse only fire at press the home button

I am developing a chat app inside I am using on pause and on resume event.

document.addEventListener('pause',onpause, false);

document.addEventListener('resume' , onresume,false);

This event works perfectly when I open the app and press home button to an Android phone these events are perfect. But my problem is that in chat app I am sending file attachments from the gallery when I select the browse button the phone image gallery, and same time pause event is fire. while I am on image gallery at a not selecting any image same time when I click on the home button the same event is not firing. So how I can prevent the pause event while selecting the file from the gallery.

Is there any other way to do this in ionic v1? Or how I can fire on pause and on resume event for the same.

like image 467
Pritish Avatar asked Feb 16 '17 19:02

Pritish


2 Answers

I wrote a small Service to solve that problem:

import {Injectable} from '@angular/core';
import {Subject} from "rxjs/Subject";

@Injectable()
export class EventService {
    protected resumeHalted = false;
    protected resumeSubject = new Subject<any>();

    protected resumeListener = () => {
        if (this.resumeHalted) {
            return;
        }
        this.resumeSubject.next();
    };

    constructor() {
        document.addEventListener('resume', this.resumeListener);
    }

    haltResume() {
        this.resumeHalted = true;
    }

    continueResume() {
        this.resumeHalted = false;
    }

    resume() {
        return this.resumeSubject;
    }
}

The gallery call is also wrapped in a service. Every time I call it, I "halt" the event and "continue" it after the user interaction finishes:

getPicture(options: CameraOptions) {
    let subject = new Subject<any>();
    
    this.eventService.haltResume();
    this.camera.getPicture(options).then((path) => {
        // ...
        subject.next();
        subject.complete();
        this.eventService.continueResume();
    }, () => {
        this.eventService.continueResume();
    });

    return subject.asObservable();
}

The last step: Instead of listening for the resume event, I subscribe to the resume Oberservable:

        this.eventService.resume().subscribe(() => {
            this.statusBar.hide();
        });
like image 160
Volker Andres Avatar answered Sep 23 '22 13:09

Volker Andres


You can remove the pause event before you open the gallery and reattach the event on the gallery's callback.

document.removeEventListener("pause", myFunction);
like image 41
Biswas Khayargoli Avatar answered Sep 26 '22 13:09

Biswas Khayargoli