Skip to content

Commit

Permalink
ファイルが添付されているノート一覧を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
poppingmoon committed Oct 23, 2023
1 parent 0d6e050 commit c5c8a06
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:miria/state_notifier/common/download_file_notifier.dart';
import 'package:miria/state_notifier/common/misskey_server_list_notifier.dart';
import 'package:miria/state_notifier/drive_page/breadcrumbs_notifier.dart';
import 'package:miria/state_notifier/drive_page/drive_file_notes_page/drive_files_attached_notes_notifier.dart';
import 'package:miria/state_notifier/drive_page/drive_files_notifier.dart';
import 'package:miria/state_notifier/drive_page/drive_folders_notifier.dart';
import 'package:miria/state_notifier/note_create_page/note_create_state_notifier.dart';
Expand Down Expand Up @@ -286,3 +287,8 @@ final breadcrumbsNotifierProvider =
NotifierProvider.autoDispose<BreadcrumbsNotifier, List<DriveFolder>>(
BreadcrumbsNotifier.new,
);

final driveFilesAttachedNotesProvider = NotifierProvider.autoDispose.family<
DriveFilesAttachedNotesNotifier,
PaginationState<Note>,
(Misskey, String)>(DriveFilesAttachedNotesNotifier.new);
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'dart:async';

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:miria/model/pagination_state.dart';
import 'package:misskey_dart/misskey_dart.dart';

class DriveFilesAttachedNotesNotifier extends AutoDisposeFamilyNotifier<
PaginationState<Note>, (Misskey, String)> {
@override
PaginationState<Note> build((Misskey, String) arg) {
Future(loadMore);
return const PaginationState();
}

Misskey get _misskey => arg.$1;

String get _fileId => arg.$2;

Future<void> loadMore() async {
if (state.isLoading || state.isLastLoaded) {
return;
}
state = state.copyWith(isLoading: true);
final untilId = state.lastOrNull?.id;
try {
final response = await _misskey.drive.files.attachedNotes(
DriveFilesAttachedNotesRequest(
fileId: _fileId,
untilId: untilId,
),
);
// Misskey 2023.10.0 より前はpaginationがなかったため
if (response.any((e) => e.id == state.firstOrNull?.id)) {
state = state.copyWith(isLastLoaded: true);
} else {
state = state.copyWith(
items: [...state, ...response],
isLastLoaded: response.isEmpty,
);
}
} finally {
state = state.copyWith(isLoading: false);
}
}
}
7 changes: 7 additions & 0 deletions lib/view/drive_page/drive_file_grid_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class DriveFileGridItem extends ConsumerWidget {
file: file,
),
),
onLongPress: () => showModalBottomSheet<void>(
context: context,
builder: (context) => DriveFileModalSheet(
account: account,
file: file,
),
),
child: Column(
children: [
Expanded(
Expand Down
77 changes: 77 additions & 0 deletions lib/view/drive_page/drive_file_page/drive_file_notes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:miria/model/account.dart';
import 'package:miria/model/general_settings.dart';
import 'package:miria/providers.dart';
import 'package:miria/view/common/account_scope.dart';
import 'package:miria/view/common/error_dialog_handler.dart';
import 'package:miria/view/common/misskey_notes/misskey_note.dart';
import 'package:miria/view/common/pagination_bottom_item.dart';
import 'package:misskey_dart/misskey_dart.dart';

class DriveFileNotes extends ConsumerWidget {
const DriveFileNotes({
super.key,
required this.account,
required this.file,
});

final Account account;
final DriveFile file;

@override
Widget build(BuildContext context, WidgetRef ref) {
final misskey = ref.watch(misskeyProvider(account));
final notes =
ref.watch(driveFilesAttachedNotesProvider((misskey, file.id)));
final loadAutomatically = ref.watch(
generalSettingsRepositoryProvider.select(
(repository) =>
repository.settings.automaticPush == AutomaticPush.automatic,
),
);

return RefreshIndicator(
onRefresh: () async =>
ref.invalidate(driveFilesAttachedNotesProvider((misskey, file.id))),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: ListView.builder(
itemCount: notes.length + 1,
itemBuilder: (context, index) {
if (index < notes.length) {
return AccountScope(
account: account,
child: MisskeyNote(note: notes[index]),
);
}
if (loadAutomatically && !notes.isLoading && !notes.isLastLoaded) {
Future(() {
ref
.read(
driveFilesAttachedNotesProvider((misskey, file.id))
.notifier,
)
.loadMore()
.expectFailure(context);
});
}
return Center(
child: Padding(
padding: const EdgeInsets.all(10),
child: PaginationBottomItem(
paginationState: notes,
noItemLabel: const Text("添付されているノートがありません"),
child: IconButton(
onPressed: () {},
icon: const Icon(Icons.keyboard_arrow_down),
),
),
),
);
},
),
),
);
}
}
5 changes: 4 additions & 1 deletion lib/view/drive_page/drive_file_page/drive_file_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:miria/model/account.dart';
import 'package:miria/providers.dart';
import 'package:miria/view/drive_page/drive_file_modal_sheet.dart';
import 'package:miria/view/drive_page/drive_file_page/drive_file_details.dart';
import 'package:miria/view/drive_page/drive_file_page/drive_file_notes.dart';
import 'package:misskey_dart/misskey_dart.dart';

@RoutePage()
Expand All @@ -29,7 +30,7 @@ class DriveFilePage extends ConsumerWidget {
) ??
this.file;
return DefaultTabController(
length: 1,
length: 2,
child: Scaffold(
appBar: AppBar(
title: const Text("ファイルの詳細"),
Expand Down Expand Up @@ -57,12 +58,14 @@ class DriveFilePage extends ConsumerWidget {
bottom: const TabBar(
tabs: [
Tab(icon: Icon(Icons.info), text: "情報"),
Tab(icon: Icon(Icons.edit), text: "添付されているノート"),
],
),
),
body: TabBarView(
children: [
DriveFileDetails(account: account, file: file),
DriveFileNotes(account: account, file: file),
],
),
),
Expand Down

0 comments on commit c5c8a06

Please sign in to comment.