diff --git a/tarok/lib/game.dart b/tarok/lib/game/game.dart similarity index 99% rename from tarok/lib/game.dart rename to tarok/lib/game/game.dart index 6a3b836..18f7049 100644 --- a/tarok/lib/game.dart +++ b/tarok/lib/game/game.dart @@ -6,11 +6,11 @@ import 'package:flutter/services.dart'; import 'package:flutter_initicon/flutter_initicon.dart'; import 'package:get/get.dart'; import 'package:tarok/constants.dart'; -import 'package:tarok/friends.dart'; -import 'package:tarok/game_controller.dart'; +import 'package:tarok/lobby/friends.dart'; +import 'package:tarok/game/game_controller.dart'; import 'package:stockskis/stockskis.dart' as stockskis; -import 'stockskis_compatibility/compatibility.dart'; +import '../stockskis_compatibility/compatibility.dart'; class Game extends StatelessWidget { const Game({super.key}); diff --git a/tarok/lib/game_controller.dart b/tarok/lib/game/game_controller.dart similarity index 100% rename from tarok/lib/game_controller.dart rename to tarok/lib/game/game_controller.dart diff --git a/tarok/lib/friends.dart b/tarok/lib/lobby/friends.dart similarity index 98% rename from tarok/lib/friends.dart rename to tarok/lib/lobby/friends.dart index a59ce23..028c157 100644 --- a/tarok/lib/friends.dart +++ b/tarok/lib/lobby/friends.dart @@ -2,8 +2,8 @@ import 'package:flutter/material.dart'; import 'package:flutter_initicon/flutter_initicon.dart'; import 'package:get/get.dart' hide FormData; import 'package:tarok/constants.dart'; -import 'package:tarok/game_controller.dart'; -import 'package:tarok/lobby_controller.dart'; +import 'package:tarok/game/game_controller.dart'; +import 'package:tarok/lobby/lobby_controller.dart'; import 'package:tarok/ui/main_page.dart'; class Friends extends StatelessWidget { diff --git a/tarok/lib/lobby.dart b/tarok/lib/lobby/lobby.dart similarity index 99% rename from tarok/lib/lobby.dart rename to tarok/lib/lobby/lobby.dart index 1827665..8ff03f2 100644 --- a/tarok/lib/lobby.dart +++ b/tarok/lib/lobby/lobby.dart @@ -3,7 +3,7 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart' hide FormData; import 'package:stockskis/stockskis.dart' hide Card, debugPrint; import 'package:tarok/constants.dart'; -import 'package:tarok/lobby_controller.dart'; +import 'package:tarok/lobby/lobby_controller.dart'; import 'package:tarok/replay.dart'; import 'package:tarok/ui/main_page.dart'; diff --git a/tarok/lib/lobby_controller.dart b/tarok/lib/lobby/lobby_controller.dart similarity index 99% rename from tarok/lib/lobby_controller.dart rename to tarok/lib/lobby/lobby_controller.dart index 4b05ef1..eab013c 100644 --- a/tarok/lib/lobby_controller.dart +++ b/tarok/lib/lobby/lobby_controller.dart @@ -276,14 +276,6 @@ class LobbyController extends GetxController { }); } - void rerenderLogin() { - storage.read(key: "token").then((value) { - if (value == null) { - Get.toNamed("/login"); - } - }); - } - Future getRegistrationCodes() async { final response = await dio.get( "$BACKEND_URL/admin/reg_code", @@ -403,7 +395,6 @@ class LobbyController extends GetxController { isAdmin.value = value == "admin"; if (!isAdmin.value) return; }); - rerenderLogin(); // ONLINE try { diff --git a/tarok/lib/replays.dart b/tarok/lib/lobby/replays.dart similarity index 96% rename from tarok/lib/replays.dart rename to tarok/lib/lobby/replays.dart index a934979..a9c1ffa 100644 --- a/tarok/lib/replays.dart +++ b/tarok/lib/lobby/replays.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart' hide FormData; -import 'package:tarok/lobby_controller.dart'; +import 'package:tarok/lobby/lobby_controller.dart'; import 'package:tarok/replay.dart'; import 'package:tarok/ui/main_page.dart'; diff --git a/tarok/lib/login.dart b/tarok/lib/login/login.dart similarity index 82% rename from tarok/lib/login.dart rename to tarok/lib/login/login.dart index 68d9612..57cba50 100644 --- a/tarok/lib/login.dart +++ b/tarok/lib/login/login.dart @@ -1,64 +1,18 @@ -import 'dart:convert'; - -import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:get/get.dart' hide FormData; import 'package:tarok/constants.dart'; +import 'package:tarok/login/login_controller.dart'; import 'package:url_launcher/url_launcher.dart'; -class MyColorMapper implements ColorMapper { - const MyColorMapper({ - required this.baseColor, - this.accentColor, - }); - - static const _rawBaseColor = Color(0xFF293540); - static const _rawAccentColor = Color(0xFFFFFFFF); - - final Color baseColor; - final Color? accentColor; - - @override - Color substitute( - String? id, String elementName, String attributeName, Color color) { - if (color == _rawBaseColor) return baseColor; - - final accentColor = this.accentColor; - if (accentColor != null && color == _rawAccentColor) return accentColor; - - return color; - } -} - -class Login extends StatefulWidget { +class Login extends StatelessWidget { const Login({super.key}); - @override - State createState() => _LoginState(); -} - -class _LoginState extends State { - late TextEditingController _username; - late TextEditingController _password; - - @override - void initState() { - super.initState(); - _username = TextEditingController(); - _password = TextEditingController(); - } - - @override - void dispose() { - _username.dispose(); - _password.dispose(); - super.dispose(); - } - @override Widget build(BuildContext context) { + LoginController controller = Get.put(LoginController()); + return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, @@ -79,7 +33,7 @@ class _LoginState extends State { SizedBox( width: 350, child: TextField( - controller: _username, + controller: controller.email.value, decoration: const InputDecoration( border: OutlineInputBorder(), labelText: 'Elektronski naslov', @@ -92,7 +46,7 @@ class _LoginState extends State { SizedBox( width: 350, child: TextField( - controller: _password, + controller: controller.password1.value, obscureText: true, decoration: const InputDecoration( border: OutlineInputBorder(), @@ -104,19 +58,7 @@ class _LoginState extends State { height: 20, ), ElevatedButton( - onPressed: () async { - final response = await dio.post( - "$BACKEND_URL/login", - data: FormData.fromMap( - {"email": _username.text, "pass": _password.text}, - ), - ); - if (response.statusCode != 200) return; - final data = jsonDecode(response.data); - await storage.write(key: "token", value: data["token"]); - await storage.write(key: "role", value: data["role"]); - Get.toNamed("/"); - }, + onPressed: controller.login, child: const Text("Prijava", style: TextStyle(fontSize: 20)), ), const SizedBox( diff --git a/tarok/lib/login/login_controller.dart b/tarok/lib/login/login_controller.dart new file mode 100644 index 0000000..e3968d0 --- /dev/null +++ b/tarok/lib/login/login_controller.dart @@ -0,0 +1,68 @@ +import 'dart:convert'; + +import 'package:dio/dio.dart'; +import 'package:flutter/material.dart'; +import 'package:get/get.dart' hide FormData; +import 'package:tarok/constants.dart'; + +class LoginController extends GetxController { + var email = TextEditingController().obs; + var name = TextEditingController().obs; + var password1 = TextEditingController().obs; + var password2 = TextEditingController().obs; + var emailController = TextEditingController().obs; + + Future login() async { + final response = await dio.post( + "$BACKEND_URL/login", + data: FormData.fromMap( + {"email": email.value.text, "pass": password1.value.text}, + ), + ); + if (response.statusCode != 200) return; + final data = jsonDecode(response.data); + await storage.write(key: "token", value: data["token"]); + await storage.write(key: "role", value: data["role"]); + Get.toNamed("/"); + } + + Future register() async { + if (password1.value.text != password2.value.text) { + Get.dialog( + AlertDialog( + title: const Text('Gesli se ne ujemata'), + content: const SizedBox(), + actions: [ + TextButton( + onPressed: () => Get.back(), + child: const Text('OK'), + ), + ], + ), + ); + return; + } + final response = await dio.post( + "$BACKEND_URL/register", + data: FormData.fromMap( + { + "email": email.value.text, + "pass": password1.value.text, + "name": name.value.text, + "regCode": "", + }, + ), + ); + if (response.statusCode != 201) return; + Get.toNamed("/login"); + } + + @override + void onClose() { + email.value.dispose(); + name.value.dispose(); + password1.value.dispose(); + password2.value.dispose(); + super.onClose(); + } +} diff --git a/tarok/lib/register.dart b/tarok/lib/login/register.dart similarity index 55% rename from tarok/lib/register.dart rename to tarok/lib/login/register.dart index edc9f27..8175844 100644 --- a/tarok/lib/register.dart +++ b/tarok/lib/login/register.dart @@ -1,43 +1,14 @@ -import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; -import 'package:tarok/constants.dart'; +import 'package:get/get.dart'; +import 'package:tarok/login/login_controller.dart'; -class Register extends StatefulWidget { +class Register extends StatelessWidget { const Register({super.key}); - @override - State createState() => _RegisterState(); -} - -class _RegisterState extends State { - late TextEditingController _email; - late TextEditingController _username; - late TextEditingController _password; - late TextEditingController _password2; - late TextEditingController _regCode; - - @override - void initState() { - super.initState(); - _email = TextEditingController(); - _username = TextEditingController(); - _password = TextEditingController(); - _password2 = TextEditingController(); - _regCode = TextEditingController(); - } - - @override - void dispose() { - _email.dispose(); - _username.dispose(); - _password.dispose(); - _password2.dispose(); - _regCode.dispose(); - super.dispose(); - } - @override Widget build(BuildContext context) { + LoginController controller = Get.put(LoginController()); + return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, @@ -58,7 +29,7 @@ class _RegisterState extends State { SizedBox( width: 350, child: TextField( - controller: _email, + controller: controller.email.value, decoration: const InputDecoration( border: OutlineInputBorder(), labelText: 'Elektronski naslov', @@ -71,7 +42,7 @@ class _RegisterState extends State { SizedBox( width: 350, child: TextField( - controller: _username, + controller: controller.name.value, decoration: const InputDecoration( border: OutlineInputBorder(), labelText: 'Ime profila', @@ -84,7 +55,7 @@ class _RegisterState extends State { SizedBox( width: 350, child: TextField( - controller: _password2, + controller: controller.password1.value, obscureText: true, decoration: const InputDecoration( border: OutlineInputBorder(), @@ -98,7 +69,7 @@ class _RegisterState extends State { SizedBox( width: 350, child: TextField( - controller: _password, + controller: controller.password2.value, obscureText: true, decoration: const InputDecoration( border: OutlineInputBorder(), @@ -123,38 +94,7 @@ class _RegisterState extends State { height: 20, ), ElevatedButton( - onPressed: () async { - if (_password.text != _password2.text) { - showDialog( - context: context, - builder: (BuildContext context) => AlertDialog( - title: const Text('Gesli se ne ujemata'), - content: const SizedBox(), - actions: [ - TextButton( - onPressed: () => Navigator.pop(context), - child: const Text('OK'), - ), - ], - ), - ); - return; - } - final response = await dio.post( - "$BACKEND_URL/register", - data: FormData.fromMap( - { - "email": _email.text, - "pass": _password.text, - "name": _username.text, - "regCode": _regCode.text - }, - ), - ); - if (response.statusCode != 201) return; - // ignore: use_build_context_synchronously - Navigator.pop(context); - }, + onPressed: controller.register, child: const Text("Registracija", style: TextStyle(fontSize: 20)), ), const SizedBox( @@ -162,7 +102,7 @@ class _RegisterState extends State { ), ElevatedButton( onPressed: () { - Navigator.pop(context); + Get.toNamed("/login"); }, child: const Text("Prijava", style: TextStyle(fontSize: 20)), ), diff --git a/tarok/lib/main.dart b/tarok/lib/main.dart index 2481a44..5747169 100644 --- a/tarok/lib/main.dart +++ b/tarok/lib/main.dart @@ -7,13 +7,13 @@ import 'package:shared_preferences/shared_preferences.dart'; import 'package:stockskis/stockskis.dart' hide debugPrint, Card; import 'package:tarok/about.dart'; import 'package:tarok/constants.dart'; -import 'package:tarok/friends.dart'; -import 'package:tarok/game.dart'; -import 'package:tarok/lobby.dart'; -import 'package:tarok/login.dart'; -import 'package:tarok/register.dart'; +import 'package:tarok/game/game.dart'; +import 'package:tarok/lobby/friends.dart'; +import 'package:tarok/lobby/lobby.dart'; +import 'package:tarok/lobby/replays.dart'; +import 'package:tarok/login/login.dart'; +import 'package:tarok/login/register.dart'; import 'package:tarok/replay.dart'; -import 'package:tarok/replays.dart'; import 'package:tarok/settings.dart'; import 'package:tarok/sounds.dart'; import 'package:url_strategy/url_strategy.dart'; @@ -61,6 +61,12 @@ void main() async { Get.changeThemeMode(ThemeMode.dark); } + String initialRoute = "/"; + var value = await storage.read(key: "token"); + if (value == null) { + initialRoute = "/login"; + } + runApp( GetMaterialApp( title: 'Tarok palcka.si', @@ -69,7 +75,7 @@ void main() async { useMaterial3: true, brightness: Brightness.light, ), - initialRoute: '/', + initialRoute: initialRoute, getPages: [ GetPage(name: '/', page: () => const Lobby()), GetPage(name: '/game', page: () => const Game()), @@ -96,7 +102,6 @@ void main() async { brightness: Brightness.dark, ), builder: InAppNotifications.init(), - home: const Lobby(), ), ); }