Skip to content

Commit 7e01469

Browse files
author
Safa
committed
AuthenticationManager added
1 parent adb630e commit 7e01469

File tree

6 files changed

+132
-36
lines changed

6 files changed

+132
-36
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import 'package:flutter/foundation.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_architecture_template/core/init/cache/locale_manager.dart';
4+
import 'package:flutter_architecture_template/core/init/main/main_init.dart';
5+
import 'package:flutter_architecture_template/product/navigator/app_router.dart';
6+
import '../../features/authentication/login/model/login_response_model.dart';
7+
import '../constants/project_items.dart';
8+
import '../init/toast/toast_service.dart';
9+
10+
class AuthenticationManager with ToastService {
11+
AuthenticationManager._();
12+
static final AuthenticationManager _instance = AuthenticationManager._();
13+
14+
static AuthenticationManager get instance => _instance;
15+
16+
void logOut({bool force = false}) async {
17+
try {
18+
await LocaleManager.instance.clearAll();
19+
getIt<AppRouter>().replace(const SplashRoute());
20+
} catch (error) {
21+
if (kDebugMode) {
22+
debugPrint(
23+
'***** DEBUG MODE ***** -- AuthenticationManager -- \n${error.toString()}');
24+
}
25+
} finally {}
26+
}
27+
28+
void login(
29+
BuildContext context, LoginResponseModel loginResponseModel) async {
30+
try {
31+
await LocaleManager.instance.setStringValue(PreferencesKeys.token,
32+
value: loginResponseModel.token!);
33+
if (context.mounted) {
34+
showToast(label: ProjectItems.loginSuccess);
35+
}
36+
getIt<AppRouter>().replace(const HomeRoute());
37+
} catch (e) {
38+
if (kDebugMode) {
39+
debugPrint(
40+
'***** DEBUG MODE *****\nAuthenticationManager-login\n${e.toString()}');
41+
}
42+
}
43+
}
44+
45+
bool checkLoginStatus() {
46+
var token = LocaleManager.instance.getStringValue(PreferencesKeys.token);
47+
if (token != null) {
48+
return true;
49+
}
50+
return false;
51+
}
52+
53+
Future<void> updateToken(LoginResponseModel model) async {}
54+
}

lib/core/constants/project_items.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ class ProjectItems {
1010
static const String facebook = 'Facebook';
1111
static const String google = 'Google';
1212
static const String apple = 'Apple';
13+
static const String loginSuccess = 'Giriş başarılı';
1314
}

lib/core/init/cache/locale_manager.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ class LocaleManager {
3434
_preferences?.getBool(preferencesKeys.toString());
3535
}
3636

37-
enum PreferencesKeys { theme }
37+
enum PreferencesKeys { theme,token }

lib/core/init/toast/toast_service.dart

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import 'package:bot_toast/bot_toast.dart';
2+
import 'package:flutter/foundation.dart';
23
import 'package:flutter/material.dart';
34
import 'package:flutter_architecture_template/core/components/widgets/svg_widget.dart';
45
import 'package:flutter_architecture_template/core/constants/project_variables.dart';
56
import 'package:flutter_architecture_template/core/extension/context_extension.dart';
6-
7+
import '../../../product/navigator/app_router.dart';
78
import '../../constants/colors.dart';
89
import '../../constants/project_items.dart';
10+
import '../main/main_init.dart';
911

1012
enum ToastType { success, error, info, warning }
1113

@@ -14,43 +16,60 @@ String? _toastTitle;
1416
Color? _color;
1517
Color? _leftBorderColor;
1618

17-
mixin ToastService<T extends StatefulWidget> on State<T> {
19+
mixin ToastService {
1820
void showToast({required String label, ToastType? toastType}) {
1921
_toastTypeControl(toastType);
2022
BotToast.showCustomNotification(
23+
align: kIsWeb ? Alignment.bottomRight : null,
2124
toastBuilder: (_) => Padding(
22-
padding: context.paddingHighHorizontal,
23-
child: Card(
24-
shape: RoundedRectangleBorder(
25-
borderRadius: context.borderRadiusLow,
26-
),
27-
color: _color,
28-
child: IntrinsicHeight(
29-
child: Row(
30-
mainAxisSize: MainAxisSize.min,
31-
children: [
32-
_leftBorderContainer,
33-
const SizedBox(width: 10),
34-
_iconWidget,
35-
const SizedBox(width: 5),
36-
Expanded(
37-
child: Padding(
38-
padding: context.paddingLow,
39-
child: Column(
40-
crossAxisAlignment: CrossAxisAlignment.start,
41-
children: [
42-
_headerText,
43-
_subText(label),
44-
],
25+
padding: getIt<AppRouter>()
26+
.root
27+
.navigatorKey
28+
.currentContext!
29+
.paddingMediumHorizontal,
30+
child: SizedBox(
31+
width: kIsWeb ? 350 : null,
32+
child: Card(
33+
shape: RoundedRectangleBorder(
34+
borderRadius: getIt<AppRouter>()
35+
.root
36+
.navigatorKey
37+
.currentContext!
38+
.borderRadiusLow,
39+
),
40+
color: _color,
41+
child: IntrinsicHeight(
42+
child: Row(
43+
mainAxisSize: MainAxisSize.min,
44+
children: [
45+
_leftBorderContainer,
46+
const SizedBox(width: 10),
47+
_iconWidget,
48+
const SizedBox(width: 5),
49+
Expanded(
50+
child: Padding(
51+
padding: getIt<AppRouter>()
52+
.root
53+
.navigatorKey
54+
.currentContext!
55+
.paddingLow,
56+
child: Column(
57+
crossAxisAlignment: CrossAxisAlignment.start,
58+
children: [
59+
_headerText,
60+
_subText(label),
61+
],
62+
),
4563
),
46-
),
47-
)
48-
],
64+
)
65+
],
66+
),
4967
),
5068
),
5169
),
5270
),
53-
duration: context.highDuration);
71+
duration:
72+
getIt<AppRouter>().root.navigatorKey.currentContext!.highDuration);
5473
}
5574

5675
void _toastTypeControl(ToastType? toastType) {
@@ -86,26 +105,41 @@ mixin ToastService<T extends StatefulWidget> on State<T> {
86105
height: ProjectVaribles.normalValue,
87106
width: ProjectVaribles.normalValue,
88107
child: SvgWidget(
89-
svgPath: SvgPath.svgCheckPath,
108+
svgPath: _svgPath ?? SvgPath.svgCheckPath,
90109
size: ProjectVaribles.normalValue,
91110
),
92111
);
93112

94113
Widget _subText(label) => Text(
95114
label,
96115
overflow: TextOverflow.clip,
97-
style: context.textTheme.caption!.copyWith(fontWeight: FontWeight.w500),
116+
style: getIt<AppRouter>()
117+
.root
118+
.navigatorKey
119+
.currentContext!
120+
.textTheme
121+
.titleSmall!
122+
.copyWith(fontWeight: FontWeight.w500),
98123
);
99124

100125
Widget get _headerText => Text(
101126
'${_toastTitle!.toUpperCase()}!',
102-
style:
103-
context.textTheme.bodySmall!.copyWith(fontWeight: FontWeight.bold),
127+
style: getIt<AppRouter>()
128+
.root
129+
.navigatorKey
130+
.currentContext!
131+
.textTheme
132+
.titleSmall!
133+
.copyWith(fontWeight: FontWeight.bold),
104134
);
105135

106136
Widget get _leftBorderContainer => Container(
107137
decoration: BoxDecoration(
108-
borderRadius: context.borderRadiusLeftLow,
138+
borderRadius: getIt<AppRouter>()
139+
.root
140+
.navigatorKey
141+
.currentContext!
142+
.borderRadiusLeftLow,
109143
color: _leftBorderColor,
110144
),
111145
height: double.infinity,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class LoginResponseModel {
2+
String? token;
3+
LoginResponseModel(this.token);
4+
}

lib/features/authentication/login/view/login_view.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import 'package:flutter_architecture_template/core/components/widgets/social/soc
33
import 'package:flutter_architecture_template/core/components/widgets/social/social_button.dart';
44
import 'package:flutter_architecture_template/core/constants/colors.dart';
55
import 'package:flutter_architecture_template/core/extension/context_extension.dart';
6+
import 'package:flutter_architecture_template/product/navigator/app_router.dart';
7+
8+
import '../../../../core/init/main/main_init.dart';
69

710
class LoginView extends StatefulWidget {
811
const LoginView({Key? key}) : super(key: key);
@@ -26,7 +29,7 @@ class _LoginViewState extends State<LoginView> {
2629
children: [
2730
SocialButton(
2831
socialAdapter: FacebookAdapter(),
29-
onComplate: print,
32+
onComplate: (result) => getIt<AppRouter>().replace(const HomeRoute()),
3033
),
3134
SocialButton(
3235
socialAdapter: GoogleAdapter(),

0 commit comments

Comments
 (0)