76 lines
2.1 KiB
PHP
76 lines
2.1 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/../vendor/autoload.php';
|
|
|
|
use PHPNative\Framework\Application;
|
|
use PHPNative\Ui\Widget\Button;
|
|
use PHPNative\Ui\Widget\Container;
|
|
use PHPNative\Ui\Widget\Label;
|
|
|
|
// Create application
|
|
$app = new Application('Async Button Example', 800, 600);
|
|
|
|
// Create UI components
|
|
$container = new Container('flex flex-col p-4 gap-4');
|
|
|
|
// Status label to show loading state
|
|
$statusLabel = new Label(
|
|
text: 'Klicken Sie den Button, um Daten zu laden...',
|
|
style: 'text-base text-gray-700'
|
|
);
|
|
|
|
// Result label to show fetched data
|
|
$resultLabel = new Label(
|
|
text: '',
|
|
style: 'text-sm text-blue-600'
|
|
);
|
|
|
|
// Button with async onClick handler
|
|
$button = new Button(
|
|
text: 'Daten aus dem Web laden',
|
|
style: 'bg-blue-500 hover:bg-blue-700 text-white p-4 rounded-lg'
|
|
);
|
|
|
|
// Set async click handler
|
|
$button->setOnClickAsync(
|
|
// Task that runs in background thread
|
|
onClickAsync: function() {
|
|
// Simulate web request (or use real HTTP client)
|
|
sleep(2); // Simulates network delay
|
|
|
|
// In production, you would use something like:
|
|
// $response = file_get_contents('https://api.example.com/data');
|
|
// return json_decode($response, true);
|
|
|
|
return [
|
|
'status' => 'success',
|
|
'data' => 'Daten erfolgreich geladen!',
|
|
'timestamp' => date('H:i:s')
|
|
];
|
|
},
|
|
|
|
// Callback when task completes successfully
|
|
onComplete: function($result) use ($statusLabel, $resultLabel) {
|
|
$statusLabel->setText('✓ Laden abgeschlossen!');
|
|
$resultLabel->setText(
|
|
'Status: ' . $result['status'] . "\n" .
|
|
'Daten: ' . $result['data'] . "\n" .
|
|
'Zeit: ' . $result['timestamp']
|
|
);
|
|
},
|
|
|
|
// Callback when task fails
|
|
onError: function($error) use ($statusLabel, $resultLabel) {
|
|
$statusLabel->setText('✗ Fehler beim Laden!');
|
|
$resultLabel->setText('Error: ' . $error->getMessage());
|
|
}
|
|
);
|
|
|
|
// Add components to container
|
|
$container->addComponent($statusLabel);
|
|
$container->addComponent($button);
|
|
$container->addComponent($resultLabel);
|
|
|
|
// Set root component and run
|
|
$app->setRoot($container)->run();
|