mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 14:47:23 +00:00
169 lines
5.1 KiB
Dart
169 lines
5.1 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:graphview/GraphView.dart';
|
|
import 'package:syncrow_web/pages/auth/model/user_model.dart';
|
|
import 'package:syncrow_web/pages/home/bloc/home_event.dart';
|
|
import 'package:syncrow_web/pages/home/bloc/home_state.dart';
|
|
import 'package:syncrow_web/pages/home/home_model/home_item_model.dart';
|
|
import 'package:syncrow_web/pages/routiens/bloc/routine_bloc/routine_bloc.dart';
|
|
import 'package:syncrow_web/services/home_api.dart';
|
|
import 'package:syncrow_web/utils/color_manager.dart';
|
|
import 'package:syncrow_web/utils/constants/assets.dart';
|
|
import 'package:syncrow_web/utils/constants/routes_const.dart';
|
|
|
|
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
|
final Graph graph = Graph()..isTree = true;
|
|
final BuchheimWalkerConfiguration builder = BuchheimWalkerConfiguration();
|
|
List<Node> sourcesList = [];
|
|
List<Node> destinationsList = [];
|
|
UserModel? user;
|
|
String terms = '';
|
|
String policy = '';
|
|
|
|
HomeBloc() : super((HomeInitial())) {
|
|
on<CreateNewNode>(_createNode);
|
|
on<FetchUserInfo>(_fetchUserInfo);
|
|
on<FetchTermEvent>(_fetchTerms);
|
|
on<FetchPolicyEvent>(_fetchPolicy);
|
|
on<ConfirmUserAgreementEvent>(_confirmUserAgreement);
|
|
}
|
|
|
|
void _createNode(CreateNewNode event, Emitter<HomeState> emit) async {
|
|
emit(HomeInitial());
|
|
sourcesList.add(event.sourceNode);
|
|
destinationsList.add(event.destinationNode);
|
|
for (int i = 0; i < sourcesList.length; i++) {
|
|
graph.addEdge(sourcesList[i], destinationsList[i]);
|
|
}
|
|
|
|
builder
|
|
..siblingSeparation = (100)
|
|
..levelSeparation = (150)
|
|
..subtreeSeparation = (150)
|
|
..orientation = (BuchheimWalkerConfiguration.ORIENTATION_TOP_BOTTOM);
|
|
emit(HomeUpdateTree(graph: graph, builder: builder));
|
|
}
|
|
|
|
Future _fetchUserInfo(FetchUserInfo event, Emitter<HomeState> emit) async {
|
|
try {
|
|
var uuid =
|
|
await const FlutterSecureStorage().read(key: UserModel.userUuidKey);
|
|
user = await HomeApi().fetchUserInfo(uuid);
|
|
add(FetchTermEvent());
|
|
emit(HomeInitial());
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
Future _fetchTerms(FetchTermEvent event, Emitter<HomeState> emit) async {
|
|
try {
|
|
emit(LoadingHome());
|
|
terms = await HomeApi().fetchTerms();
|
|
add(FetchPolicyEvent());
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
Future _fetchPolicy(FetchPolicyEvent event, Emitter<HomeState> emit) async {
|
|
try {
|
|
emit(LoadingHome());
|
|
policy = await HomeApi().fetchPolicy();
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
Future _confirmUserAgreement(
|
|
ConfirmUserAgreementEvent event, Emitter<HomeState> emit) async {
|
|
try {
|
|
emit(LoadingHome());
|
|
var uuid =
|
|
await const FlutterSecureStorage().read(key: UserModel.userUuidKey);
|
|
policy = await HomeApi().confirmUserAgreements(uuid);
|
|
emit(PolicyAgreement());
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
// static Future fetchUserInfo() async {
|
|
// try {
|
|
// var uuid =
|
|
// await const FlutterSecureStorage().read(key: UserModel.userUuidKey);
|
|
// user = await HomeApi().fetchUserInfo(uuid);
|
|
// } catch (e) {
|
|
// return;
|
|
// }
|
|
// }
|
|
|
|
List<HomeItemModel> homeItems = [
|
|
HomeItemModel(
|
|
title: 'Access',
|
|
icon: Assets.accessIcon,
|
|
active: true,
|
|
onPress: (context) {
|
|
context.go(RoutesConst.accessManagementPage);
|
|
},
|
|
color: null,
|
|
),
|
|
HomeItemModel(
|
|
title: 'Space Management',
|
|
icon: Assets.spaseManagementIcon,
|
|
active: true,
|
|
onPress: (context) {
|
|
context.go(RoutesConst.spacesManagementPage);
|
|
},
|
|
color: ColorsManager.primaryColor,
|
|
),
|
|
HomeItemModel(
|
|
title: 'Devices',
|
|
icon: Assets.devicesIcon,
|
|
active: true,
|
|
onPress: (context) {
|
|
BlocProvider.of<RoutineBloc>(context)
|
|
.add(const TriggerSwitchTabsEvent(isRoutineTab: false));
|
|
context.go(RoutesConst.deviceManagementPage);
|
|
},
|
|
color: ColorsManager.primaryColor,
|
|
),
|
|
// HomeItemModel(
|
|
// title: 'Move in',
|
|
// icon: Assets.moveinIcon,
|
|
// active: false,
|
|
// onPress: (context) {},
|
|
// color: ColorsManager.primaryColor,
|
|
// ),
|
|
// HomeItemModel(
|
|
// title: 'Construction',
|
|
// icon: Assets.constructionIcon,
|
|
// active: false,
|
|
// onPress: (context) {},
|
|
// color: ColorsManager.primaryColor,
|
|
// ),
|
|
// HomeItemModel(
|
|
// title: 'Energy',
|
|
// icon: Assets.energyIcon,
|
|
// active: false,
|
|
// onPress: (context) {},
|
|
// color: ColorsManager.slidingBlueColor.withOpacity(0.2),
|
|
// ),
|
|
// HomeItemModel(
|
|
// title: 'Integrations',
|
|
// icon: Assets.integrationsIcon,
|
|
// active: false,
|
|
// onPress: (context) {},
|
|
// color: ColorsManager.slidingBlueColor.withOpacity(0.2),
|
|
// ),
|
|
// HomeItemModel(
|
|
// title: 'Asset',
|
|
// icon: Assets.assetIcon,
|
|
// active: false,
|
|
// onPress: (context) {},
|
|
// color: ColorsManager.slidingBlueColor.withOpacity(0.2),
|
|
// ),
|
|
];
|
|
}
|