Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
refactor: migrate to isar v4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammedKpln committed Sep 21, 2023
1 parent c83fbb4 commit 44d14b8
Show file tree
Hide file tree
Showing 29 changed files with 603 additions and 593 deletions.
6 changes: 6 additions & 0 deletions lib/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

analyzer:
enable-experiment:
- records
- patterns
- sealed-class

linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
Expand Down
32 changes: 15 additions & 17 deletions lib/lib/core/auth/controllers/auth.controller.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import 'dart:async';

import 'package:barbox/core/services/di.service.dart';
import 'package:barbox/features/home/repositories/account.repository.dart';
import 'package:barbox/utils.dart';
import 'package:injectable/injectable.dart';
import 'package:mobx/mobx.dart';
import 'package:barbox/core/storage/account.storage.dart';
import 'package:barbox/core/storage/isar/local_account.db.dart';
import 'package:barbox/core/storage/messages.storage.dart';
import 'package:barbox/features/home/repositories/account.repository.dart';
import 'package:barbox/types/account.dart';
import 'package:barbox/utils.dart';
import 'package:injectable/injectable.dart';
import 'package:mobx/mobx.dart';

part 'auth.controller.g.dart';

enum AuthState { loggedIn, none }
Expand Down Expand Up @@ -71,36 +72,33 @@ abstract class _AuthControllerBase with Store {
accountId: acc.id,
);

print(token);

await _accountStorage.saveAccount(account.value!);
availableAccounts.add(account.value!);
authState.value = AuthState.loggedIn;
}

@action
Future<void> switchAccount(int id) async {
final toAccount = await _accountStorage.getAccount(id: id);
Future<void> switchAccount(String accountId) async {
final toAccount = await _accountStorage.getAccount(accountId: accountId);
account.value = toAccount;
}

Future<bool> register(
{required String username,
required String selectedDomain,
required String password}) async {
try {
final _accountRepository = getIt<AccountRepository>();
final address = "$username@$selectedDomain";
final _accountRepository = getIt<AccountRepository>();
final address = "$username@$selectedDomain";

final _account =
await _accountRepository.createAccount(address, password);
final _account = await _accountRepository.createAccount(address, password);

final token = await _accountRepository.login(_account.address, password);
final token = await _accountRepository.login(_account.address, password);

await login(acc: _account, password: password, token: token.token);
await login(acc: _account, password: password, token: token.token);

return true;
} catch (e) {
rethrow;
}
return true;
}

Future<void> registerWithRandomUsername() async {
Expand Down
12 changes: 6 additions & 6 deletions lib/lib/core/shared/toast/views/toast.component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ class ToastComponent extends StatelessWidget {
return closeDialog();
}

Widget renderPrimaryButton() {
PushButton renderPrimaryButton() {
if (action != null) {
return PushButton(
child: Text(action!.label),
buttonSize: ButtonSize.large,
controlSize: ControlSize.large,
onPressed: onPressed,
);
}

return PushButton(
buttonSize: ButtonSize.large,
controlSize: ControlSize.large,
child: const Text("Close"),
secondary: true,
onPressed: onPressed,
isSecondary: true,
);
}

Expand All @@ -61,10 +61,10 @@ class ToastComponent extends StatelessWidget {
primaryButton: renderPrimaryButton(),
secondaryButton: action != null
? PushButton(
buttonSize: ButtonSize.large,
controlSize: ControlSize.large,
child: const Text("Close"),
onPressed: closeDialog,
isSecondary: true,
secondary: true,
)
: null,
);
Expand Down
23 changes: 13 additions & 10 deletions lib/lib/core/storage/account.storage.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
import 'package:injectable/injectable.dart';
import 'package:barbox/core/storage/isar/base.db.dart';
import 'package:barbox/core/storage/isar/local_account.db.dart';
import 'package:injectable/injectable.dart';
import 'package:isar/isar.dart';

@LazySingleton()
class AccountStorage {
Future<void> saveAccount(LocalAccount account) async {
isarInstance.writeTxn(() async {
await isarInstance.localAccounts.put(account);
isarInstance.writeAsync((isar) async {
isar.localAccounts.put(account);
});
}

Future<void> removeAccount(LocalAccount account) async {
isarInstance.writeTxn(() async {
await isarInstance.localAccounts.delete(account.id);
isarInstance.writeAsync((isar) async {
isar.localAccounts.delete(account.id);
});
}

Future<void> removeAllAccounts() async {
isarInstance.writeTxn(() async {
await isarInstance.localAccounts.clear();
isarInstance.writeAsync((isar) async {
isar.localAccounts.clear();
});
}

Future<LocalAccount?> getAccount({int? id}) async {
return isarInstance.localAccounts.get(id ?? 1);
Future<LocalAccount?> getAccount({required String accountId}) async {
return isarInstance.localAccounts
.where()
.accountIdEqualTo(accountId)
.findFirstAsync();
}

Future<List<LocalAccount>> getAllAvailableAccounts() async {
return isarInstance.localAccounts.where().findAll();
return isarInstance.localAccounts.where().findAllAsync();
}

Future<bool> isLoggedIn() async {
Expand Down
11 changes: 9 additions & 2 deletions lib/lib/core/storage/isar/base.db.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import 'package:isar/isar.dart';
import 'package:path_provider/path_provider.dart';

late Isar isarInstance;

Future<void> initDb(List<CollectionSchema<dynamic>> schemas) async {
isarInstance = await Isar.open(schemas);
Future<void> initDb(List<IsarGeneratedSchema> schemas) async {
final path = await getApplicationDocumentsDirectory();
print(path);
isarInstance = await Isar.openAsync(
schemas: schemas,
directory: path.path,
inspector: true,
);
}
18 changes: 12 additions & 6 deletions lib/lib/core/storage/isar/local_account.db.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import 'dart:math';

import 'package:isar/isar.dart';

part 'local_account.db.g.dart';

@collection
class LocalAccount {
LocalAccount({this.address, this.password, this.token, this.accountId});
LocalAccount(
{required this.address,
required this.password,
required this.token,
required this.accountId});

Id id = Isar.autoIncrement;
int get id => Random().nextInt(999999);

String? address;
final String address;

String? password;
final String password;

String? token;
final String token;

String? accountId;
final String accountId;
}
37 changes: 17 additions & 20 deletions lib/lib/core/storage/messages.storage.dart
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import 'package:barbox/core/storage/isar/base.db.dart';
import 'package:barbox/core/storage/isar/local_account.db.dart';
import 'package:barbox/types/messages/message.dart';
import 'package:injectable/injectable.dart';
import 'package:isar/isar.dart';
import 'package:barbox/core/storage/isar/base.db.dart';
import 'package:barbox/types/messages/message.dart';

@LazySingleton()
class MessagesStorage {
Future<void> saveMessage(Message message) async {
await isarInstance.writeTxn(() async {
await isarInstance.messages.put(message);
await isarInstance.writeAsync((isar) async {
isar.messages.put(message);
});
}

Future<void> saveMessages(List<Message> message) async {
await isarInstance.writeTxn(() async {
await isarInstance.messages.putAll(message);
await isarInstance.writeAsync((isar) async {
isar.messages.putAll(message);
});
}

//Isar's put() method will either insert or update the object depending on
//whether it already exists in the collection.
Future<void> updateMessageSeen(String msgId, bool seen) async {
await isarInstance.writeTxn(() async {
Message? message = await isarInstance.messages.getById(msgId);
await isarInstance.writeAsync((isar) async {
Message? message =
await isar.messages.where().idEqualTo(msgId).findFirstAsync();

if (message != null) {
message = message.copyWith(seen: seen);
await isarInstance.messages.putById(message);
isar.messages.put(message);
}
});
}

Future<bool> containsMessage(Message message) async {
final entries =
await isarInstance.messages.where().idEqualTo(message.id).count();
final entries = isarInstance.messages.where().idEqualTo(message.id).count();

if (entries > 0) {
return true;
Expand All @@ -47,23 +47,20 @@ class MessagesStorage {
}

Future<void> deleteMessage(int isarId) async {
return isarInstance.writeTxn(() async {
await isarInstance.messages.delete(isarId);
return isarInstance.writeAsync((isar) async {
isar.messages.delete(isarId);
});
}

Future<void> clear() async {
return isarInstance.writeTxn(() async {
await isarInstance.messages.clear();
return isarInstance.writeAsync((isar) async {
isar.messages.clear();
});
}

Future<void> clearMessagesByAddress(LocalAccount account) async {
return isarInstance.writeTxn(() async {
await isarInstance.messages
.where()
.accountIdEqualTo(account.accountId!)
.deleteAll();
return isarInstance.writeAsync((isar) async {
isar.messages.where().accountIdEqualTo(account.accountId).deleteAll();
});
}
}
14 changes: 7 additions & 7 deletions lib/lib/features/app/controller/app.controller.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import 'dart:async';

import 'package:barbox/core/auth/controllers/auth.controller.dart';
import 'package:barbox/core/services/notification.service.dart';
import 'package:barbox/core/services/router/router.service.dart';
import 'package:barbox/core/shared/toast/views/controllers/toast.controller.dart';
import 'package:barbox/core/storage/account.storage.dart';
import 'package:barbox/features/app/views/components/sidebarItem.component.dart';
import 'package:beamer/beamer.dart';
import 'package:flutter/cupertino.dart';
import 'package:injectable/injectable.dart';
import 'package:macos_ui/macos_ui.dart';
import 'package:mobx/mobx.dart';
import 'package:barbox/core/auth/controllers/auth.controller.dart';
import 'package:barbox/core/services/notification.service.dart';
import 'package:barbox/features/app/views/components/sidebarItem.component.dart';
import 'package:barbox/core/services/router/router.service.dart';
import 'package:barbox/core/storage/account.storage.dart';

part 'app.controller.g.dart';

Expand Down Expand Up @@ -58,11 +58,11 @@ abstract class _AppViewControllerBase with Store {
return authController.availableAccounts
.where((element) => element.id != authController.account.value?.id)
.map((e) => MacosListTile(
title: Text(e.address ?? "s",
title: Text(e.address,
style: const TextStyle(fontWeight: FontWeight.normal)),
leading: const MacosIcon(CupertinoIcons.person_circle),
mouseCursor: SystemMouseCursors.click,
onClick: () => authController.switchAccount(e.id),
onClick: () => authController.switchAccount(e.accountId),
))
.toList();
}
Expand Down
27 changes: 13 additions & 14 deletions lib/lib/features/app/controller/new_account.controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,15 @@ abstract class _NewAccountControllerBase with Store {
return;
}

try {
await _authController.register(
username: username!,
selectedDomain: selectedDomain!,
password: password!);

_toastService.showToast("Account created successfully",
toastType: ToastType.success);

Navigator.of(context).pop();
} catch (e) {
_toastService.showToast(e.toString(), toastType: ToastType.error);
}
await _authController.register(
username: username!,
selectedDomain: selectedDomain!,
password: password!);

_toastService.showToast("Account created successfully",
toastType: ToastType.success);

Navigator.of(context).pop();
}

@action
Expand Down Expand Up @@ -129,7 +125,10 @@ abstract class _NewAccountControllerBase with Store {
selectedDomain = value;
}

void dispose() {}
void dispose() {
controllerPassword.dispose();
controllerUsername.dispose();
}
}

disposeNewAccountController(NewAccountController instance) {
Expand Down
6 changes: 3 additions & 3 deletions lib/lib/features/app/views/app.view.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:barbox/core/services/di.service.dart';
import 'package:barbox/core/services/router/router.service.dart';
import 'package:barbox/features/app/controller/app.controller.dart';
import 'package:barbox/features/app/views/components/new_account.component.dart';
import 'package:barbox/main.dart';
import 'package:beamer/beamer.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:macos_ui/macos_ui.dart';
import 'package:barbox/core/services/router/router.service.dart';
import 'package:barbox/features/app/controller/app.controller.dart';
import 'package:barbox/core/services/di.service.dart';

class App extends StatefulWidget {
const App({super.key});
Expand Down
Loading

0 comments on commit 44d14b8

Please sign in to comment.