Better files list
All checks were successful
ci/woodpecker/push/flutterBuild Pipeline was successful

This commit is contained in:
Balazs Toldi 2022-05-16 10:02:11 +02:00
parent ec8f4a4b25
commit c272d8c888
Signed by: Bazsalanszky
GPG key ID: 6C7D440036F99D58
4 changed files with 139 additions and 75 deletions

View file

@ -0,0 +1,28 @@
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:gitea_client/model/File.dart';
import '../service/gitea_service.dart';
part 'file_list_load_event.dart';
part 'file_list_load_state.dart';
class FileListLoadBloc extends Bloc<FileListLoadEvent, FileListLoadState> {
final GiteaService giteaService;
FileListLoadBloc({required this.giteaService}) : super(const FileListLoadState()) {
on<FileListLoadPathEvent>(_FileListLoadState);
}
Future<void> _FileListLoadState(
FileListLoadPathEvent event, Emitter<FileListLoadState> emit) async {
if(event.repoName.isEmpty) return;
emit(FileListLoadState(status: FileLoadStatus.loading));
final fileListResult = await giteaService.getFolder(
event.repoName.split('/')[0], event.repoName.split('/')[1], event.path);
return emit(FileListLoadState(status: FileLoadStatus.success,files: fileListResult,path: event.path,repoFullname: event.repoName));
}
}

View file

@ -0,0 +1,15 @@
part of 'file_list_load_bloc.dart';
abstract class FileListLoadEvent extends Equatable {
const FileListLoadEvent();
}
class FileListLoadPathEvent extends FileListLoadEvent {
final repoName;
final path;
const FileListLoadPathEvent(this.path, this.repoName);
@override
List<Object?> get props => [path,repoName];
}

View file

@ -0,0 +1,15 @@
part of 'file_list_load_bloc.dart';
enum FileLoadStatus { loading, success,failure }
class FileListLoadState extends Equatable {
const FileListLoadState({this.path="", this.repoFullname ="", this.error_message = null, this.files= const <RepoFile>[],this.status = FileLoadStatus.loading});
final FileLoadStatus status;
final String path;
final String repoFullname;
final String? error_message;
final List<RepoFile> files;
@override
List<Object> get props => [path,repoFullname];
}

View file

@ -2,7 +2,9 @@ import 'dart:convert';
import 'package:badges/badges.dart'; import 'package:badges/badges.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_markdown/flutter_markdown.dart'; import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:gitea_client/cubit/file_list_load_bloc.dart';
import 'package:gitea_client/model/repository.dart'; import 'package:gitea_client/model/repository.dart';
import 'package:gitea_client/model/user.dart'; import 'package:gitea_client/model/user.dart';
import 'package:gitea_client/service/gitea_service.dart'; import 'package:gitea_client/service/gitea_service.dart';
@ -147,13 +149,18 @@ class _RepoHome extends State<RepoHome> {
width: (media.width > 600) width: (media.width > 600)
? media.width * 0.6 ? media.width * 0.6
: media.width * 0.9, : media.width * 0.9,
height: media.height -
height: media.height - padding.top - padding.bottom -kToolbarHeight -kBottomNavigationBarHeight-30, padding.top -
padding.bottom -
kToolbarHeight -
kBottomNavigationBarHeight -
30,
child: Column( child: Column(
children: [ children: [
Expanded(child: Markdown(selectable: true, data: content)), Expanded(
], child: Markdown(selectable: true, data: content)),
)), ],
)),
); );
} else { } else {
return const Center( return const Center(
@ -201,73 +208,72 @@ class _RepoFiles extends State<RepoFiles> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return FutureBuilder<List<RepoFile>>( return BlocProvider(
future: readmeRequest, create: (context) {
builder: (context, snapshot) { return FileListLoadBloc(
if (snapshot.hasError) { giteaService: GiteaService(apiAccess: widget.user.apiAccess))
return Center( ..add(FileListLoadPathEvent("/", widget.repo.fullName!));
child: Column( },
children: [ child: BlocBuilder<FileListLoadBloc, FileListLoadState>(
Text("No Readme file found!", builder: (context, state) {
style: Theme.of(context).textTheme.headline6), switch (state.status) {
], case FileLoadStatus.failure:
), String error_message = state.error_message!;
); return Center(child: Text('failed to fetch $error_message'));
} else if (snapshot.hasData) { case FileLoadStatus.success:
var files = (path == "/") if (state.files.isEmpty) {
? [] return const Center(
: [ child: Text('No files in current directory'));
RepoFile( }
name: "..", var files = (state.path == "/")
path: path, ? []
sha: "", : [
type: "dir", RepoFile(
size: 0, name: "..",
url: "", path: state.path,
htmlUrl: "", sha: "",
gitUrl: "", type: "dir",
lLinks: Links(self: "", git: "", html: "")) size: 0,
]; url: "",
files.addAll(snapshot.data!); htmlUrl: "",
gitUrl: "",
return ListView.builder( lLinks: Links(self: "", git: "", html: ""))
itemBuilder: (BuildContext context, int index) { ];
return FileListItem( files.addAll(state.files);
return ListView.builder(
itemBuilder: (BuildContext context, int index) {
return FileListItem(
file: files[index], file: files[index],
onTap: () => { onTap: () {
if (files[index].type == "dir") if (files[index].type == "dir") {
setState(() { String prev = files[index].path.contains("/")
String prev = files[index].path.contains("/") ? files[index].path.substring(
? files[index].path.substring( 0, files[index].path.lastIndexOf('/'))
0, files[index].path.lastIndexOf('/')) : "/";
: "/"; path = (files[index].name == "..")
path = (files[index].name == "..") ? prev
? prev : files[index].path;
: files[index].path; context.read<FileListLoadBloc>().add(FileListLoadPathEvent(path,widget.repo.fullName!));
readmeRequest = null; } else {
readmeRequest = giteaService.getFolder( Navigator.of(context).pushNamed("/fileview",
widget.repo.owner.username!, arguments: CodePageData(
widget.repo.name, files[index].path,
path); widget.repo.name,
}) widget.repo.owner.username!,
else widget.user));
Navigator.of(context).pushNamed("/fileview", }
arguments: CodePageData( },
files[index].path, );
widget.repo.name, },
widget.repo.owner.username!, itemCount: files.length,
widget.user)) controller: _scrollController,
}); );
}, default:
itemCount: files.length, return const Center(child: CircularProgressIndicator());
controller: _scrollController,
);
} else {
return const Center(
child: CircularProgressIndicator(),
);
} }
}); },
),
);
} }
} }
@ -306,7 +312,7 @@ class _RepoIssues extends State<RepoIssues> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return DefaultTabController( return DefaultTabController(
length: 2, length: 2,
child:DefaultTabController( child: DefaultTabController(
length: 2, length: 2,
child: Scaffold( child: Scaffold(
appBar: PreferredSize( appBar: PreferredSize(
@ -328,10 +334,10 @@ class _RepoIssues extends State<RepoIssues> {
body: TabBarView( body: TabBarView(
children: <Widget>[ children: <Widget>[
Column( Column(
children: const [Text("Lunches Page")], children: const [Text("Open Issues")],
), ),
Column( Column(
children: const [ Text("Cart Page")], children: const [Text("Closed Issues")],
) )
], ],
), ),