import 'package:flutter/material.dart'; import 'package:isar/isar.dart'; import 'package:stadtbote/content_views/utils/content_view_widget.dart'; import 'package:stadtbote/content_views/utils/header_info.dart'; import 'package:stadtbote/model/ranking.dart'; import 'package:stadtbote/popups/tasks_popups/zip_popup.dart'; import 'package:stadtbote/protocoll_messages/zip_message.dart'; import 'package:stadtbote/tools/constants.dart'; import 'package:stadtbote/tools/message_service.dart'; import 'package:stadtbote/tools/messages/ranking_updated.dart'; import 'package:stadtbote/tools/mqtt_core.dart'; import '../main.dart'; import '../tools/util.dart'; enum ModeEnum { set, request } class ApproachAreaPage extends ContentViewWidget { const ApproachAreaPage({super.key}); @override State createState() => _ApproachAreaPageState(); @override Future getHeaderInfo(BuildContext context) async { return HeaderInfo( titleWidget: const Text( 'Anfahrtsbereich', style: TextStyle( color: Colors.white, fontSize: 20.0, ), ), rightButtonEnabled: true, rightButtonVisible: true, rightButtonImage: Util.fetchFutureIcon(const Icon(Icons.help_outline, size: 20.0, color: Colors.white)), rightButtonOnPressed: () async { await Util.startBrowser('https://app.assecutor.de/help/sb/app/context/001/index.html'); }); } } class _ApproachAreaPageState extends State { late Ranking? _ranking; late bool _initCompleted = false; @override void initState() { super.initState(); Future.delayed(const Duration(seconds: 0), () async { _ranking = await MyApp.isarInstance.rankings.where().findFirst(); _initCompleted = true; setState(() {}); }); _initMessages(); } @override void deactivate() { super.deactivate(); MessageService().unsubscribe(Constants.messageTopicRankingUpdated, this); } @override Widget build(BuildContext context) { if (!_initCompleted) { return Util.getWaitWidget(context); } return Padding( padding: const EdgeInsets.all(8.0), child: InteractiveViewer( alignment: Alignment.topCenter, child: SingleChildScrollView( scrollDirection: Axis.vertical, child: SingleChildScrollView( scrollDirection: Axis.horizontal, child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: _fillColumns()), ), ), ), ); } List _fillColumns() { List columns = []; if (_ranking != null) { for (int i = 0; i < _ranking!.ranks.length; i++) { var rank = _ranking!.ranks[i]; List entries = []; entries.add( GestureDetector( onTap: ()=> { if (i == 0) { showZipPopup(context, 'ACHTUNG: Wenn Sie Ihre PLZ ändern, verlieren Sie Ihr Ranking!\n', 'Freimelden', (zip) => {_sendZip(rank, zip, ModeEnum.set)}), } else { showZipPopup(context, 'ACHTUNG: Wenn Sie Ihre PLZ ändern, verlieren Sie Ihr Ranking!\n', 'OK', (zip) => {_sendZip(rank, zip, ModeEnum.request)}), } }, child: Container( padding: const EdgeInsets.all(4), margin: const EdgeInsets.all(2), decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: i == 0 ? Constants.colorPrimary : Colors.white, border: Border.all( color: Colors.grey, width: 0.5, ), ), width: 85, height: 60, child: Column(children: [ Text( rank.zip, style: TextStyle(fontSize: 20, color: (i == 0 ? Colors.white : Colors.black)), ), Text( rank.area, style: TextStyle(fontSize: 13, color: (i == 0 ? Colors.white : Colors.black)), ) ] ) ), ), ); for (int j = 0; j < rank.vehicles.length; j++) { var vehicle = rank.vehicles[j]; var imageFilename = vehicle.self == true ? "assets/mein_${mapVehicleTypeToImageName(vehicle.type)}" : "assets/${mapVehicleTypeToImageName(vehicle.type)}"; entries.add(Container( padding: const EdgeInsets.all(4), margin: const EdgeInsets.all(2), decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: Colors.white, border: Border.all( color: Colors.grey, width: 0.5, ), ), width: 80, height: 60, child: Column( children: [ Image.asset(imageFilename, width: 30, height: 30), const SizedBox(width: 10), Text( mapVehicleTypeToText(vehicle.type), ), ], ), )); } columns.add(Align( alignment: Alignment.topLeft, child: Row( mainAxisAlignment: MainAxisAlignment.start, children: entries, ), )); } } return columns; } _sendZip(Rank rank, String zip, ModeEnum mode) { var zipMessage = ZipMessage(); if (mode == ModeEnum.set) { var position = MyApp.currentPosition; zipMessage.setPos = ZipSetPos(); zipMessage.setPos!.zip = zip; if (position != null) { zipMessage.setPos!.track = ZipTrack(); zipMessage.setPos!.track.time = DateTime.now().toIso8601String(); zipMessage.setPos!.track.pos = ZipTrackPos(); zipMessage.setPos!.track.pos!.lat = position.latitude; zipMessage.setPos!.track.pos!.long = position.longitude; zipMessage.setPos!.track.pos!.acc = position.accuracy.toInt(); } } else { zipMessage.reqZip = ZipReqZip(); zipMessage.reqZip!.id = rank.id; zipMessage.reqZip!.zip = zip; } MqttCore.instance.publishPayloadMessage("rank", zipMessage.toJson()); } _initMessages() { MessageService().subscribe(Constants.messageTopicRankingUpdated, this, (rankingUpdateMessage) async { _ranking = await MyApp.isarInstance.rankings.where().findFirst(); setState(() {}); }); } }