diff --git a/lib/pages/home_page.dart b/lib/pages/home_page.dart index fbfcec0..9ba5a59 100644 --- a/lib/pages/home_page.dart +++ b/lib/pages/home_page.dart @@ -14,6 +14,16 @@ class _HomePageState extends State { final agent = AgentProvider(); ShipProvider shipProvider = ShipProvider(); + Future>? shipsFuture; + + @override + void didChangeDependencies() { + super.didChangeDependencies(); + shipsFuture = listShips(); + } + + + Future> listShips() async { List ships = await shipProvider.listShips(); return ships; @@ -67,7 +77,7 @@ class _HomePageState extends State { Card( color: Colors.white, child: FutureBuilder>( - future: listShips(), + future: shipsFuture,//listShips(), builder: (BuildContext context, AsyncSnapshot> snapshot) { if (snapshot.hasData) { List ships = snapshot.data!; diff --git a/lib/pages/ship_page.dart b/lib/pages/ship_page.dart index 3c8efc4..718453c 100644 --- a/lib/pages/ship_page.dart +++ b/lib/pages/ship_page.dart @@ -15,6 +15,8 @@ class ShipPage extends StatefulWidget { class _ShipPageState extends State { ShipModel? ship; Timer? _timer; + Timer? _timermin; + bool isButtonEnabled = true; @override void initState() { @@ -30,7 +32,7 @@ class _ShipPageState extends State { } void _startTimer() { - _timer = Timer.periodic(Duration(seconds: 15), (_) { + _timer = Timer.periodic(Duration(seconds: 5), (_) { setState(() { _fetchShipData(); }); @@ -46,6 +48,7 @@ class _ShipPageState extends State { void _stopTimer() { _timer?.cancel(); + _timermin?.cancel(); } ElevatedButton buildActionButton(String status) { @@ -54,22 +57,19 @@ class _ShipPageState extends State { if (status == 'IN_ORBIT') { buttonText = 'Dock'; - buttonAction = () { - // Action 1 - print('Action 1'); + buttonAction = () async { + Nav newNav = await ShipProvider.dockShip(widget.ship.symbol!); + ship?.nav = newNav; }; - } else if (status == 'Status2') { - buttonText = 'Action 2'; - buttonAction = () { - // Action 2 - print('Action 2'); + } else if (status == 'DOCKED') { + buttonText = 'Orbit'; + buttonAction = () async{ + Nav newNav = await ShipProvider.orbitShip(widget.ship.symbol!); + ship?.nav = newNav; }; } else { - buttonText = 'Default Action'; - buttonAction = () { - // Default Action - print('Default Action'); - }; + buttonText = ''; + buttonAction = null; } return ElevatedButton( @@ -78,6 +78,24 @@ class _ShipPageState extends State { ); } + void _onButtonPressed() async { + int duration; + if (isButtonEnabled) { + setState(() { + isButtonEnabled = false; + }); + + duration = await ShipProvider.extract(widget.ship.symbol!); + final disabledDuration = Duration(seconds: duration); + + _timermin = Timer(disabledDuration, () { + setState(() { + isButtonEnabled = true; + }); + }); + } + } + @override Widget build(BuildContext context) { return Scaffold( @@ -171,11 +189,11 @@ class _ShipPageState extends State { fontWeight: FontWeight.bold, ), ), - ElevatedButton( - onPressed: () { - }, - child: Text('Markets'), - ), + //ElevatedButton( + // onPressed: () { + // }, + // child: Text('Markets'), + // ), ], ), SizedBox(height: 10), @@ -191,7 +209,14 @@ class _ShipPageState extends State { fontSize: 16, ), ), - Divider(), // Add a line separator between each item + + ElevatedButton( + onPressed: () async { + Cargo newCargo = await ShipProvider.sell(widget.ship.symbol!, item.name!, item.units!); + }, + child: Text('Sell'), + ), + Divider(), ], ); }).toList() ?? [], @@ -200,6 +225,27 @@ class _ShipPageState extends State { ), ), ), + Card( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Padding( + padding: const EdgeInsets.all(8.0), + child: Text( + 'Mining', + style: TextStyle( + fontWeight: FontWeight.bold, + fontSize: 18, + ), + ), + ), + ElevatedButton( + onPressed: isButtonEnabled ? _onButtonPressed : null, + child: Text('Start Mining'), + ), + ], + ), + ), ], ), ), diff --git a/lib/providers/ship_provider.dart b/lib/providers/ship_provider.dart index 1db04b0..e7ccb38 100644 --- a/lib/providers/ship_provider.dart +++ b/lib/providers/ship_provider.dart @@ -39,5 +39,75 @@ class ShipProvider { return ShipModel.fromJson(decodedData['data']); } + + static Future