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

Develop #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 0 additions & 15 deletions db.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,6 @@
"text": "test 3",
"creation-date": "2021-06-16 11:29:50.054625",
"id": 8
},
{
"text": "",
"creation-date": "2021-06-16 11:56:00.178866",
"id": 9
},
{
"text": "",
"creation-date": "2021-06-16 11:56:21.636490",
"id": 10
},
{
"text": "",
"creation-date": "2021-06-16 11:56:32.360321",
"id": 11
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ import 'package:flutter/material.dart';
class InputText extends StatelessWidget {
const InputText({
Key? key,
required this.textEditingController,
required this.onChanged,
required this.validator,
}) : super(key: key);

final TextEditingController textEditingController;
final void Function(String)? onChanged;
final String? Function(String?)? validator;

@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
controller: textEditingController,
autovalidateMode: AutovalidateMode.onUserInteraction,
onChanged: onChanged,
decoration: const InputDecoration(
hintText: 'type the text to you post',
border: OutlineInputBorder(),
Expand Down
58 changes: 22 additions & 36 deletions lib/modules/posts/view_model/new_post_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter_mobx_template/models/post.dart';
import 'package:flutter_mobx_template/repository/i_post_repository.dart';
import 'package:flutter_mobx_template/ui/functions/show_adaptive_dialog.dart';
import 'package:mobx/mobx.dart';

part 'new_post_view_model.g.dart';
Expand All @@ -14,14 +14,7 @@ abstract class NewPostViewModelBase with Store {
final IPostRepository _repository;

@observable
bool _isSaving = false;
@observable
GlobalKey<FormState> formKey = GlobalKey<FormState>();
@observable
TextEditingController textController = TextEditingController(text: '');

@computed
bool get isSaving => _isSaving;
String text = '';

@action
String? validateTextPost(String? value) {
Expand All @@ -31,36 +24,29 @@ abstract class NewPostViewModelBase with Store {
return null;
}

@observable
ObservableFuture<Post> savePostFuture = ObservableFuture<Post>.error('');

@computed
String get errorMessage => savePostFuture.status == FutureStatus.rejected
? savePostFuture.error.toString()
: '';

@observable
bool isSavingPost = false;

@action
Future<Post> addNewPost(BuildContext context) async {
Future<Post> addNewPost() async {
try {
_isSaving = true;
if (!formKey.currentState!.validate()) {
throw const FormatException('The Form is invalid');
}
final Post post = await _repository.add(
text: textController.text,
creationDate: DateTime.now().toString(),
);
textController.clear(); // empty TextEditingController
Navigator.of(context).pop(post); // close BottomSheet
_isSaving = false;
isSavingPost = true;
savePostFuture = _repository
.add(text: text, creationDate: DateTime.now().toString())
.asObservable();
final Post post = await savePostFuture;
isSavingPost = false;
return post;
} on FormatException catch (e, stackTrace) {
_isSaving = false;
showAdaptiveDialog(
context: context,
title: 'Error',
content: e.message,
);
return Future<Post>.error(e, stackTrace);
} catch (e, stackTrace) {
_isSaving = false;
await showAdaptiveDialog(
context: context,
title: 'Error',
content: e.toString(),
);
isSavingPost = false;
return Future<Post>.error(e, stackTrace);
}
}
Expand Down
63 changes: 32 additions & 31 deletions lib/modules/posts/view_model/new_post_view_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 18 additions & 38 deletions lib/modules/posts/view_model/post_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import 'dart:developer';
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_mobx_template/models/post.dart';
import 'package:flutter_mobx_template/modules/posts/view_model/new_post_view_model.dart';
import 'package:flutter_mobx_template/modules/posts/views/new_post_view.dart';
import 'package:flutter_mobx_template/repository/i_post_repository.dart';
import 'package:mobx/mobx.dart';

Expand All @@ -21,43 +18,26 @@ abstract class PostViewModelBase with Store {
@observable
ObservableList<Post> posts = ObservableList<Post>.of(<Post>[]);

@action
Future<void> loadPosts({BuildContext? context}) async {
try {
posts = (await _repository.getAll()).asObservable();
} catch (e, stackTrace) {
log(e.toString(),
name: 'PostViewModel.loadPosts', stackTrace: stackTrace);
if (context != null) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(e.toString())));
}
}
}
@observable
ObservableFuture<List<Post>> getPostsFuture =
ObservableFuture<List<Post>>.value(<Post>[]);

@computed
String get errorMessage => getPostsFuture.status == FutureStatus.rejected
? getPostsFuture.error.toString()
: '';

Future<void> openNewPostBottomSheet(
BuildContext context,
PostViewModel postViewModel,
) async {
@computed
bool get hasData =>
getPostsFuture.status == FutureStatus.fulfilled && posts.isNotEmpty;

@action
Future<void> loadPosts() async {
try {
final Post? post = await showModalBottomSheet<Post>(
context: context,
builder: (BuildContext context) {
return NewPostPage(newPostViewModel: NewPostViewModel(_repository));
},
backgroundColor: Colors.black54,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(5),
topRight: Radius.circular(5),
),
),
);
if (post != null) {
loadPosts();
}
getPostsFuture = _repository.getAll().asObservable();
posts = (await getPostsFuture).asObservable();
} catch (e, stackTrace) {
log(e.toString(), name: 'openNewPostBottomSheet', stackTrace: stackTrace);
return Future<void>.error(e, stackTrace);
}
}
}
38 changes: 35 additions & 3 deletions lib/modules/posts/view_model/post_view_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading