Skip to content

Commit

Permalink
feat: 폴더탭 화면 riverpod 상태관리 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
nain93 committed Aug 3, 2023
1 parent 76e5f59 commit cadb1c7
Show file tree
Hide file tree
Showing 19 changed files with 422 additions and 183 deletions.
43 changes: 43 additions & 0 deletions lib/providers/folder_detail_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'dart:async';

import 'package:moa_app/models/content_model.dart';
import 'package:moa_app/repositories/folder_repository.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'folder_detail_provider.g.dart';

/// AsyncNotifierProvider
// @Riverpod(keepAlive: true)
@riverpod
class FolderDetail extends _$FolderDetail {
Future<List<ContentModel>> fetchItem({required String folderName}) async {
// get the [KeepAliveLink]
var link = ref.keepAlive();
// a timer to be used by the callbacks below
Timer? timer;
// An object from package:dio that allows cancelling http requests
// When the provider is destroyed, cancel the http request and the timer
ref.onDispose(() {
timer?.cancel();
});
// When the last listener is removed, start a timer to dispose the cached data
ref.onCancel(() {
// start a 30 second timer
timer = Timer(const Duration(seconds: 30), () {
// dispose on timeout
link.close();
});
});
// If the provider is listened again after it was paused, cancel the timer
ref.onResume(() {
timer?.cancel();
});

var data =
FolderRepository.instance.getFolderDetailList(folderName: folderName);
return data;
}

@override
Future<void> build() async {}
}
62 changes: 61 additions & 1 deletion lib/providers/folder_view_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,72 @@ class FolderView extends _$FolderView {
timer?.cancel();
});

var data = FolderRepository.instance.getFolderList();
var data = await FolderRepository.instance.getFolderList();
return data;
}

@override
Future<List<FolderModel>> build() async {
return fetchItem();
}

Future<void> addFolder({
required String folderName,
}) async {
state = const AsyncValue.loading();

state = await AsyncValue.guard(() async {
// await FolderRepository.instance.addFolder(folderName: folderName);
return fetchItem();
// return [
// ...state.value ?? [],
// FolderModel(
// folderId: 0,
// folderName: folderName,
// count: 0,
// )
// ];
});
}

Future<void> editFolderName({
required String currentFolderName,
required String editFolderName,
}) async {
state = const AsyncValue.loading();

state = await AsyncValue.guard(() async {
// await FolderRepository.instance.editFolderName(
// currentFolderName: currentFolderName,
// editFolderName: editFolderName,
// );
return fetchItem();

// return state.value?.map((element) {
// if (element.folderName == currentFolderName) {
// return FolderModel(
// folderId: element.folderId,
// folderName: editFolderName,
// count: element.count,
// );
// }
// return element;
// }).toList() ??
// [];
});
}

Future<void> deleteFolder({required String folderName}) async {
state = const AsyncValue.loading();

state = await AsyncValue.guard(() async {
// await FolderRepository.instance.deleteFolder(folderName: folderName);

return fetchItem();
// return state.value
// ?.where((element) => element.folderName != folderName)
// .toList() ??
// [];
});
}
}
61 changes: 40 additions & 21 deletions lib/providers/hashtag_view_provider.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:moa_app/models/content_model.dart';
import 'package:moa_app/repositories/content_repository.dart';
import 'package:moa_app/repositories/hashtag_repository.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

Expand All @@ -11,27 +12,27 @@ part 'hashtag_view_provider.g.dart';
@riverpod
class HashtagView extends _$HashtagView {
Future<(List<ContentModel>, int)> fetchItem() async {
// get the [KeepAliveLink]
var link = ref.keepAlive();
// a timer to be used by the callbacks below
Timer? timer;
// An object from package:dio that allows cancelling http requests
// When the provider is destroyed, cancel the http request and the timer
ref.onDispose(() {
timer?.cancel();
});
// When the last listener is removed, start a timer to dispose the cached data
ref.onCancel(() {
// start a 30 second timer
timer = Timer(const Duration(seconds: 30), () {
// dispose on timeout
link.close();
});
});
// If the provider is listened again after it was paused, cancel the timer
ref.onResume(() {
timer?.cancel();
});
// // get the [KeepAliveLink]
// var link = ref.keepAlive();
// // a timer to be used by the callbacks below
// Timer? timer;
// // An object from package:dio that allows cancelling http requests
// // When the provider is destroyed, cancel the http request and the timer
// ref.onDispose(() {
// timer?.cancel();
// });
// // When the last listener is removed, start a timer to dispose the cached data
// ref.onCancel(() {
// // start a 30 second timer
// timer = Timer(const Duration(seconds: 30), () {
// // dispose on timeout
// link.close();
// });
// });
// // If the provider is listened again after it was paused, cancel the timer
// ref.onResume(() {
// timer?.cancel();
// });

var data = await HashtagRepository.instance.getHashtagView();
return data;
Expand All @@ -42,6 +43,24 @@ class HashtagView extends _$HashtagView {
return fetchItem();
}

Future<void> deleteContent({required String contentId, required}) async {
state = const AsyncValue.loading();

state = await AsyncValue.guard(() async {
await ContentRepository.instance.deleteContent(contentId: contentId);
var value = state.value;
if (value != null) {
var (list, count) = value;
var filteredList =
list.where((element) => element.contentId != contentId).toList();

return (filteredList, count);
}

return value!;
});
}

Future<(List<ContentModel>, int)> loadMoreData({
int? page,
int? size,
Expand Down
36 changes: 36 additions & 0 deletions lib/repositories/content_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ abstract class IContentRepository {
Future<ContentModel> getContentDetail({
required String contentId,
});

Future<void> editContent({
required String contentId,
required ContentModel content,
required String hashTagStringList,
});

Future<void> deleteContent({
required String contentId,
});
}

class ContentRepository implements IContentRepository {
Expand Down Expand Up @@ -100,4 +110,30 @@ class ContentRepository implements IContentRepository {
rethrow;
}
}

@override
Future<void> editContent({
required String contentId,
required ContentModel content,
required String hashTagStringList,
}) async {
throw UnimplementedError();
}

@override
Future<void> deleteContent({required String contentId}) async {
var token = await TokenRepository.instance.getToken();

await dio.post(
'/api/v1/content/delete',
data: {
'ids': [contentId]
},
options: Options(
headers: {
'Authorization': 'Bearer $token',
},
),
);
}
}
10 changes: 9 additions & 1 deletion lib/screens/home/content_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:moa_app/constants/file_constants.dart';
import 'package:moa_app/constants/font_constants.dart';
import 'package:moa_app/models/content_model.dart';
import 'package:moa_app/providers/content_detail_provider.dart';
import 'package:moa_app/providers/hashtag_view_provider.dart';
import 'package:moa_app/screens/home/edit_content_view.dart';
import 'package:moa_app/utils/general.dart';
import 'package:moa_app/widgets/app_bar.dart';
Expand All @@ -27,6 +28,7 @@ class ContentView extends HookConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
var contentNotifier = ref.watch(contentDetailProvider.notifier);
var hashtagAsync = ref.watch(hashtagViewProvider.notifier);
var isEditMode = useState(false);

void pressGoToLink() {}
Expand All @@ -37,7 +39,12 @@ class ContentView extends HookConsumerWidget {
isEditMode.value = true;
}

void deleteContent() {}
void deleteContent() async {
await hashtagAsync.deleteContent(contentId: id);
if (context.mounted) {
context.pop();
}
}

void showContentModal() {
General.instance.showBottomSheet(
Expand Down Expand Up @@ -149,6 +156,7 @@ class ContentView extends HookConsumerWidget {
),
child: ImageOnNetwork(
imageURL: content.contentImageUrl,
fit: BoxFit.contain,
),
),
),
Expand Down
8 changes: 5 additions & 3 deletions lib/screens/home/folder_detail_view.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:moa_app/constants/color_constants.dart';
import 'package:moa_app/constants/file_constants.dart';
import 'package:moa_app/models/content_model.dart';
Expand All @@ -13,12 +13,12 @@ import 'package:moa_app/widgets/moa_widgets/dynamic_grid_list.dart';
import 'package:moa_app/widgets/moa_widgets/empty_content.dart';
import 'package:share_plus/share_plus.dart';

class FolderDetailView extends HookWidget {
class FolderDetailView extends HookConsumerWidget {
const FolderDetailView({super.key, required this.folderName});
final String folderName;

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
Future<void> pullToRefresh() async {
return Future.delayed(
const Duration(seconds: 2),
Expand Down Expand Up @@ -57,9 +57,11 @@ class FolderDetailView extends HookWidget {
.getFolderDetailList(folderName: folderName),
builder: (context, snapshot) {
var contentList = snapshot.data ?? [];

if (snapshot.connectionState == ConnectionState.waiting) {
return const LoadingIndicator();
}

if (snapshot.hasData && contentList.isNotEmpty) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 15),
Expand Down
5 changes: 0 additions & 5 deletions lib/screens/home/hashtag_detail_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class HashtagDetailView extends HookWidget {
Widget build(BuildContext context) {
var gridController = useScrollController();
var gridPageNum = useState(20);

var updatedHashtagName = useState('');

Future<void> pullToRefresh() async {
Expand All @@ -31,10 +30,6 @@ class HashtagDetailView extends HookWidget {
);
}

// Future<void> getHashtagDetailViewList() async {
// hashTagList.value = await
// }

void showEditHashtagModal() {
General.instance.showBottomSheet(
context: context,
Expand Down
Loading

0 comments on commit cadb1c7

Please sign in to comment.