2020-12-03 16:52:53 +00:00
|
|
|
import 'package:bloc/bloc.dart';
|
|
|
|
import 'package:equatable/equatable.dart';
|
2021-07-29 05:24:42 +00:00
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:selfprivacy/config/hive_config.dart';
|
2020-12-03 16:52:53 +00:00
|
|
|
import 'package:selfprivacy/logic/models/user.dart';
|
|
|
|
export 'package:provider/provider.dart';
|
|
|
|
|
|
|
|
part 'users_state.dart';
|
|
|
|
|
|
|
|
class UsersCubit extends Cubit<UsersState> {
|
2021-07-29 05:24:42 +00:00
|
|
|
UsersCubit() : super(UsersState(<User>[]));
|
|
|
|
Box<User> box = Hive.box<User>(BNames.users);
|
2020-12-03 16:52:53 +00:00
|
|
|
|
2021-07-29 05:24:42 +00:00
|
|
|
void load() async {
|
|
|
|
var loadedUsers = box.values.toList();
|
|
|
|
if (loadedUsers.isNotEmpty) {
|
|
|
|
emit(UsersState(loadedUsers));
|
|
|
|
}
|
|
|
|
}
|
2020-12-03 16:52:53 +00:00
|
|
|
|
2021-07-29 05:24:42 +00:00
|
|
|
void addUsers(List<User> users) async {
|
|
|
|
var newUserList = <User>[...state.users, ...users];
|
|
|
|
|
|
|
|
await box.addAll(users);
|
|
|
|
emit(UsersState(newUserList));
|
2020-12-03 16:52:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 05:24:42 +00:00
|
|
|
void remove(User user) async {
|
2021-01-14 18:45:10 +00:00
|
|
|
var users = [...state.users];
|
2021-07-29 05:24:42 +00:00
|
|
|
var index = users.indexOf(user);
|
2020-12-03 16:52:53 +00:00
|
|
|
users.remove(user);
|
2021-07-29 05:24:42 +00:00
|
|
|
await box.deleteAt(index);
|
2020-12-03 16:52:53 +00:00
|
|
|
|
|
|
|
emit(UsersState(users));
|
|
|
|
}
|
|
|
|
}
|