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

Refactor/i category view model #1

Merged
Merged
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
11 changes: 11 additions & 0 deletions lib/common/utils.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter_emoji/flutter_emoji.dart';
import 'package:intl/intl.dart';
Expand Down Expand Up @@ -48,3 +49,13 @@ extension WidgetUtils on Widget {
);
}
}

extension ToMapX on DocumentSnapshot {
Map<String, dynamic> toMap() {
final _map = data();
return {
'id': id,
if (_map != null) ..._map,
};
}
}
9 changes: 9 additions & 0 deletions lib/model/category.model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ class Category {
final Emoji emoji;
final int? todoSize;

factory Category.fromMap(Map<String, dynamic> map) {
return Category(
id: map['id'],
name: map['name'],
emoji: map['emoji'],
todoSize: map['todoSize'],
);
}

Category.fromSnapshot(DocumentSnapshot snapshot)
: id = snapshot.id,
name = snapshot.data()!['name'],
Expand Down
5 changes: 3 additions & 2 deletions lib/provider_dependency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ final databaseProvider = Provider((_) => FirebaseFirestore.instance);
/// Repository

final todoRepositoryProvider = Provider<ITodoRepository>(
(ref) => TodoRepository(ref.watch(databaseProvider)),
(ref) => TodoRepository(ref.read),
);

/// ViewModel

final categoryViewModelProvider =
StateNotifierProvider<CategoryViewModel, CategoryState>(
(ref) => CategoryViewModel(ref.read));
(ref) => CategoryViewModel(ref.read),
);

final todoViewModelProvider = StateNotifierProvider<TodoViewModel, TodoState>(
(ref) => TodoViewModel(ref.read),
Expand Down
9 changes: 7 additions & 2 deletions lib/repository/todo.repository.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:crud_todo_app/model/category.model.dart';
import 'package:crud_todo_app/model/todo.model.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../provider_dependency.dart';

abstract class ITodoRepository {
Stream<List<Category>> getCategories();
Expand All @@ -17,9 +20,11 @@ abstract class ITodoRepository {
}

class TodoRepository implements ITodoRepository {
final FirebaseFirestore _database;
TodoRepository(this.read);

final Reader read;

TodoRepository(this._database);
FirebaseFirestore get _database => read(databaseProvider);

static const String _categoryCollection = 'categories';
static const String _todoCollection = 'todos';
Expand Down
35 changes: 28 additions & 7 deletions lib/viewmodel/category/category_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import 'package:crud_todo_app/viewmodel/category/category_state.dart';
import 'package:flutter_emoji/flutter_emoji.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

class CategoryViewModel extends StateNotifier<CategoryState> with Validations {
late final Reader _read;
import '../../model/validation_text.model.dart';
import '../../repository/todo.repository.dart';

abstract class ICategoryViewModel extends StateNotifier<CategoryState>
with Validations {
ICategoryViewModel() : super(const CategoryState.initial());

CategoryViewModel(this._read) : super(const CategoryState.initial());
ValidationText get nameCat;
ValidationText get emojiCat;
ITodoRepository get todoRepo;

ValidationText onChangeName(String value) => validateEmpty(value);

Expand All @@ -20,10 +26,10 @@ class CategoryViewModel extends StateNotifier<CategoryState> with Validations {
try {
state = CategoryState.loading();

final name = _read(nameCatProvider).state;
final emoji = _read(emojiCatProvider).state;
final name = nameCat;
final emoji = emojiCat;

await _read(todoRepositoryProvider).saveCategory(Category(
await todoRepo.saveCategory(Category(
name: name.text!,
emoji: EmojiParser().getEmoji(emoji.text!),
));
Expand All @@ -37,10 +43,25 @@ class CategoryViewModel extends StateNotifier<CategoryState> with Validations {
void removeCategory(String id) async {
try {
state = CategoryState.loading();
await _read(todoRepositoryProvider).deleteCategory(id);
await todoRepo.deleteCategory(id);
state = CategoryState.success(CategoryAction.remove);
} on Exception catch (e) {
state = CategoryState.error(e.toString());
}
}
}

class CategoryViewModel extends ICategoryViewModel {
CategoryViewModel(this._read);

late final Reader _read;

@override
ValidationText get nameCat => _read(nameCatProvider).state;

@override
ValidationText get emojiCat => _read(emojiCatProvider).state;

@override
ITodoRepository get todoRepo => _read(todoRepositoryProvider);
}
6 changes: 3 additions & 3 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ packages:
name: flutter_riverpod
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.0+1"
version: "0.14.0+3"
flutter_slidable:
dependency: "direct main"
description:
Expand Down Expand Up @@ -325,7 +325,7 @@ packages:
name: hooks_riverpod
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.0+1"
version: "0.14.0+3"
http:
dependency: transitive
description:
Expand Down Expand Up @@ -507,7 +507,7 @@ packages:
name: riverpod
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.0+1"
version: "0.14.0+3"
shelf:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies:
sdk: flutter

cupertino_icons: ^1.0.2
hooks_riverpod: ^0.14.0+1
hooks_riverpod: ^0.14.0+3
flutter_hooks: ^0.16.0
freezed_annotation: ^0.14.1
firebase_core: ^1.0.3
Expand Down