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

feat:외부에서 공유 url 받아오기 #47

Merged
merged 2 commits into from
Jul 29, 2023
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
1 change: 1 addition & 0 deletions lib/models/content_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ContentModel with _$ContentModel {
factory ContentModel({
required String contentId,
required String contentImageUrl,
String? contentUrl,
String? contentMemo,
required String contentName,
required List<HashtagModel> contentHashTag,
Expand Down
94 changes: 92 additions & 2 deletions lib/navigations/main_bottom_tab.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,109 @@
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:moa_app/constants/app_constants.dart';
import 'package:moa_app/constants/color_constants.dart';
import 'package:moa_app/constants/file_constants.dart';
import 'package:moa_app/screens/add_content/folder_select.dart';
import 'package:moa_app/utils/my_platform.dart';
import 'package:moa_app/utils/router_provider.dart';
import 'package:receive_sharing_intent/receive_sharing_intent.dart';

class MainBottomTab extends HookWidget {
class MainBottomTab extends HookConsumerWidget {
const MainBottomTab({super.key, required this.child});
final Widget child;

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
var currentIndex = useState(0);
bool keyboardIsOpen = MediaQuery.of(context).viewInsets.bottom == 0;
var lifeCycle = useAppLifecycleState();

void navigateToShareMedia(
BuildContext context, List<SharedMediaFile> value) {
if (value.isNotEmpty) {
var newFiles = <File>[];
for (var element in value) {
newFiles.add(File(
Platform.isIOS
? element.type == SharedMediaType.FILE
? element.path
.toString()
.replaceAll(AppConstants.replaceableText, '')
: element.path
: element.path,
));
}

context.push(
'${GoRoutes.fileSharing.fullPath}/$newFiles',
);
}
}

var receiveUrl = useState('');

void navigateToShareText(BuildContext context, String? value) {
if (value != null && value.toString().isNotEmpty && context.mounted) {
receiveUrl.value = value;
}
}

//All listeners to listen Sharing media files & text
void listenShareMediaFiles(BuildContext context) {
// For sharing images coming from outside the app
// while the app is in the memory
ReceiveSharingIntent.getMediaStream().listen((value) {
navigateToShareMedia(context, value);
}, onError: (err) {
debugPrint('$err');
});

// For sharing images coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialMedia().then((value) {
navigateToShareMedia(context, value);
});

// For sharing or opening urls/text coming from outside the app while the app is in the memory
ReceiveSharingIntent.getTextStream().listen((value) {
navigateToShareText(context, value);
}, onError: (err) {
debugPrint('$err');
});

// For sharing or opening urls/text coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialText().then((value) {
navigateToShareText(context, value);
});
}

useEffect(() {
if (!kIsWeb) {
SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
listenShareMediaFiles(context);
});
}
return null;
}, []);

useEffect(() {
/// 외부에서 url 공유로 들어왔을경우 폴더선택화면으로 이동
if (receiveUrl.value.isNotEmpty &&
lifeCycle == AppLifecycleState.resumed) {
context.go(
GoRoutes.folderSelect.fullPath,
extra: FolderSelect(receiveUrl: receiveUrl.value),
);
receiveUrl.value = '';
return;
}
return null;
}, [lifeCycle]);

void tap(BuildContext context, int index) {
if (index == currentIndex.value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import 'dart:async';

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

part 'item_provider.g.dart';
part 'folder_view_provider.g.dart';

/// AsyncNotifierProvider
// @Riverpod(keepAlive: true)
@riverpod
class AsyncItems extends _$AsyncItems {
class FolderView extends _$FolderView {
Future<List<FolderModel>> fetchItem() async {
// get the [KeepAliveLink]
var link = ref.keepAlive();
Expand All @@ -32,45 +33,12 @@ class AsyncItems extends _$AsyncItems {
timer?.cancel();
});

// var itemData = ItemRepository.instance.getItems();
return [];
var data = FolderRepository.instance.getFolderList();
return data;
}

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

Future<void> addItems({
required FolderModel item,
}) async {
state = const AsyncValue.loading();

state = await AsyncValue.guard(() async {
// await ItemRepository.instance.addItem(item: item);
return fetchItem();
});
}

Future<void> removeItems({
required int id,
}) async {
state = const AsyncValue.loading();

state = await AsyncValue.guard(() async {
// await ItemRepository.instance.removeItem(id: id);
return fetchItem();
});
}

Future<void> updateItems({
required FolderModel item,
}) async {
state = const AsyncValue.loading();

state = await AsyncValue.guard(() async {
// await ItemRepository.instance.updateItem(item: item);
return fetchItem();
});
}
}
44 changes: 44 additions & 0 deletions lib/providers/hashtag_view_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'dart:async';

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

part 'hashtag_view_provider.g.dart';

/// AsyncNotifierProvider
// @Riverpod(keepAlive: true)
@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();
});

var data = HashtagRepository.instance.getHashtagView();
return data;
}

@override
Future<(List<ContentModel>, int)> build() async {
return fetchItem();
}
}
19 changes: 19 additions & 0 deletions lib/repositories/content_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ContentRepository implements IContentRepository {
var token = await TokenRepository.instance.getToken();

if (contentType.name == AddContentType.image.name) {
/// 이미지 방식
await dio.post(
'/api/v1/content/create',
data: {
Expand All @@ -47,6 +48,24 @@ class ContentRepository implements IContentRepository {

if (contentType.name == AddContentType.url.name) {
/// 링크 방식
await dio.post(
'/api/v1/content/create',
data: {
'folderId': content.contentId,
'name': content.contentName,
'memo': content.contentMemo,
'hashTag': hashTagStringList,
'contentType': 'URL',
'url': content.contentUrl,
// 'originalFileName': '${content.contentName}.png',
// 'image': 'image/png:base64:${content.contentImageUrl}',
},
options: Options(
headers: {
'Authorization': 'Bearer $token',
},
),
);
}
}
}
Loading
Loading