Skip to content
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
4 changes: 2 additions & 2 deletions lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class MyApp extends StatelessWidget {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => ArticleStore(loginStore, client),
create: (_) => ArticleRepository(loginStore, client),
),
ChangeNotifierProvider(
create: (_) => CategoryStore(client),
create: (_) => CategoryRepository(client),
),
ChangeNotifierProvider(
create: (_) => PostPageStore(),
Expand Down
6 changes: 6 additions & 0 deletions lib/models/api_repository.dart → lib/models/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class ApiClient implements BaseClient {
return lists;
}

@override
Future<List<Article>> getFilteredArticles(String categoryId) {
// TODO: implement getFilteredArticles
throw UnimplementedError();
}

@override
Future<void> postArticle(String title, File before, File after, String body, String categoryId, String userId) async {
// base64 encode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import 'package:gottiesclient/models/entities/entities.dart';

abstract class BaseClient {
Future<List<Article>> getArticles();

Future<List<Article>> getFilteredArticles(String categoryId);

Future<void> postArticle(String title, File before, File after, String body, String categoryId, String userId);

Future<void> deleteArticle(String id, String userID);

Future<List<Category>> getCategories();

/// いいね関係
Future<void> likeArticle(String userID, String articleID);

Future<void> unlikeArticle(String userID, String articleID);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ class FirestoreClient implements BaseClient {
@override
Future<List<Article>> getArticles() async {
final documents = (await _firestore.collection('articles').getDocuments()).documents;
return mapJsonToArticle(documents);
}

@override
Future<List<Article>> getFilteredArticles(String categoryId) async {
final categoryRef = _firestore.collection('category').where('id', isEqualTo: categoryId);
final articleRef = _firestore.collection('articles');
final documents = (await articleRef.where('category', isEqualTo: categoryRef).getDocuments()).documents;
return mapJsonToArticle(documents);
}

Future<List<Article>> mapJsonToArticle(List<DocumentSnapshot> documents) {
return Future.wait(
documents.map((document) async {
final data = document.data;
Expand Down
6 changes: 3 additions & 3 deletions lib/models/models.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export 'api_repository.dart';
export 'base_repository.dart';
export 'api_client.dart';
export 'base_client.dart';
export 'custom_exception.dart';
export 'entities/entities.dart';
export 'firestore_repository.dart';
export 'firestore_client.dart';
88 changes: 88 additions & 0 deletions lib/models/stores/article_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:gottiesclient/models/entities/article.dart';
import 'package:gottiesclient/models/models.dart';
import 'package:gottiesclient/models/stores/stores.dart';

class ArticleRepository extends ChangeNotifier {
ArticleRepository(LoginStore loginStore, BaseClient client)
: assert(client != null),
assert(loginStore != null),
_loginStore = loginStore,
_client = client {
getArticles();
}

final BaseClient _client;
final LoginStore _loginStore;
List<Article> articles;

String get userID => _loginStore.user?.uid;

// TODO: try catchはStoreで
Future<void> getArticles() async {
articles = await _client.getArticles();
notifyListeners();
}

Future<void> postArticle(String title, File before, File after, String body, String categoryId) async {
if (!_loginStore.isLoggedIn) {
await _loginStore.loginAnonymously();
}
await _client.postArticle(title, before, after, body, categoryId, userID ?? '');
}

Future<void> deleteArticle(Article article) async {
if (userID == article.userID) {
throw UnauthorisedException();
}
await _client.deleteArticle(article.id, userID);
articles = articles.where((element) => element.id != article.id).toList();
notifyListeners();
}

Future<void> likeArticle(String articleID) async {
if (userID == null) {
throw UnauthorisedException();
}
await _client.likeArticle(userID, articleID);
articles.forEach((article) {
if (article.id == articleID) {
article.likeUserIds.add(_loginStore.user.uid);
}
});
notifyListeners();
}

bool isLike(String articleId) {
if (userID == null) {
return false;
}
final article = articles.firstWhere((article) => article.id == articleId);
final isLike = article.likeUserIds.contains(userID);
return isLike;
}

Future<void> unlikeArticle(String articleID) async {
if (userID == null) {
throw UnauthorisedException();
}
await _client.unlikeArticle(userID, articleID);
articles.forEach((article) {
if (article.id == articleID) {
article.likeUserIds.remove(userID);
}
});
notifyListeners();
}

Future<void> filterArticles(Category category) async {
articles = await _client.getFilteredArticles(category.id);
notifyListeners();
}
Comment on lines +80 to +83
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なぜかファイルごとaddな差分になってしまってるけど、変更した箇所はここと、
それぞれのメソッドのtry-catchを一時的に外した感じ

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

今View に入ってる Repository はふさわしい Store で置き換えられる認識でいいよね?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうやね!HomeStoreとかProfileStoreとかな感じ!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OKOK~


bool isMyArticle(Article article) {
return article.userID == userID;
}
}
118 changes: 0 additions & 118 deletions lib/models/stores/article_store.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ import 'package:flutter/material.dart';
import 'package:gottiesclient/models/entities/entities.dart';
import 'package:gottiesclient/models/models.dart';

class CategoryStore extends ChangeNotifier {
CategoryStore(BaseClient client)
class CategoryRepository extends ChangeNotifier {
CategoryRepository(BaseClient client)
: assert(client != null),
_client = client {
getCategories();
}

final BaseClient _client;

List<Category> _categories;
List<Category> searchedCategories;

// カテゴリを選択していない状態(null) = 全てのカテゴリ
Category selectedCategory;

// TODO: try catchはStoreで
Future<void> getCategories() async {
try {
_categories = await _client.getCategories();
searchedCategories = _categories;
notifyListeners();
} on Exception catch (e) {
debugPrint(e.toString());
}
_categories = await _client.getCategories();
searchedCategories = _categories;
notifyListeners();
}

void searchCategory(String word) {
Expand Down
4 changes: 2 additions & 2 deletions lib/models/stores/stores.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export 'article_store.dart';
export 'category_store.dart';
export 'article_repository.dart';
export 'category_repository.dart';
export 'detail_store.dart';
export 'focus_node_store.dart';
export 'login_store.dart';
Expand Down
16 changes: 8 additions & 8 deletions lib/pages/detail/widgets/content_container.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gottiesclient/models/entities/article.dart';
import 'package:gottiesclient/models/stores/article_store.dart';
import 'package:gottiesclient/models/stores/share_store.dart';
import 'package:gottiesclient/models/stores/stores.dart';
import 'package:gottiesclient/util/converter.dart';
import 'package:gottiesclient/widgets/icon_button_with_splash.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -105,18 +105,18 @@ class ContentContainer extends StatelessWidget {
width: 45,
height: 45,
icon: Icon(
Provider.of<ArticleStore>(context).isLike(article.id)
Provider.of<ArticleRepository>(context).isLike(article.id)
? Icons.favorite
: Icons.favorite_border,
color: Colors.red,
),
onTapButton: () {
final store = Provider.of<ArticleStore>(context, listen: false);
final isLike = store.isLike(article.id);
final repository = Provider.of<ArticleRepository>(context, listen: false);
final isLike = repository.isLike(article.id);
if (isLike) {
store.unlikeArticle(article.id);
repository.unlikeArticle(article.id);
} else {
store.likeArticle(article.id);
repository.likeArticle(article.id);
}
},
),
Expand All @@ -130,13 +130,13 @@ class ContentContainer extends StatelessWidget {
article.beforeImageURL,
),
),
if (Provider.of<ArticleStore>(context).isMyArticle(article))
if (Provider.of<ArticleRepository>(context).isMyArticle(article))
IconButtonWithSplash(
width: 45,
height: 45,
icon: Icon(Icons.delete),
onTapButton: () {
Provider.of<ArticleStore>(context, listen: false).deleteArticle(article);
Provider.of<ArticleRepository>(context, listen: false).deleteArticle(article);
Navigator.pop(context);
},
),
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/home/article_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ArticleList extends StatelessWidget {
alignment: Alignment.centerLeft,
margin: const EdgeInsets.only(top: 16, left: 8, bottom: 8),
child: Text(
'${Provider.of<CategoryStore>(context).selectedCategory?.title ?? 'ALL'}',
'${Provider.of<CategoryRepository>(context).selectedCategory?.title ?? 'ALL'}',
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
),
Expand Down
Loading