selfprivacy.org.app/lib/logic/cubit/users/users_cubit.dart

55 lines
1.8 KiB
Dart
Raw Normal View History

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));
}
}
2021-01-06 17:35:57 +00:00
// final initMockUsers = <User>[
// User(login: 'Heartbreaking.Goose', password: genPass()),
// User(login: 'Alma.lawson', password: genPass()),
// User(login: 'Bee.gees', password: genPass()),
// User(login: 'Bim.jennings', password: genPass()),
// User(login: 'Debra.holt', password: genPass()),
// User(login: 'Georgia.young', password: genPass()),
// User(login: 'Kenzi.lawson', password: genPass()),
// User(login: 'Le.jennings', password: genPass()),
// User(login: 'Kirill.Zh', password: genPass()),
// User(login: 'Tina.Bolton', password: genPass()),
// User(login: 'Rebekah.Lynn', password: genPass()),
// User(login: 'Aleena.Armstrong', password: genPass()),
// User(login: 'Rosemary.Williams', password: genPass()),
// User(login: 'Sullivan.Nixon', password: genPass()),
// User(login: 'Aleena.Armstrong', password: genPass()),
// ];