Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.2 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. import '/components/custom_title_bar.dart';
  4. import '/models/my_profile.dart';
  5. import '/utils/storage/database.dart';
  6. @immutable
  7. class ChangeServerUrl extends StatefulWidget {
  8. const ChangeServerUrl({
  9. Key? key,
  10. }) : super(key: key);
  11. @override
  12. State<ChangeServerUrl> createState() => _ChangeServerUrl();
  13. }
  14. class _ChangeServerUrl extends State<ChangeServerUrl> {
  15. final _formKey = GlobalKey<FormState>();
  16. final TextEditingController _serverUrlController = TextEditingController();
  17. bool invalidCurrentPassword = false;
  18. @override
  19. void initState() {
  20. setUrl();
  21. super.initState();
  22. }
  23. Future<void> setUrl() async {
  24. final preferences = await SharedPreferences.getInstance();
  25. _serverUrlController.text = preferences.getString('server_url') ?? defaultServerUrl;
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. return Scaffold(
  30. appBar: const CustomTitleBar(
  31. title: Text(
  32. 'Profile',
  33. style: TextStyle(
  34. fontSize: 32,
  35. fontWeight: FontWeight.bold
  36. )
  37. ),
  38. showBack: true,
  39. backgroundColor: Colors.transparent,
  40. ),
  41. body: SingleChildScrollView(
  42. child: Center(
  43. child: Form(
  44. key: _formKey,
  45. child: Padding(
  46. padding: const EdgeInsets.only(
  47. left: 20,
  48. right: 20,
  49. top: 30,
  50. ),
  51. child: Column(
  52. crossAxisAlignment: CrossAxisAlignment.center,
  53. children: [
  54. const Text(
  55. 'Change Server Url',
  56. style: TextStyle(
  57. fontSize: 25,
  58. ),
  59. ),
  60. const SizedBox(height: 30),
  61. showWarning(),
  62. const SizedBox(height: 30),
  63. TextFormField(
  64. controller: _serverUrlController,
  65. decoration: const InputDecoration(
  66. hintText: 'Server Url',
  67. ),
  68. // The validator receives the text that the user has entered.
  69. validator: (String? value) {
  70. if (value == null || !Uri.parse(value).isAbsolute) {
  71. return 'Invalid URL';
  72. }
  73. return null;
  74. },
  75. ),
  76. const SizedBox(height: 15),
  77. ElevatedButton(
  78. onPressed: () {
  79. if (!_formKey.currentState!.validate()) {
  80. return;
  81. }
  82. ScaffoldMessenger.of(context).showSnackBar(
  83. const SnackBar(content: Text('Processing Data')),
  84. );
  85. },
  86. child: const Text('CHANGE SERVER URL'),
  87. ),
  88. ],
  89. )
  90. )
  91. )
  92. )
  93. )
  94. );
  95. }
  96. Widget showWarning() {
  97. String warning1 = '''
  98. WARNING: Do not use this feature unless you know what you\'re doing!
  99. ''';
  100. String warning2 = '''
  101. Changing the server url will disconnect you from all friends and conversations on this server, and connect you to a fresh environment. This feature is intended to be used by people that are willing to host their own Capsule server, which you can find by going to \nhttps://github.com/SomeUsername/SomeRepo.\n\n
  102. You can revert this by entering \nhttps://envelope-messenger.com\n on the login screen.
  103. ''';
  104. return Column(
  105. children: [
  106. Text(
  107. warning1,
  108. textAlign: TextAlign.center,
  109. style: TextStyle(
  110. color: Theme.of(context).colorScheme.error,
  111. )
  112. ),
  113. Text(
  114. warning2,
  115. textAlign: TextAlign.center,
  116. ),
  117. ],
  118. );
  119. }
  120. // TODO: Write user data to new server??
  121. Future<void> changeUrl() async {
  122. MyProfile.setServerUrl(_serverUrlController.text);
  123. deleteDb();
  124. MyProfile.logout();
  125. Navigator.pushNamedAndRemoveUntil(context, '/landing', ModalRoute.withName('/landing'));
  126. }
  127. }