217 lines
8.0 KiB
Dart
217 lines
8.0 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:insparkspokalen_ui/models/postModel.dart';
|
|
import 'package:insparkspokalen_ui/models/teamModel.dart';
|
|
import 'package:insparkspokalen_ui/models/missionModel.dart';
|
|
import 'package:insparkspokalen_ui/services/backend/feedService.dart';
|
|
import 'package:insparkspokalen_ui/services/image_service.dart';
|
|
import 'package:insparkspokalen_ui/services/googleAuthService.dart';
|
|
import 'package:insparkspokalen_ui/services/locationService.dart';
|
|
|
|
void showAddPostDialog(
|
|
BuildContext context, {
|
|
required FeedService feedService,
|
|
required List<TeamModel> teamItems,
|
|
required ImageService imageService,
|
|
required List<MissionModel> missions,
|
|
required Future<void> Function() onPostAdded,
|
|
}) {
|
|
final TextEditingController textController = TextEditingController();
|
|
final locationService = LocationService(
|
|
'AIzaSyDoaswxc9QOjMX_xY8jF4P0zAENjLlXVl4',
|
|
);
|
|
|
|
File? selectedImage;
|
|
MissionModel? selectedMission;
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) {
|
|
bool isPosting = false; // Flagga som kontrollerar om inläggen är pågående för att stoppa button spam crashes
|
|
return StatefulBuilder(
|
|
builder: (context, setState) {
|
|
return AlertDialog(
|
|
backgroundColor: Colors.white,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
title: const Text("Nytt inlägg"),
|
|
content: SingleChildScrollView(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
TextField(
|
|
controller: textController,
|
|
style: const TextStyle(color: Colors.black),
|
|
decoration: const InputDecoration(
|
|
hintText: "Skriv din information här",
|
|
hintStyle: TextStyle(color: Colors.grey),
|
|
),
|
|
maxLines: 3,
|
|
),
|
|
const SizedBox(height: 10),
|
|
missions.isNotEmpty
|
|
? DropdownButton<MissionModel>(
|
|
value: selectedMission,
|
|
hint: const Text("Välj uppdrag"),
|
|
isExpanded: true,
|
|
items:
|
|
missions.map((mission) {
|
|
return DropdownMenuItem(
|
|
value: mission,
|
|
child: Text(mission.name),
|
|
);
|
|
}).toList(),
|
|
onChanged: (value) {
|
|
setState(() {
|
|
selectedMission = value;
|
|
});
|
|
},
|
|
)
|
|
: const CircularProgressIndicator(),
|
|
const SizedBox(height: 10),
|
|
selectedImage != null
|
|
? Image.file(selectedImage!, height: 150)
|
|
: const Text("Ingen bild vald"),
|
|
TextButton.icon(
|
|
onPressed: () async {
|
|
final pickedFile = await ImagePicker().pickImage(
|
|
source: ImageSource.gallery,
|
|
);
|
|
if (pickedFile != null) {
|
|
setState(() {
|
|
selectedImage = File(pickedFile.path);
|
|
});
|
|
}
|
|
},
|
|
icon: const Icon(Icons.image, color: Color(0xFF111111)),
|
|
label: const Text(
|
|
"Välj bild",
|
|
style: TextStyle(color: Color(0xFF111111)),
|
|
),
|
|
),
|
|
TextButton.icon(
|
|
onPressed: () async {
|
|
final pickedFile = await ImagePicker().pickImage(
|
|
source: ImageSource.camera,
|
|
);
|
|
if (pickedFile != null) {
|
|
setState(() {
|
|
selectedImage = File(pickedFile.path);
|
|
});
|
|
}
|
|
},
|
|
icon: const Icon(
|
|
Icons.camera_alt,
|
|
color: Color(0xFF111111),
|
|
),
|
|
label: const Text(
|
|
"Ta bild",
|
|
style: TextStyle(color: Color(0xFF111111)),
|
|
),
|
|
),
|
|
const SizedBox(height: 10),
|
|
const Divider(color: Color(0xFF111111)),
|
|
],
|
|
),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () {
|
|
textController.clear();
|
|
Navigator.pop(context);
|
|
},
|
|
child: const Text(
|
|
"Avbryt",
|
|
style: TextStyle(color: Colors.black),
|
|
),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: isPosting
|
|
? null // Disable the button
|
|
: () async {
|
|
setState(() => isPosting = true); // Set flag -> Start loading
|
|
|
|
final account = GoogleAuthService.getCurrentUser();
|
|
if (account == null) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text("Du måste vara inloggad för att posta."),
|
|
),
|
|
);
|
|
setState(() => isPosting = false);
|
|
return;
|
|
}
|
|
|
|
final email = account.email;
|
|
|
|
TeamModel? team;
|
|
try {
|
|
team = teamItems.firstWhere(
|
|
(t) => t.userEmails.contains(email),
|
|
);
|
|
} catch (e) {
|
|
team = null;
|
|
}
|
|
|
|
|
|
String? filename;
|
|
String? fileUrl;
|
|
|
|
if (selectedImage != null) {
|
|
filename = selectedImage!.path.split('/').last;
|
|
fileUrl = await imageService.upload(
|
|
XFile(selectedImage!.path),
|
|
);
|
|
}
|
|
|
|
final locationData =
|
|
await locationService.getCurrentLocationAndAddress();
|
|
|
|
final post = PostModel(
|
|
title: "",
|
|
body: textController.text,
|
|
filename: filename,
|
|
filetype: fileUrl,
|
|
time: DateTime.now(),
|
|
locationname: locationData["place"],
|
|
latitude: locationData["latitude"],
|
|
longitude: locationData["longitude"],
|
|
points: selectedMission?.points ?? 0,
|
|
teamid: account.teamId,
|
|
missionid: selectedMission?.id,
|
|
);
|
|
try {
|
|
await feedService.addPost(post);
|
|
// rawait feedService.loadPosts();
|
|
Navigator.pop(context);
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text("Inlägg skapades!")),
|
|
);
|
|
await onPostAdded();
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text("Fel: $e")),
|
|
);
|
|
} finally {
|
|
setState(() => isPosting = false); // Reset the flag
|
|
}
|
|
},
|
|
child: isPosting
|
|
? const SizedBox(
|
|
height: 16,
|
|
width: 16,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Text("Publicera"),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|