mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-15 17:47:28 +00:00
getting spaces and rooms from api {null checks}
This commit is contained in:
@ -16,6 +16,13 @@ class NavCubit extends Cubit<NavState> {
|
|||||||
NavCubit() : super(NavInitial());
|
NavCubit() : super(NavInitial());
|
||||||
|
|
||||||
static NavCubit of(context) => BlocProvider.of<NavCubit>(context);
|
static NavCubit of(context) => BlocProvider.of<NavCubit>(context);
|
||||||
|
|
||||||
|
//functoin to do the important work when the user logs out
|
||||||
|
static clear() {
|
||||||
|
pageIndex = 0;
|
||||||
|
pageController.jumpToPage(0);
|
||||||
|
}
|
||||||
|
|
||||||
static int pageIndex = 0;
|
static int pageIndex = 0;
|
||||||
|
|
||||||
static Map<String, List<Widget>> appBarActions = {
|
static Map<String, List<Widget>> appBarActions = {
|
||||||
@ -139,11 +146,10 @@ class NavCubit extends Cubit<NavState> {
|
|||||||
const MenuView(),
|
const MenuView(),
|
||||||
];
|
];
|
||||||
|
|
||||||
final PageController pageController = PageController();
|
static final PageController pageController = PageController();
|
||||||
|
|
||||||
void updatePageIndex(int index) {
|
void updatePageIndex(int index) {
|
||||||
pageIndex = index;
|
pageIndex = index;
|
||||||
print('index: $index');
|
|
||||||
pageController.animateToPage(index,
|
pageController.animateToPage(index,
|
||||||
duration: const Duration(milliseconds: 150), curve: Curves.easeIn);
|
duration: const Duration(milliseconds: 150), curve: Curves.easeIn);
|
||||||
emit(NavChangePage());
|
emit(NavChangePage());
|
||||||
|
@ -10,7 +10,11 @@ part 'spaces_state.dart';
|
|||||||
|
|
||||||
class SpacesCubit extends Cubit<SpacesState> {
|
class SpacesCubit extends Cubit<SpacesState> {
|
||||||
SpacesCubit() : super(SpacesInitial()) {
|
SpacesCubit() : super(SpacesInitial()) {
|
||||||
fetchSpaces();
|
fetchSpaces().then((value) {
|
||||||
|
if (selectedSpace != null) {
|
||||||
|
fetchRooms(selectedSpace!);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static SpacesCubit get(context) => BlocProvider.of(context);
|
static SpacesCubit get(context) => BlocProvider.of(context);
|
||||||
@ -83,12 +87,24 @@ class SpacesCubit extends Cubit<SpacesState> {
|
|||||||
emit(SpacesLoading());
|
emit(SpacesLoading());
|
||||||
try {
|
try {
|
||||||
spaces = await SpacesAPI.getSpaces();
|
spaces = await SpacesAPI.getSpaces();
|
||||||
|
spaces.isNotEmpty ? selectSpace(spaces.first) : null;
|
||||||
emit(SpacesLoaded(spaces));
|
emit(SpacesLoaded(spaces));
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
||||||
emit(SpacesError(e.message ?? "Something went wrong"));
|
emit(SpacesError(ServerFailure.fromDioError(e).errMessage));
|
||||||
throw ServerFailure.fromDioError(e);
|
}
|
||||||
} catch (e) {
|
}
|
||||||
emit(SpacesError(e.toString()));
|
|
||||||
|
fetchRooms(SpaceModel space) async {
|
||||||
|
emit(SpaceRoomsLoading());
|
||||||
|
try {
|
||||||
|
space.rooms = await SpacesAPI.getRooms(space.id!);
|
||||||
|
if (space.rooms != null) {
|
||||||
|
emit(SpaceRoomsLoaded(space.rooms!));
|
||||||
|
} else {
|
||||||
|
emit(SpaceRoomsError("No rooms found"));
|
||||||
|
}
|
||||||
|
} on DioException catch (e) {
|
||||||
|
emit(SpacesError(ServerFailure.fromDioError(e).errMessage));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,18 @@ class SpacesError extends SpacesState {
|
|||||||
SpacesError(this.errMessage);
|
SpacesError(this.errMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class SpaceRoomsLoading extends SpacesLoading {}
|
||||||
|
|
||||||
|
class SpaceRoomsLoaded extends SpacesLoading {
|
||||||
|
final List<RoomModel> rooms;
|
||||||
|
|
||||||
|
SpaceRoomsLoaded(this.rooms);
|
||||||
|
}
|
||||||
|
|
||||||
|
class SpaceRoomsError extends SpacesError {
|
||||||
|
SpaceRoomsError(super.errMessage);
|
||||||
|
}
|
||||||
|
|
||||||
class SpacesSelected extends SpacesState {
|
class SpacesSelected extends SpacesState {
|
||||||
final SpaceModel space;
|
final SpaceModel space;
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ import 'package:syncrow_app/features/devices/model/room_model.dart';
|
|||||||
class SpaceModel {
|
class SpaceModel {
|
||||||
final int? id;
|
final int? id;
|
||||||
final String? name;
|
final String? name;
|
||||||
final List<RoomModel>? rooms;
|
late List<RoomModel>? rooms;
|
||||||
|
|
||||||
SpaceModel({
|
SpaceModel({
|
||||||
required this.id,
|
required this.id,
|
||||||
|
@ -6,6 +6,7 @@ import 'package:syncrow_app/features/app_layout/bloc/spaces_cubit.dart';
|
|||||||
import 'package:syncrow_app/features/app_layout/view/widgets/app_body.dart';
|
import 'package:syncrow_app/features/app_layout/view/widgets/app_body.dart';
|
||||||
import 'package:syncrow_app/features/app_layout/view/widgets/default_app_bar.dart';
|
import 'package:syncrow_app/features/app_layout/view/widgets/default_app_bar.dart';
|
||||||
import 'package:syncrow_app/features/app_layout/view/widgets/default_nav_bar.dart';
|
import 'package:syncrow_app/features/app_layout/view/widgets/default_nav_bar.dart';
|
||||||
|
import 'package:syncrow_app/navigation/routing_constants.dart';
|
||||||
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
||||||
|
|
||||||
class AppLayout extends StatelessWidget {
|
class AppLayout extends StatelessWidget {
|
||||||
@ -13,30 +14,51 @@ class AppLayout extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocBuilder<NavCubit, NavState>(
|
return BlocProvider(
|
||||||
builder: (context, state) {
|
create: (context) => SpacesCubit(),
|
||||||
return AnnotatedRegion(
|
child: BlocListener<SpacesCubit, SpacesState>(
|
||||||
value: SystemUiOverlayStyle(
|
listener: (context, state) {
|
||||||
statusBarColor: ColorsManager.primaryColor.withOpacity(0.5),
|
if (state is SpacesError) {
|
||||||
statusBarIconBrightness: Brightness.light,
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
),
|
SnackBar(
|
||||||
child: SafeArea(
|
content: Text(state.errMessage),
|
||||||
child: BlocBuilder<SpacesCubit, SpacesState>(
|
),
|
||||||
builder: (context, state) {
|
);
|
||||||
return Scaffold(
|
Navigator.of(context)
|
||||||
backgroundColor: ColorsManager.backgroundColor,
|
.popUntil((route) => route.settings.name == Routes.authLogin);
|
||||||
extendBodyBehindAppBar: true,
|
}
|
||||||
extendBody: true,
|
},
|
||||||
appBar: state is SpacesLoaded ? DefaultAppBar(context) : null,
|
child: BlocBuilder<NavCubit, NavState>(
|
||||||
body: const AppBody(),
|
builder: (context, state) {
|
||||||
bottomNavigationBar:
|
return AnnotatedRegion(
|
||||||
state is SpacesLoaded ? const DefaultNavBar() : null,
|
value: SystemUiOverlayStyle(
|
||||||
);
|
statusBarColor: ColorsManager.primaryColor.withOpacity(0.5),
|
||||||
},
|
statusBarIconBrightness: Brightness.light,
|
||||||
),
|
),
|
||||||
),
|
child: SafeArea(
|
||||||
);
|
child: BlocBuilder<SpacesCubit, SpacesState>(
|
||||||
},
|
builder: (context, state) {
|
||||||
|
return Scaffold(
|
||||||
|
backgroundColor: ColorsManager.backgroundColor,
|
||||||
|
extendBodyBehindAppBar: true,
|
||||||
|
extendBody: true,
|
||||||
|
appBar:
|
||||||
|
state is! SpacesLoading || state is! SpaceRoomsLoading
|
||||||
|
? const DefaultAppBar()
|
||||||
|
: null,
|
||||||
|
body: const AppBody(),
|
||||||
|
bottomNavigationBar:
|
||||||
|
state is! SpacesLoading || state is! SpaceRoomsLoading
|
||||||
|
? const DefaultNavBar()
|
||||||
|
: null,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,10 +28,10 @@ class AppBarHomeDropdown extends StatelessWidget {
|
|||||||
underline: const SizedBox.shrink(),
|
underline: const SizedBox.shrink(),
|
||||||
padding: const EdgeInsets.all(0),
|
padding: const EdgeInsets.all(0),
|
||||||
borderRadius: BorderRadius.circular(20),
|
borderRadius: BorderRadius.circular(20),
|
||||||
value: SpacesCubit.get(context).selectedSpace,
|
value: SpacesCubit.get(context).selectedSpace!.id,
|
||||||
items: SpacesCubit.spaces.map((space) {
|
items: SpacesCubit.spaces.map((space) {
|
||||||
return DropdownMenuItem(
|
return DropdownMenuItem(
|
||||||
value: space,
|
value: space.id,
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
width: 100,
|
width: 100,
|
||||||
child: Row(
|
child: Row(
|
||||||
@ -50,7 +50,7 @@ class AppBarHomeDropdown extends StatelessWidget {
|
|||||||
const SizedBox(width: 5),
|
const SizedBox(width: 5),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: BodyMedium(
|
child: BodyMedium(
|
||||||
text: space.name ?? "",
|
text: space.name ?? "??",
|
||||||
style: context.bodyMedium.copyWith(
|
style: context.bodyMedium.copyWith(
|
||||||
fontSize: 15,
|
fontSize: 15,
|
||||||
color: ColorsManager.textPrimaryColor,
|
color: ColorsManager.textPrimaryColor,
|
||||||
@ -64,7 +64,10 @@ class AppBarHomeDropdown extends StatelessWidget {
|
|||||||
);
|
);
|
||||||
}).toList(),
|
}).toList(),
|
||||||
onChanged: (value) {
|
onChanged: (value) {
|
||||||
SpacesCubit.get(context).selectSpace(value!);
|
if (value != null) {
|
||||||
|
SpacesCubit.get(context).selectSpace(SpacesCubit.spaces
|
||||||
|
.firstWhere((element) => element.id == value));
|
||||||
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -15,39 +15,38 @@ class AppBody extends StatelessWidget {
|
|||||||
return BlocBuilder<NavCubit, NavState>(
|
return BlocBuilder<NavCubit, NavState>(
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
return Container(
|
return Container(
|
||||||
width: MediaQuery.sizeOf(context).width,
|
width: MediaQuery.sizeOf(context).width,
|
||||||
height: MediaQuery.sizeOf(context).height,
|
height: MediaQuery.sizeOf(context).height,
|
||||||
decoration: const BoxDecoration(
|
decoration: const BoxDecoration(
|
||||||
image: DecorationImage(
|
image: DecorationImage(
|
||||||
image: AssetImage(
|
image: AssetImage(
|
||||||
Assets.imagesBackground,
|
Assets.imagesBackground,
|
||||||
),
|
|
||||||
fit: BoxFit.cover,
|
|
||||||
opacity: 0.4,
|
|
||||||
),
|
),
|
||||||
|
fit: BoxFit.cover,
|
||||||
|
opacity: 0.4,
|
||||||
),
|
),
|
||||||
child: BlocConsumer<SpacesCubit, SpacesState>(
|
),
|
||||||
listener: (context, state) {
|
child: BlocConsumer<SpacesCubit, SpacesState>(
|
||||||
if (state is SpacesError) {
|
listener: (context, state) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
if (state is SpacesError) {
|
||||||
SnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
content: Text(state.errMessage),
|
SnackBar(
|
||||||
),
|
content: Text(state.errMessage),
|
||||||
);
|
),
|
||||||
}
|
);
|
||||||
},
|
}
|
||||||
builder: (context, state) {
|
},
|
||||||
return state is! SpacesLoading
|
builder: (context, state) {
|
||||||
? PageView(
|
return state is! SpacesLoading || state is! SpaceRoomsLoading
|
||||||
physics: const NeverScrollableScrollPhysics(),
|
? PageView(
|
||||||
controller: NavCubit.of(context).pageController,
|
physics: const NeverScrollableScrollPhysics(),
|
||||||
children: NavCubit.of(context).pages,
|
controller: NavCubit.pageController,
|
||||||
)
|
children: NavCubit.of(context).pages,
|
||||||
: const Center(child: CircularProgressIndicator());
|
)
|
||||||
},
|
: const Center(child: CircularProgressIndicator());
|
||||||
)
|
},
|
||||||
// NavCubit.of(context).currentPage,
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,7 @@ import 'package:syncrow_app/features/app_layout/bloc/nav_cubit.dart';
|
|||||||
import 'package:syncrow_app/utils/resource_manager/constants.dart';
|
import 'package:syncrow_app/utils/resource_manager/constants.dart';
|
||||||
|
|
||||||
class DefaultAppBar extends StatelessWidget implements PreferredSizeWidget {
|
class DefaultAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||||
const DefaultAppBar(this.context, {super.key});
|
const DefaultAppBar({super.key});
|
||||||
|
|
||||||
final BuildContext context;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
@ -3,6 +3,7 @@ import 'package:flutter/cupertino.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||||
|
import 'package:syncrow_app/features/app_layout/bloc/nav_cubit.dart';
|
||||||
import 'package:syncrow_app/features/auth/model/login_with_email_model.dart';
|
import 'package:syncrow_app/features/auth/model/login_with_email_model.dart';
|
||||||
import 'package:syncrow_app/features/auth/model/token.dart';
|
import 'package:syncrow_app/features/auth/model/token.dart';
|
||||||
import 'package:syncrow_app/features/auth/model/user_model.dart';
|
import 'package:syncrow_app/features/auth/model/user_model.dart';
|
||||||
@ -20,7 +21,7 @@ class AuthCubit extends Cubit<AuthState> {
|
|||||||
TextEditingController passwordController = TextEditingController();
|
TextEditingController passwordController = TextEditingController();
|
||||||
bool isPasswordVisible = false;
|
bool isPasswordVisible = false;
|
||||||
|
|
||||||
GlobalKey<FormState> formKey = GlobalKey<FormState>();
|
static GlobalKey<FormState> formKey = GlobalKey<FormState>();
|
||||||
|
|
||||||
void changePasswordVisibility() {
|
void changePasswordVisibility() {
|
||||||
isPasswordVisible = !isPasswordVisible;
|
isPasswordVisible = !isPasswordVisible;
|
||||||
@ -54,15 +55,16 @@ class AuthCubit extends Cubit<AuthState> {
|
|||||||
FlutterSecureStorage storage = const FlutterSecureStorage();
|
FlutterSecureStorage storage = const FlutterSecureStorage();
|
||||||
await storage.write(
|
await storage.write(
|
||||||
key: Token.loginAccessTokenKey, value: token.accessToken);
|
key: Token.loginAccessTokenKey, value: token.accessToken);
|
||||||
|
const FlutterSecureStorage().write(
|
||||||
|
key: UserModel.userUuidKey,
|
||||||
|
value: Token.decodeToken(token.accessToken)['uuid'].toString());
|
||||||
user = UserModel.fromToken(token);
|
user = UserModel.fromToken(token);
|
||||||
emit(AuthSuccess());
|
emit(AuthSuccess());
|
||||||
} else {
|
} else {
|
||||||
emit(AuthError('Something went wrong'));
|
emit(AuthError('Something went wrong'));
|
||||||
}
|
}
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
||||||
emit(AuthError(e.message ?? "Something went wrong"));
|
emit(AuthError(ServerFailure.fromDioError(e).errMessage));
|
||||||
throw ServerFailure.fromDioError(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,9 +73,10 @@ class AuthCubit extends Cubit<AuthState> {
|
|||||||
try {
|
try {
|
||||||
FlutterSecureStorage storage = const FlutterSecureStorage();
|
FlutterSecureStorage storage = const FlutterSecureStorage();
|
||||||
await storage.delete(key: Token.loginAccessTokenKey);
|
await storage.delete(key: Token.loginAccessTokenKey);
|
||||||
|
NavCubit.clear();
|
||||||
emit(AuthLoggedOut());
|
emit(AuthLoggedOut());
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
||||||
throw ServerFailure.fromDioError(e);
|
emit(AuthError(ServerFailure.fromDioError(e).errMessage));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,26 @@ class Token {
|
|||||||
final String accessToken;
|
final String accessToken;
|
||||||
final String refreshToken;
|
final String refreshToken;
|
||||||
|
|
||||||
|
//{
|
||||||
|
// "email": "test@test.com",
|
||||||
|
// "userId": 3,
|
||||||
|
// "uuid": "563e22d2-cb30-46d3-8c48-fa7d762342f0",
|
||||||
|
// "sessionId": "f76aa067-c915-4921-b04d-9fbc71c4965a",
|
||||||
|
// "iat": 1710137435,
|
||||||
|
// "exp": 1710137735
|
||||||
|
// }
|
||||||
|
|
||||||
|
final String sessionId;
|
||||||
|
|
||||||
|
final int iat;
|
||||||
|
final int exp;
|
||||||
|
|
||||||
Token.emptyConstructor()
|
Token.emptyConstructor()
|
||||||
: accessToken = '',
|
: accessToken = '',
|
||||||
refreshToken = '';
|
refreshToken = '',
|
||||||
|
sessionId = '',
|
||||||
|
iat = 0,
|
||||||
|
exp = 0;
|
||||||
|
|
||||||
bool get accessTokenIsNotEmpty => accessToken.isNotEmpty;
|
bool get accessTokenIsNotEmpty => accessToken.isNotEmpty;
|
||||||
|
|
||||||
@ -23,9 +40,16 @@ class Token {
|
|||||||
Token(
|
Token(
|
||||||
this.accessToken,
|
this.accessToken,
|
||||||
this.refreshToken,
|
this.refreshToken,
|
||||||
|
this.sessionId,
|
||||||
|
this.iat,
|
||||||
|
this.exp,
|
||||||
);
|
);
|
||||||
|
|
||||||
Token.refreshToken(this.refreshToken) : accessToken = '';
|
Token.refreshToken(this.refreshToken)
|
||||||
|
: accessToken = '',
|
||||||
|
sessionId = '',
|
||||||
|
iat = 0,
|
||||||
|
exp = 0;
|
||||||
|
|
||||||
factory Token.fromJson(Map<String, dynamic> json) {
|
factory Token.fromJson(Map<String, dynamic> json) {
|
||||||
//save token to secure storage
|
//save token to secure storage
|
||||||
@ -34,15 +58,16 @@ class Token {
|
|||||||
key: loginAccessTokenKey, value: json[loginAccessTokenKey] ?? '');
|
key: loginAccessTokenKey, value: json[loginAccessTokenKey] ?? '');
|
||||||
|
|
||||||
//create token object ?
|
//create token object ?
|
||||||
return Token(
|
return Token(json[loginAccessTokenKey] ?? '',
|
||||||
json[loginAccessTokenKey] ?? '', json[loginRefreshTokenKey] ?? '');
|
json[loginRefreshTokenKey] ?? '', '', 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, String> toJson() => {loginRefreshTokenKey: refreshToken};
|
Map<String, String> refreshTokenToJson() =>
|
||||||
|
{loginRefreshTokenKey: refreshToken};
|
||||||
|
|
||||||
Map<String, String> accessTokenToJson() => {loginAccessTokenKey: accessToken};
|
Map<String, String> accessTokenToJson() => {loginAccessTokenKey: accessToken};
|
||||||
|
|
||||||
Map<String, dynamic> decodeToken() {
|
static Map<String, dynamic> decodeToken(String accessToken) {
|
||||||
final parts = accessToken.split('.');
|
final parts = accessToken.split('.');
|
||||||
if (parts.length != 3) {
|
if (parts.length != 3) {
|
||||||
throw Exception('invalid access token');
|
throw Exception('invalid access token');
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import 'package:syncrow_app/features/auth/model/token.dart';
|
import 'package:syncrow_app/features/auth/model/token.dart';
|
||||||
|
|
||||||
class UserModel {
|
class UserModel {
|
||||||
|
static String userUuidKey = 'userUuid';
|
||||||
final String? uuid;
|
final String? uuid;
|
||||||
final String? email;
|
final String? email;
|
||||||
final String? name;
|
final String? name;
|
||||||
@ -36,13 +37,9 @@ class UserModel {
|
|||||||
|
|
||||||
//uuid to json
|
//uuid to json
|
||||||
|
|
||||||
Map<String, dynamic> uuIdAsJson() => {
|
|
||||||
'userUuid': uuid,
|
|
||||||
};
|
|
||||||
|
|
||||||
//from token
|
//from token
|
||||||
factory UserModel.fromToken(Token token) {
|
factory UserModel.fromToken(Token token) {
|
||||||
Map<String, dynamic> tempJson = token.decodeToken();
|
Map<String, dynamic> tempJson = Token.decodeToken(token.accessToken);
|
||||||
|
|
||||||
return UserModel(
|
return UserModel(
|
||||||
uuid: tempJson['uuid'].toString(),
|
uuid: tempJson['uuid'].toString(),
|
||||||
|
@ -1,46 +0,0 @@
|
|||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
||||||
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
|
|
||||||
import 'package:syncrow_app/features/shared_widgets/default_button.dart';
|
|
||||||
|
|
||||||
class LoginButton extends StatelessWidget {
|
|
||||||
const LoginButton({
|
|
||||||
super.key,
|
|
||||||
});
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return BlocBuilder<AuthCubit, AuthState>(
|
|
||||||
builder: (context, state) {
|
|
||||||
return Row(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
|
||||||
children: [
|
|
||||||
Expanded(
|
|
||||||
child: DefaultButton(
|
|
||||||
isDone: state is AuthSuccess,
|
|
||||||
isLoading: state is AuthLoading,
|
|
||||||
customButtonStyle: ButtonStyle(
|
|
||||||
backgroundColor: MaterialStateProperty.all(
|
|
||||||
Colors.black.withOpacity(.25),
|
|
||||||
),
|
|
||||||
foregroundColor: MaterialStateProperty.all(
|
|
||||||
Colors.white,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
child: const Text(
|
|
||||||
'Login',
|
|
||||||
),
|
|
||||||
onPressed: () {
|
|
||||||
if (AuthCubit.get(context).formKey.currentState!.validate()) {
|
|
||||||
AuthCubit.get(context).login();
|
|
||||||
FocusScope.of(context).unfocus();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +1,8 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
|
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
|
||||||
|
import 'package:syncrow_app/features/auth/view/widgets/login/forget_password.dart';
|
||||||
|
import 'package:syncrow_app/features/shared_widgets/default_button.dart';
|
||||||
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
|
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
|
||||||
import 'package:syncrow_app/utils/resource_manager/styles_manager.dart';
|
import 'package:syncrow_app/utils/resource_manager/styles_manager.dart';
|
||||||
|
|
||||||
@ -11,10 +13,11 @@ class LoginForm extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
var formKey = GlobalKey<FormState>();
|
||||||
return BlocBuilder<AuthCubit, AuthState>(
|
return BlocBuilder<AuthCubit, AuthState>(
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
return Form(
|
return Form(
|
||||||
key: AuthCubit.get(context).formKey,
|
key: formKey,
|
||||||
child: SingleChildScrollView(
|
child: SingleChildScrollView(
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
@ -40,7 +43,7 @@ class LoginForm extends StatelessWidget {
|
|||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
onTapOutside: (event) {
|
onTapOutside: (event) {
|
||||||
AuthCubit.get(context).formKey.currentState!.validate();
|
formKey.currentState!.validate();
|
||||||
},
|
},
|
||||||
decoration: defaultInputDecoration(context,
|
decoration: defaultInputDecoration(context,
|
||||||
hint: "Example@email.com"),
|
hint: "Example@email.com"),
|
||||||
@ -65,12 +68,44 @@ class LoginForm extends StatelessWidget {
|
|||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
onTapOutside: (event) {
|
onTapOutside: (event) {
|
||||||
AuthCubit.get(context).formKey.currentState!.validate();
|
formKey.currentState!.validate();
|
||||||
},
|
},
|
||||||
obscureText: !AuthCubit.get(context).isPasswordVisible,
|
obscureText: !AuthCubit.get(context).isPasswordVisible,
|
||||||
decoration: defaultInputDecoration(context,
|
decoration: defaultInputDecoration(context,
|
||||||
hint: "At least 8 characters"),
|
hint: "At least 8 characters"),
|
||||||
),
|
),
|
||||||
|
const SizedBox(height: 10),
|
||||||
|
// const LoginUserAgreement(),
|
||||||
|
const ForgetPassword(),
|
||||||
|
const SizedBox(height: 10),
|
||||||
|
Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: DefaultButton(
|
||||||
|
isDone: state is AuthSuccess,
|
||||||
|
isLoading: state is AuthLoading,
|
||||||
|
customButtonStyle: ButtonStyle(
|
||||||
|
backgroundColor: MaterialStateProperty.all(
|
||||||
|
Colors.black.withOpacity(.25),
|
||||||
|
),
|
||||||
|
foregroundColor: MaterialStateProperty.all(
|
||||||
|
Colors.white,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
child: const Text(
|
||||||
|
'Login',
|
||||||
|
),
|
||||||
|
onPressed: () {
|
||||||
|
if (formKey.currentState!.validate()) {
|
||||||
|
AuthCubit.get(context).login();
|
||||||
|
FocusScope.of(context).unfocus();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -3,8 +3,6 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
|||||||
import 'package:flutter_svg/flutter_svg.dart';
|
import 'package:flutter_svg/flutter_svg.dart';
|
||||||
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
|
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
|
||||||
import 'package:syncrow_app/features/auth/view/widgets/login/dont_have_an_account.dart';
|
import 'package:syncrow_app/features/auth/view/widgets/login/dont_have_an_account.dart';
|
||||||
import 'package:syncrow_app/features/auth/view/widgets/login/forget_password.dart';
|
|
||||||
import 'package:syncrow_app/features/auth/view/widgets/login/login_button.dart';
|
|
||||||
import 'package:syncrow_app/features/auth/view/widgets/login/login_divider.dart';
|
import 'package:syncrow_app/features/auth/view/widgets/login/login_divider.dart';
|
||||||
import 'package:syncrow_app/features/auth/view/widgets/login/login_form.dart';
|
import 'package:syncrow_app/features/auth/view/widgets/login/login_form.dart';
|
||||||
import 'package:syncrow_app/features/auth/view/widgets/login/login_with_google_facebook.dart';
|
import 'package:syncrow_app/features/auth/view/widgets/login/login_with_google_facebook.dart';
|
||||||
@ -94,11 +92,6 @@ class LoginView extends StatelessWidget {
|
|||||||
height: 20,
|
height: 20,
|
||||||
),
|
),
|
||||||
const LoginForm(),
|
const LoginForm(),
|
||||||
const SizedBox(height: 10),
|
|
||||||
// const LoginUserAgreement(),
|
|
||||||
const ForgetPassword(),
|
|
||||||
const SizedBox(height: 10),
|
|
||||||
const LoginButton(),
|
|
||||||
const LoginDivider(),
|
const LoginDivider(),
|
||||||
const LoginWithGoogleFacebook(),
|
const LoginWithGoogleFacebook(),
|
||||||
const DontHaveAnAccount(),
|
const DontHaveAnAccount(),
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import 'package:syncrow_app/features/devices/model/device_category_model.dart';
|
import 'package:syncrow_app/features/devices/model/device_category_model.dart';
|
||||||
|
|
||||||
class RoomModel {
|
class RoomModel {
|
||||||
final String id;
|
final int? id;
|
||||||
final String name;
|
final String? name;
|
||||||
|
|
||||||
final List<DevicesCategoryModel> categories;
|
final List<DevicesCategoryModel>? categories;
|
||||||
|
|
||||||
RoomModel({
|
RoomModel({
|
||||||
required this.id,
|
required this.id,
|
||||||
@ -22,8 +22,8 @@ class RoomModel {
|
|||||||
|
|
||||||
factory RoomModel.fromJson(Map<String, dynamic> json) {
|
factory RoomModel.fromJson(Map<String, dynamic> json) {
|
||||||
return RoomModel(
|
return RoomModel(
|
||||||
id: json['id'],
|
id: json['roomId'],
|
||||||
name: json['name'],
|
name: json['roomName'],
|
||||||
categories: json['devices'],
|
categories: json['devices'],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -19,12 +19,17 @@ class DevicesViewBody extends StatelessWidget {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocProvider(
|
return BlocProvider(
|
||||||
create: (context) => DevicesCubit(),
|
create: (context) => DevicesCubit(),
|
||||||
child: BlocBuilder<DevicesCubit, DevicesState>(
|
child: BlocBuilder<SpacesCubit, SpacesState>(
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
//TODO : move to NavigationCubit
|
return BlocBuilder<DevicesCubit, DevicesState>(
|
||||||
return state is DevicesLoading
|
builder: (context, state) {
|
||||||
? const Center(child: CircularProgressIndicator())
|
print(
|
||||||
: Padding(
|
"length : ${SpacesCubit.get(context).selectedSpace!.rooms!.length}");
|
||||||
|
//TODO : move to NavigationCubit
|
||||||
|
if (state is DevicesLoading) {
|
||||||
|
return const Center(child: CircularProgressIndicator());
|
||||||
|
} else {
|
||||||
|
return Padding(
|
||||||
padding: EdgeInsets.only(
|
padding: EdgeInsets.only(
|
||||||
top: Constants.appBarHeight,
|
top: Constants.appBarHeight,
|
||||||
bottom: Constants.bottomNavBarHeight,
|
bottom: Constants.bottomNavBarHeight,
|
||||||
@ -46,16 +51,21 @@ class DevicesViewBody extends StatelessWidget {
|
|||||||
},
|
},
|
||||||
children: [
|
children: [
|
||||||
const WizardPage(),
|
const WizardPage(),
|
||||||
...SpacesCubit.get(context)
|
if (SpacesCubit.get(context).selectedSpace != null)
|
||||||
.selectedSpace!
|
if (SpacesCubit.get(context)
|
||||||
.rooms!
|
.selectedSpace!
|
||||||
.map(
|
.rooms !=
|
||||||
(room) {
|
null)
|
||||||
return RoomPage(
|
...SpacesCubit.get(context)
|
||||||
room: room,
|
.selectedSpace!
|
||||||
);
|
.rooms!
|
||||||
},
|
.map(
|
||||||
)
|
(room) {
|
||||||
|
return RoomPage(
|
||||||
|
room: room,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -66,7 +76,11 @@ class DevicesViewBody extends StatelessWidget {
|
|||||||
child: SmoothPageIndicator(
|
child: SmoothPageIndicator(
|
||||||
controller:
|
controller:
|
||||||
SpacesCubit.get(context).devicesPageController,
|
SpacesCubit.get(context).devicesPageController,
|
||||||
count: 3,
|
count: SpacesCubit.get(context)
|
||||||
|
.selectedSpace!
|
||||||
|
.rooms!
|
||||||
|
.length +
|
||||||
|
1,
|
||||||
effect: const WormEffect(
|
effect: const WormEffect(
|
||||||
paintStyle: PaintingStyle.stroke,
|
paintStyle: PaintingStyle.stroke,
|
||||||
dotHeight: 8,
|
dotHeight: 8,
|
||||||
@ -77,6 +91,9 @@ class DevicesViewBody extends StatelessWidget {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -14,7 +14,7 @@ class RoomPage extends StatelessWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
List<DeviceModel> devices = [];
|
List<DeviceModel> devices = [];
|
||||||
for (var category in room.categories) {
|
for (var category in room.categories ?? []) {
|
||||||
devices.addAll(category.devices);
|
devices.addAll(category.devices);
|
||||||
}
|
}
|
||||||
return Padding(
|
return Padding(
|
||||||
|
@ -40,30 +40,33 @@ class RoomsSlider extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
...SpacesCubit.get(context).selectedSpace!.rooms!.map(
|
if (SpacesCubit.get(context).selectedSpace != null)
|
||||||
(room) => Padding(
|
if (SpacesCubit.get(context).selectedSpace!.rooms != null)
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 15),
|
...SpacesCubit.get(context).selectedSpace!.rooms!.map(
|
||||||
child: InkWell(
|
(room) => Padding(
|
||||||
onTap: () {
|
padding: const EdgeInsets.symmetric(horizontal: 15),
|
||||||
SpacesCubit.get(context).roomSliderPageChanged(
|
child: InkWell(
|
||||||
SpacesCubit.get(context)
|
onTap: () {
|
||||||
.selectedSpace!
|
SpacesCubit.get(context).roomSliderPageChanged(
|
||||||
.rooms!
|
SpacesCubit.get(context)
|
||||||
.indexOf(room));
|
.selectedSpace!
|
||||||
},
|
.rooms!
|
||||||
child: TitleMedium(
|
.indexOf(room));
|
||||||
text: room.name,
|
},
|
||||||
style: context.titleMedium.copyWith(
|
child: TitleMedium(
|
||||||
fontSize: 25,
|
text: room.name!,
|
||||||
color: SpacesCubit.get(context).selectedRoom == room
|
style: context.titleMedium.copyWith(
|
||||||
? ColorsManager.textPrimaryColor
|
fontSize: 25,
|
||||||
: ColorsManager.textPrimaryColor
|
color: SpacesCubit.get(context).selectedRoom ==
|
||||||
.withOpacity(.2),
|
room
|
||||||
|
? ColorsManager.textPrimaryColor
|
||||||
|
: ColorsManager.textPrimaryColor
|
||||||
|
.withOpacity(.2),
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
)
|
||||||
),
|
|
||||||
)
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -10,50 +10,77 @@ class SplashView extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
//TODO remove this delay
|
return FutureBuilder(
|
||||||
Future.value().then((value) async {
|
future: getTokenAndValidate(context),
|
||||||
var isLoggedIn = await const FlutterSecureStorage()
|
builder: (context, snapshot) {
|
||||||
.read(key: Token.loginAccessTokenKey) !=
|
if (snapshot.hasData) {
|
||||||
null;
|
if (snapshot.data ?? false) {
|
||||||
if (isLoggedIn) {
|
Future.delayed(const Duration(seconds: 1), () {
|
||||||
Navigator.pushReplacementNamed(context, Routes.homeRoute);
|
Navigator.pushReplacementNamed(context, Routes.homeRoute);
|
||||||
} else {
|
});
|
||||||
Navigator.pushReplacementNamed(context, Routes.authLogin);
|
} else {
|
||||||
}
|
Future.delayed(const Duration(seconds: 1), () {
|
||||||
});
|
Navigator.pushReplacementNamed(context, Routes.authLogin);
|
||||||
return Scaffold(
|
});
|
||||||
body: Stack(
|
}
|
||||||
alignment: Alignment.center,
|
}
|
||||||
children: [
|
return Scaffold(
|
||||||
Container(
|
body: Stack(
|
||||||
width: MediaQuery.sizeOf(context).width,
|
alignment: Alignment.center,
|
||||||
height: MediaQuery.sizeOf(context).height,
|
children: [
|
||||||
decoration: const BoxDecoration(
|
Container(
|
||||||
image: DecorationImage(
|
width: MediaQuery.sizeOf(context).width,
|
||||||
image: AssetImage(
|
height: MediaQuery.sizeOf(context).height,
|
||||||
Assets.imagesBackground,
|
decoration: const BoxDecoration(
|
||||||
|
image: DecorationImage(
|
||||||
|
image: AssetImage(
|
||||||
|
Assets.imagesBackground,
|
||||||
|
),
|
||||||
|
fit: BoxFit.cover,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
fit: BoxFit.cover,
|
|
||||||
),
|
),
|
||||||
),
|
Container(
|
||||||
),
|
width: MediaQuery.sizeOf(context).width,
|
||||||
Container(
|
height: MediaQuery.sizeOf(context).height,
|
||||||
width: MediaQuery.sizeOf(context).width,
|
decoration: const BoxDecoration(
|
||||||
height: MediaQuery.sizeOf(context).height,
|
image: DecorationImage(
|
||||||
decoration: const BoxDecoration(
|
image: AssetImage(Assets.imagesVector),
|
||||||
image: DecorationImage(
|
fit: BoxFit.cover,
|
||||||
image: AssetImage(Assets.imagesVector),
|
opacity: 0.9,
|
||||||
fit: BoxFit.cover,
|
),
|
||||||
opacity: 0.9,
|
),
|
||||||
),
|
),
|
||||||
),
|
SvgPicture.asset(
|
||||||
|
Assets.imagesLogo,
|
||||||
|
width: 240,
|
||||||
|
)
|
||||||
|
],
|
||||||
),
|
),
|
||||||
SvgPicture.asset(
|
);
|
||||||
Assets.imagesLogo,
|
},
|
||||||
width: 240,
|
|
||||||
)
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<bool> getTokenAndValidate(BuildContext context) async {
|
||||||
|
return await const FlutterSecureStorage()
|
||||||
|
.read(key: Token.loginAccessTokenKey)
|
||||||
|
.then((value) {
|
||||||
|
if (value == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
print("Decoding token Started");
|
||||||
|
var tokenData = Token.decodeToken(value ?? "");
|
||||||
|
print("checking token data");
|
||||||
|
|
||||||
|
if (tokenData.containsKey('exp')) {
|
||||||
|
var exp = tokenData['exp'] ?? 0;
|
||||||
|
var currentTime = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||||
|
print('time: $currentTime exp: $exp');
|
||||||
|
return currentTime < exp;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,12 @@ class Assets {
|
|||||||
static const String iconsDevices = 'assets/icons/Devices.svg';
|
static const String iconsDevices = 'assets/icons/Devices.svg';
|
||||||
static const String iconsDevicesFill = 'assets/icons/Devices-fill.svg';
|
static const String iconsDevicesFill = 'assets/icons/Devices-fill.svg';
|
||||||
static const String iconsDoorLock = 'assets/icons/doorLock.svg';
|
static const String iconsDoorLock = 'assets/icons/doorLock.svg';
|
||||||
|
static const String iconsDoorLockLinkage = 'assets/icons/DoorLockLinkage.svg';
|
||||||
|
static const String iconsDoorLockLock = 'assets/icons/DoorLockLock.svg';
|
||||||
|
static const String iconsDoorLockMembers = 'assets/icons/DoorLockMembers.svg';
|
||||||
|
static const String iconsDoorLockPassword =
|
||||||
|
'assets/icons/DoorLockPassword.svg';
|
||||||
|
static const String iconsDoorLockRecords = 'assets/icons/DoorLockRecords.svg';
|
||||||
static const String iconsFacebook = 'assets/icons/Facebook.svg';
|
static const String iconsFacebook = 'assets/icons/Facebook.svg';
|
||||||
static const String iconsFan0 = 'assets/icons/fan-0.svg';
|
static const String iconsFan0 = 'assets/icons/fan-0.svg';
|
||||||
static const String iconsFan1 = 'assets/icons/fan-1.svg';
|
static const String iconsFan1 = 'assets/icons/fan-1.svg';
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:syncrow_app/features/app_layout/bloc/nav_cubit.dart';
|
import 'package:syncrow_app/features/app_layout/bloc/nav_cubit.dart';
|
||||||
import 'package:syncrow_app/features/app_layout/bloc/spaces_cubit.dart';
|
|
||||||
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
|
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
|
||||||
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
||||||
import 'package:syncrow_app/utils/resource_manager/constants.dart';
|
import 'package:syncrow_app/utils/resource_manager/constants.dart';
|
||||||
@ -30,9 +29,6 @@ class MyApp extends StatelessWidget {
|
|||||||
BlocProvider(
|
BlocProvider(
|
||||||
create: (context) => NavCubit(),
|
create: (context) => NavCubit(),
|
||||||
),
|
),
|
||||||
BlocProvider(
|
|
||||||
create: (context) => SpacesCubit(),
|
|
||||||
),
|
|
||||||
BlocProvider(
|
BlocProvider(
|
||||||
create: (context) => DevicesCubit(),
|
create: (context) => DevicesCubit(),
|
||||||
),
|
),
|
||||||
|
@ -12,4 +12,5 @@ abstract class ApiEndpoints {
|
|||||||
|
|
||||||
// Spaces
|
// Spaces
|
||||||
static const String spaces = '$baseUrl/home';
|
static const String spaces = '$baseUrl/home';
|
||||||
|
static const String rooms = '$baseUrl/room';
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import 'package:flutter/cupertino.dart';
|
|
||||||
import 'package:syncrow_app/features/auth/model/login_with_email_model.dart';
|
import 'package:syncrow_app/features/auth/model/login_with_email_model.dart';
|
||||||
import 'package:syncrow_app/features/auth/model/token.dart';
|
import 'package:syncrow_app/features/auth/model/token.dart';
|
||||||
import 'package:syncrow_app/features/auth/model/verify_code.dart';
|
import 'package:syncrow_app/features/auth/model/verify_code.dart';
|
||||||
@ -11,21 +10,22 @@ class AuthenticationAPI {
|
|||||||
path: ApiEndpoints.verifyOtp,
|
path: ApiEndpoints.verifyOtp,
|
||||||
body: data.toJson(),
|
body: data.toJson(),
|
||||||
showServerMessage: false,
|
showServerMessage: false,
|
||||||
expectedResponseModel: (json) {
|
expectedResponseModel: (json) => Token.fromJson(json));
|
||||||
Token token = Token.fromJson(json);
|
|
||||||
return token;
|
|
||||||
});
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<Token> loginWithEmail(
|
static Future<Token> loginWithEmail(
|
||||||
{required LoginWithEmailModel model}) async {
|
{required LoginWithEmailModel model}) async {
|
||||||
final response = await HTTPService().post(
|
try {
|
||||||
path: ApiEndpoints.login,
|
final response = await HTTPService().post(
|
||||||
body: model.toJson(),
|
path: ApiEndpoints.login,
|
||||||
showServerMessage: false,
|
body: model.toJson(),
|
||||||
expectedResponseModel: (json) => Token.fromJson(json['data']));
|
showServerMessage: false,
|
||||||
debugPrint("response: $response");
|
expectedResponseModel: (json) => Token.fromJson(json['data']));
|
||||||
return response;
|
// debugPrint("response: $response");
|
||||||
|
return response;
|
||||||
|
} catch (e) {
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||||
|
import 'package:syncrow_app/features/auth/model/token.dart';
|
||||||
|
|
||||||
class HTTPInterceptor extends InterceptorsWrapper {
|
class HTTPInterceptor extends InterceptorsWrapper {
|
||||||
// @override
|
// @override
|
||||||
@ -11,9 +12,9 @@ class HTTPInterceptor extends InterceptorsWrapper {
|
|||||||
@override
|
@override
|
||||||
void onRequest(
|
void onRequest(
|
||||||
RequestOptions options, RequestInterceptorHandler handler) async {
|
RequestOptions options, RequestInterceptorHandler handler) async {
|
||||||
//pass the token from the flutter secure storage to the request header
|
var storage = FlutterSecureStorage();
|
||||||
|
var token = await storage.read(key: Token.loginAccessTokenKey);
|
||||||
options.headers['Authorization'] = 'Bearer ${AuthCubit.token.accessToken}';
|
options.headers['Authorization'] = 'Bearer $token';
|
||||||
|
|
||||||
super.onRequest(options, handler);
|
super.onRequest(options, handler);
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,8 @@ class HTTPService {
|
|||||||
baseUrl: ApiEndpoints.baseUrl,
|
baseUrl: ApiEndpoints.baseUrl,
|
||||||
receiveDataWhenStatusError: true,
|
receiveDataWhenStatusError: true,
|
||||||
followRedirects: false,
|
followRedirects: false,
|
||||||
connectTimeout: const Duration(seconds: 5),
|
connectTimeout: const Duration(seconds: 30),
|
||||||
receiveTimeout: const Duration(seconds: 5),
|
receiveTimeout: const Duration(seconds: 30),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -62,8 +62,8 @@ class HTTPService {
|
|||||||
queryParameters: queryParameters,
|
queryParameters: queryParameters,
|
||||||
options: options,
|
options: options,
|
||||||
);
|
);
|
||||||
debugPrint("status code is ${response.statusCode}");
|
// debugPrint("status code is ${response.statusCode}");
|
||||||
debugPrint("response data is ${response.data}");
|
// debugPrint("response data is ${response.data}");
|
||||||
return expectedResponseModel(response.data);
|
return expectedResponseModel(response.data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
rethrow;
|
rethrow;
|
||||||
|
@ -1,26 +1,19 @@
|
|||||||
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||||
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
|
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
|
||||||
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
|
import 'package:syncrow_app/features/auth/model/user_model.dart';
|
||||||
|
import 'package:syncrow_app/features/devices/model/room_model.dart';
|
||||||
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
|
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
|
||||||
import 'package:syncrow_app/services/api/http_service.dart';
|
import 'package:syncrow_app/services/api/http_service.dart';
|
||||||
|
|
||||||
class SpacesAPI {
|
class SpacesAPI {
|
||||||
// static Future<Token> loginWithEmail(
|
|
||||||
// {required LoginWithEmailModel model}) async {
|
|
||||||
// final response = await HTTPService().post(
|
|
||||||
// path: ApiEndpoints.login,
|
|
||||||
// body: model.toJson(),
|
|
||||||
// showServerMessage: false,
|
|
||||||
// expectedResponseModel: (json) {
|
|
||||||
// Token token = Token.fromJson(json['data']);
|
|
||||||
// return token;
|
|
||||||
// });
|
|
||||||
// debugPrint("response: $response");
|
|
||||||
// return response;
|
|
||||||
// }
|
|
||||||
|
|
||||||
static Future<List<SpaceModel>> getSpaces() async {
|
static Future<List<SpaceModel>> getSpaces() async {
|
||||||
|
var uuid =
|
||||||
|
await const FlutterSecureStorage().read(key: UserModel.userUuidKey);
|
||||||
final response = await HTTPService().get(
|
final response = await HTTPService().get(
|
||||||
path: "${ApiEndpoints.spaces}/${AuthCubit.user!.uuid}",
|
path: ApiEndpoints.spaces,
|
||||||
|
queryParameters: {
|
||||||
|
"userUuid": uuid,
|
||||||
|
},
|
||||||
showServerMessage: false,
|
showServerMessage: false,
|
||||||
expectedResponseModel: (json) {
|
expectedResponseModel: (json) {
|
||||||
List<SpaceModel> spaces = [];
|
List<SpaceModel> spaces = [];
|
||||||
@ -32,4 +25,21 @@ class SpacesAPI {
|
|||||||
);
|
);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//get rooms by space id
|
||||||
|
static Future<List<RoomModel>> getRooms(int spaceId) async {
|
||||||
|
final response = await HTTPService().get(
|
||||||
|
path: ApiEndpoints.rooms,
|
||||||
|
queryParameters: {"homeId": spaceId},
|
||||||
|
showServerMessage: false,
|
||||||
|
expectedResponseModel: (json) {
|
||||||
|
List<RoomModel> rooms = [];
|
||||||
|
for (var room in json) {
|
||||||
|
rooms.add(RoomModel.fromJson(room));
|
||||||
|
}
|
||||||
|
return rooms;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user