PHP code example of djurovicigoor / app-lifecycle

1. Go to this page and download the library: Download djurovicigoor/app-lifecycle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

djurovicigoor / app-lifecycle example snippets


// app/Providers/NativeServiceProvider.php

public function plugins(): array
{
    return [
        // ...other plugins
        \Djurovicigoor\AppLifecycle\AppLifecycleServiceProvider::class,
    ];
}

// app/Providers/AppServiceProvider.php

use Djurovicigoor\AppLifecycle\Events\AppForegrounded;
use Djurovicigoor\AppLifecycle\Events\AppBackgrounded;
use Illuminate\Support\Facades\Event;

public function boot(): void
{
    // Fires every time the user returns to the app
    Event::listen(AppForegrounded::class, function (AppForegrounded $event) {
        // make an API call, refresh data, etc.
    });

    // Fires every time the user leaves the app
    Event::listen(AppBackgrounded::class, function (AppBackgrounded $event) {
        // flush pending writes, pause timers, etc.
    });
}

// app/Listeners/SyncOnForeground.php

namespace App\Listeners;

use Djurovicigoor\AppLifecycle\Events\AppForegrounded;
use Illuminate\Support\Facades\Http;

class SyncOnForeground
{
    public function handle(AppForegrounded $event): void
    {

    }
}

// app/Providers/AppServiceProvider.php

Event::listen(AppForegrounded::class, SyncOnForeground::class);

use Livewire\Attributes\On;
use Livewire\Component;

class Dashboard extends Component
{
    #[On('native:Djurovicigoor\AppLifecycle\Events\AppForegrounded')]
    public function handleForegrounded(int $timestamp): void
    {
        // Refresh data when app returns to foreground
        $this->loadLatestData();
    }

    #[On('native:Djurovicigoor\AppLifecycle\Events\AppBackgrounded')]
    public function handleBackgrounded(int $timestamp): void
    {
        // Save state when app goes to background
        $this->saveCurrentState();
    }
}