Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Greencode module #366

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions lib/greencode/class/completion.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'package:myecl/user/class/user.dart';

class Completion {
late final String id;
late final int discoveredCount;
late final int totalCount;
late final User user;

Completion({
required this.id,
required this.discoveredCount,
required this.totalCount,
required this.user,
});

Completion.fromJson(Map<String, dynamic> json) {
id = json["id"];
discoveredCount = json["discovered_count"];
totalCount = json["total_count"];
user = User.fromJson(json["user"]);
}

Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data["id"] = id;
data["discovered_count"] = discoveredCount;
data["total_count"] = totalCount;
data["user"] = user.toJson();
return data;
}

Completion copyWith({id, discoveredCount, totalCount, user}) {
return Completion(
id: id ?? this.id,
discoveredCount: discoveredCount ?? this.discoveredCount,
totalCount: totalCount ?? this.totalCount,
user: user ?? this.user,
);
}

static Completion empty() {
return Completion(
id: "",
discoveredCount: 0,
totalCount: 0,
user: User.empty(),
);
}

@override
String toString() {
return 'Advert{id: $id, discoveredCount: $discoveredCount, totalCount: $totalCount, user: $user}';
}
}
63 changes: 63 additions & 0 deletions lib/greencode/class/greencode_item.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'package:myecl/user/class/user.dart';

class GreenCodeItem {
late final String id;
late final String qrCodeContent;
late final String title;
late final String content;
late final List<User> users;

GreenCodeItem({
required this.id,
required this.qrCodeContent,
required this.title,
required this.content,
this.users = const [],
});

GreenCodeItem.fromJson(Map<String, dynamic> json) {
id = json["id"];
qrCodeContent = json["qr_code_content"];
title = json["title"];
content = json["content"];
if (json["users"] != null) {
users = List<User>.from(json["users"].map((x) => User.fromJson(x)));
}
}

Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data["id"] = id;
data["qr_code_content"] = qrCodeContent;
data["title"] = title;
data["content"] = content;
if (data["users"] != null) {
data["users"] = users.map((x) => x.toJson()).toList();
}
return data;
}

GreenCodeItem copyWith({id, qrCodeContent, title, content, users}) {
return GreenCodeItem(
id: id ?? this.id,
qrCodeContent: qrCodeContent ?? this.qrCodeContent,
title: title ?? this.title,
content: content ?? this.content,
users: users ?? this.users,
);
}

static GreenCodeItem empty() {
return GreenCodeItem(
id: "",
qrCodeContent: "",
title: "",
content: "",
);
}

@override
String toString() {
return 'Advert{id: $id, qrCodeContent: $qrCodeContent, title: $title, content: $content , users: $users}';
}
}
51 changes: 51 additions & 0 deletions lib/greencode/providers/all_greencode_item_list_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:myecl/greencode/class/greencode_item.dart';
import 'package:myecl/greencode/repositories/greencode_item_repository.dart';
import 'package:myecl/tools/providers/list_notifier.dart';
import 'package:myecl/tools/token_expire_wrapper.dart';

class AllGreenCodeItemListNotifier extends ListNotifier<GreenCodeItem> {
final GreenCodeItemRepository greencodeItemRepository;
AllGreenCodeItemListNotifier({required this.greencodeItemRepository})
: super(const AsyncValue.loading());

Future<AsyncValue<List<GreenCodeItem>>> loadGreenCodeItemList() async {
return await loadList(greencodeItemRepository.getAllGreenCodeItems);
}

Future<bool> addGreenCodeItem(GreenCodeItem greencodeItem) async {
return await add(greencodeItemRepository.addGreenCodeItem, greencodeItem);
}

Future<bool> updateGreenCodeItem(GreenCodeItem greencodeItem) async {
return await update(
greencodeItemRepository.updateGreenCodeItem,
(greencodeItems, greencodeItem) => greencodeItems
..[greencodeItems.indexWhere((item) => item.id == greencodeItem.id)] =
greencodeItem,
greencodeItem,
);
}

Future<bool> deleteGreenCodeItem(GreenCodeItem greencodeItem) async {
return await delete(
greencodeItemRepository.deleteGreenCodeItem,
(greencodeItems, greencodeItem) =>
greencodeItems..removeWhere((item) => item.id == greencodeItem.id),
greencodeItem.id,
greencodeItem,
);
}
}

final greencodeItemListProvider = StateNotifierProvider<
AllGreenCodeItemListNotifier, AsyncValue<List<GreenCodeItem>>>((ref) {
final greencodeItemRepository = ref.watch(greencodeItemRepositoryProvider);
final greencodeItemListNotifier = AllGreenCodeItemListNotifier(
greencodeItemRepository: greencodeItemRepository,
);
tokenExpireWrapperAuth(ref, () async {
await greencodeItemListNotifier.loadGreenCodeItemList();
});
return greencodeItemListNotifier;
});
30 changes: 30 additions & 0 deletions lib/greencode/providers/greencode_item_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:myecl/greencode/class/greencode_item.dart';
import 'package:myecl/greencode/repositories/greencode_item_repository.dart';
import 'package:myecl/tools/providers/single_notifier.dart';

class GreenCodeItemNotifier extends SingleNotifier<GreenCodeItem> {
final GreenCodeItemRepository greencodeItemRepository;
GreenCodeItemNotifier({required this.greencodeItemRepository})
: super(const AsyncLoading());

void loadGreenCodeItembyQRCode(String qrCodeContent) async {
state = const AsyncLoading();
final item =
await greencodeItemRepository.getGreenCodeItemByQR(qrCodeContent);
state = AsyncData(item);
}

void setGreenCodeItem(GreenCodeItem i) {
state = AsyncData(i);
}
}

final greenCodeItemProvider =
StateNotifierProvider<GreenCodeItemNotifier, AsyncValue<GreenCodeItem>>(
(ref) {
final greencodeItemRepository = ref.watch(greencodeItemRepositoryProvider);
return GreenCodeItemNotifier(
greencodeItemRepository: greencodeItemRepository,
);
});
9 changes: 9 additions & 0 deletions lib/greencode/providers/is_greencode_admin_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:myecl/user/providers/user_provider.dart';

final isGreenCodeAdminProvider = StateProvider<bool>((ref) {
final me = ref.watch(userProvider);
return me.groups
.map((e) => e.id)
.contains("e23d52eb-3fe4-4ce3-91f5-1f645647ef7d");
});
52 changes: 52 additions & 0 deletions lib/greencode/providers/user_greencode_item_list_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:myecl/greencode/class/greencode_item.dart';
import 'package:myecl/greencode/repositories/greencode_item_repository.dart';
import 'package:myecl/tools/providers/list_notifier.dart';
import 'package:myecl/tools/token_expire_wrapper.dart';

class UserGreenCodeItemListNotifier extends ListNotifier<GreenCodeItem> {
final GreenCodeItemRepository greencodeItemRepository;
UserGreenCodeItemListNotifier({required this.greencodeItemRepository})
: super(const AsyncValue.loading());

Future<AsyncValue<List<GreenCodeItem>>>
loadCurrentUserGreenCodeItemList() async {
return await loadList(greencodeItemRepository.getCurrentUserGreenCodeItems);
}

Future<bool> addCurrentUserToGreenCodeItem(
GreenCodeItem greencodeItem,
) async {
return await add(
(GreenCodeItem item) async {
await greencodeItemRepository.addCurrentUserToGreenCodeItem(item.id);
return item;
},
greencodeItem,
);
}

Future<bool> removeCurrentUserFromGreenCodeItem(
GreenCodeItem greencodeItem,
) async {
return delete(
greencodeItemRepository.removeCurrentUserFromGreenCodeItem,
(items, item) =>
items..removeWhere((itemToCheck) => itemToCheck.id == item.id),
greencodeItem.id,
greencodeItem,
);
}
}

final greencodeItemListProvider = StateNotifierProvider<
UserGreenCodeItemListNotifier, AsyncValue<List<GreenCodeItem>>>((ref) {
final greencodeItemRepository = ref.watch(greencodeItemRepositoryProvider);
final greencodeItemListNotifier = UserGreenCodeItemListNotifier(
greencodeItemRepository: greencodeItemRepository,
);
tokenExpireWrapperAuth(ref, () async {
await greencodeItemListNotifier.loadCurrentUserGreenCodeItemList();
});
return greencodeItemListNotifier;
});
22 changes: 22 additions & 0 deletions lib/greencode/repositories/greencode_completion_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:myecl/greencode/class/completion.dart';
import 'package:myecl/tools/repository/repository.dart';

class GreenCodeCompletionRepository extends Repository {
@override
// ignore: overridden_fields
final ext = 'greencode/completion/';

Future<List<Completion>> getAllGreenCodeCompletions() async {
return (await getList(suffix: 'all'))
.map((jsonCompletion) => Completion.fromJson(jsonCompletion))
.toList();
}

Future<Completion> getCurrentUserGreenCodeCompletion() async {
return Completion.fromJson(await getOne("me"));
}

Future<Completion> getGreenCodeCompletionByUser(String userId) async {
return Completion.fromJson(await getOne(userId));
}
}
60 changes: 60 additions & 0 deletions lib/greencode/repositories/greencode_item_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:myecl/auth/providers/openid_provider.dart';
import 'package:myecl/greencode/class/greencode_item.dart';
import 'package:myecl/tools/repository/repository.dart';

class GreenCodeItemRepository extends Repository {
@override
// ignore: overridden_fields
final ext = 'greencode/';

Future<List<GreenCodeItem>> getAllGreenCodeItems() async {
return (await getList(suffix: 'items'))
.map((jsonItem) => GreenCodeItem.fromJson(jsonItem))
.toList();
}

Future<List<GreenCodeItem>> getCurrentUserGreenCodeItems() async {
return (await getList(suffix: 'items/me'))
.map((jsonItem) => GreenCodeItem.fromJson(jsonItem))
.toList();
}

Future<GreenCodeItem> getGreenCodeItemByQR(String qrCodeContent) async {
return GreenCodeItem.fromJson(await getOne(qrCodeContent));
}

Future<GreenCodeItem> addGreenCodeItem(GreenCodeItem item) async {
return GreenCodeItem.fromJson(await create(item.toJson(), suffix: 'item'));
}

Future<bool> updateGreenCodeItem(GreenCodeItem item) async {
return await update(item.toJson(), "item/${item.id}");
}

Future<bool> deleteGreenCodeItem(String itemId) async {
return await delete("item/$itemId");
}

Future<bool> addCurrentUserToGreenCodeItem(String itemId) async {
return await create("", suffix: 'item/$itemId/me');
}

Future<bool> removeCurrentUserFromGreenCodeItem(String itemId) async {
return await delete("item/$itemId/me");
}

Future<bool> addUserToGreenCodeItem(String itemId, String userId) async {
return await create("", suffix: 'item/$itemId/$userId');
}

Future<bool> removeUserFromGreenCodeItem(String itemId, String userId) async {
return await delete("item/$itemId/$userId");
}
}

final greencodeItemRepositoryProvider =
Provider<GreenCodeItemRepository>((ref) {
final token = ref.watch(tokenProvider);
return GreenCodeItemRepository()..setToken(token);
});
Loading