import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class LiveActivityScreen extends StatefulWidget {
  @override
  _LiveActivityScreenState createState() => _LiveActivityScreenState();
}

class _LiveActivityScreenState extends State<LiveActivityScreen> {
  static const platform = MethodChannel('live_activity_channel');

  Future<void> _startLiveActivity() async {
    try {
      await platform.invokeMethod('startLiveActivity');
    } on PlatformException catch (e) {
      print("Failed to start Live Activity: '${e.message}'.");
    }
  }

  Future<void> _updateLiveActivity(double progress) async {
    try {
      await platform.invokeMethod('updateLiveActivity', {"progress": progress});
    } on PlatformException catch (e) {
      print("Failed to update Live Activity: '${e.message}'.");
    }
  }

  Future<void> _endLiveActivity() async {
    try {
      await platform.invokeMethod('endLiveActivity');
    } on PlatformException catch (e) {
      print("Failed to end Live Activity: '${e.message}'.");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Live Activity Test")),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ElevatedButton(
            onPressed: _startLiveActivity,
            child: Text("Start Live Activity"),
          ),
          ElevatedButton(
            onPressed: () => _updateLiveActivity(0.5),
            child: Text("Update Progress to 50%"),
          ),
          ElevatedButton(
            onPressed: _endLiveActivity,
            child: Text("End Live Activity"),
          ),
        ],
      ),
    );
  }
}
