157 lines
5.3 KiB
Dart
157 lines
5.3 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:solaragent/auth.dart';
|
|
import 'package:solaragent/router.dart';
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
class MomentEditorScreen extends StatefulWidget {
|
|
const MomentEditorScreen({super.key});
|
|
|
|
@override
|
|
State<MomentEditorScreen> createState() => _MomentEditorScreenState();
|
|
}
|
|
|
|
class _MomentEditorScreenState extends State<MomentEditorScreen> {
|
|
final contentController = TextEditingController();
|
|
|
|
bool isSubmitting = false;
|
|
|
|
bool showRecommendationBanner = true;
|
|
|
|
Future<void> postMoment() async {
|
|
if (authClient.client == null) return;
|
|
|
|
setState(() => isSubmitting = true);
|
|
var res = await authClient.client!.post(
|
|
Uri.parse("https://co.solsynth.dev/api/p/moments"),
|
|
headers: <String, String>{
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: jsonEncode(<String, dynamic>{
|
|
'content': contentController.value.text,
|
|
}),
|
|
);
|
|
if (res.statusCode != 200) {
|
|
var message = utf8.decode(res.bodyBytes);
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text("Something went wrong... $message")),
|
|
);
|
|
} else {
|
|
if (router.canPop()) {
|
|
router.pop();
|
|
}
|
|
}
|
|
setState(() => isSubmitting = false);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text("Record a moment"),
|
|
actions: <Widget>[
|
|
TextButton(
|
|
onPressed: !isSubmitting ? postMoment : null,
|
|
child: const Text('POST'),
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
children: [
|
|
// Loading indicator
|
|
isSubmitting ? const LinearProgressIndicator() : Container(),
|
|
// Userinfo
|
|
FutureBuilder(
|
|
future: authClient.getProfiles(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.hasData) {
|
|
var userinfo = snapshot.data;
|
|
return Container(
|
|
color: Colors.grey[50],
|
|
margin: const EdgeInsets.only(bottom: 12),
|
|
child: ListTile(
|
|
title: Text(userinfo["nick"]),
|
|
subtitle: const Text("You will post this post as"),
|
|
leading: CircleAvatar(
|
|
backgroundImage: NetworkImage(userinfo["picture"]),
|
|
),
|
|
),
|
|
);
|
|
} else {
|
|
return Container();
|
|
}
|
|
}),
|
|
// Editor
|
|
Expanded(
|
|
child: Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
child: TextField(
|
|
maxLines: null,
|
|
autofocus: true,
|
|
autocorrect: true,
|
|
keyboardType: TextInputType.multiline,
|
|
controller: contentController,
|
|
decoration: const InputDecoration.collapsed(
|
|
hintText: "What\'s happened?!"),
|
|
),
|
|
),
|
|
),
|
|
// Recommend website banner
|
|
showRecommendationBanner
|
|
? FutureBuilder(
|
|
future: SharedPreferences.getInstance(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.hasData &&
|
|
snapshot.data?.getBool(
|
|
"editor.hide_website_recommendation") ==
|
|
null) {
|
|
snapshot.data?.remove("editor.hide_website_recommendation");
|
|
return MaterialBanner(
|
|
padding: const EdgeInsets.all(20),
|
|
content: const Text(
|
|
'SolarAgent still in early stage development. Some features isn\'t available. We recommend use our website, also optimized for moblie!',
|
|
),
|
|
leading: const Icon(Icons.construction),
|
|
backgroundColor: const Color(0xFFE0E0E0),
|
|
actions: <Widget>[
|
|
TextButton(
|
|
child: const Text('OPEN'),
|
|
onPressed: () async {
|
|
await launchUrl(
|
|
Uri.parse("https://co.solsynth.dev"));
|
|
},
|
|
),
|
|
TextButton(
|
|
child: const Text('DISMISS'),
|
|
onPressed: () async {
|
|
await snapshot.data?.setBool(
|
|
"editor.hide_website_recommendation",
|
|
true,
|
|
);
|
|
setState(() {
|
|
showRecommendationBanner = false;
|
|
});
|
|
},
|
|
),
|
|
],
|
|
);
|
|
} else {
|
|
return Container();
|
|
}
|
|
})
|
|
: Container(),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
contentController.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|