add buildlogprovider to handle scrolling from outside widget

This commit is contained in:
lukas-heiligenbrunner 2024-02-16 22:37:35 +01:00 committed by Lukas-Heiligenbrunner
parent 7ea241112c
commit 598159a2bc
19 changed files with 142 additions and 96 deletions

View File

@ -3,7 +3,7 @@ import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import '../../providers/BaseProvider.dart'; import '../../providers/api/BaseProvider.dart';
class APIBuilder<T extends BaseProvider, K, DTO> extends StatefulWidget { class APIBuilder<T extends BaseProvider, K, DTO> extends StatefulWidget {
const APIBuilder( const APIBuilder(

View File

@ -2,9 +2,11 @@ import 'dart:async';
import 'package:aurcache/api/builds.dart'; import 'package:aurcache/api/builds.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../api/API.dart'; import '../api/API.dart';
import '../models/build.dart'; import '../models/build.dart';
import '../providers/build_log_provider.dart';
class BuildOutput extends StatefulWidget { class BuildOutput extends StatefulWidget {
const BuildOutput({super.key, required this.build}); const BuildOutput({super.key, required this.build});
@ -20,14 +22,15 @@ class _BuildOutputState extends State<BuildOutput> {
String output = ""; String output = "";
Timer? outputTimer; Timer? outputTimer;
final scrollController = ScrollController();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final sc =
Provider.of<BuildLogProvider>(context, listen: false).scrollController;
return Expanded( return Expanded(
flex: 1, flex: 1,
child: SingleChildScrollView( child: SingleChildScrollView(
controller: scrollController, controller: sc,
scrollDirection: Axis.vertical, //.horizontal scrollDirection: Axis.vertical, //.horizontal
child: Padding( child: Padding(
padding: const EdgeInsets.only(left: 30, right: 15), padding: const EdgeInsets.only(left: 30, right: 15),
@ -57,38 +60,28 @@ class _BuildOutputState extends State<BuildOutput> {
setState(() { setState(() {
output = value; output = value;
}); });
_scrollToBottom(); Provider.of<BuildLogProvider>(context, listen: false).go_to_bottom();
}); });
// poll new output only if not finished // poll new output only if not finished
if (widget.build.status == 0) { if (widget.build.status == 0) {
outputTimer = Timer.periodic(const Duration(seconds: 3), (Timer t) async { outputTimer = Timer.periodic(const Duration(seconds: 3), (Timer t) async {
print("refreshing output"); final follow =
final value = await API.getOutput( Provider.of<BuildLogProvider>(context, listen: false).followLog;
buildID: widget.build.id, line: output.split("\n").length); if (follow) {
setState(() { print("refreshing output");
output += value; final value = await API.getOutput(
}); buildID: widget.build.id, line: output.split("\n").length);
setState(() {
output += value;
});
_scrollToBottom(); Provider.of<BuildLogProvider>(context, listen: false).go_to_bottom();
}
}); });
} }
} }
void _scrollToBottom() {
WidgetsBinding.instance.addPostFrameCallback((_) {
// scroll to bottom
final scrollPosition = scrollController.position;
if (scrollPosition.viewportDimension < scrollPosition.maxScrollExtent) {
scrollController.animateTo(
scrollPosition.maxScrollExtent,
duration: const Duration(milliseconds: 200),
curve: Curves.easeOut,
);
}
});
}
@override @override
void dispose() { void dispose() {
super.dispose(); super.dispose();

View File

@ -1,7 +1,7 @@
import 'package:aurcache/components/builds_table.dart'; import 'package:aurcache/components/builds_table.dart';
import 'package:aurcache/models/build.dart'; import 'package:aurcache/models/build.dart';
import 'package:aurcache/components/api/APIBuilder.dart'; import 'package:aurcache/components/api/APIBuilder.dart';
import 'package:aurcache/providers/builds_provider.dart'; import 'package:aurcache/providers/api/builds_provider.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import '../../constants/color_constants.dart'; import '../../constants/color_constants.dart';

View File

@ -1,6 +1,6 @@
import 'package:aurcache/components/api/APIBuilder.dart'; import 'package:aurcache/components/api/APIBuilder.dart';
import 'package:aurcache/components/packages_table.dart'; import 'package:aurcache/components/packages_table.dart';
import 'package:aurcache/providers/packages_provider.dart'; import 'package:aurcache/providers/api/packages_provider.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import '../../constants/color_constants.dart'; import '../../constants/color_constants.dart';

View File

@ -6,9 +6,9 @@ import 'package:provider/provider.dart';
import '../api/API.dart'; import '../api/API.dart';
import '../constants/color_constants.dart'; import '../constants/color_constants.dart';
import '../models/package.dart'; import '../models/package.dart';
import '../providers/builds_provider.dart'; import '../providers/api/builds_provider.dart';
import '../providers/packages_provider.dart'; import '../providers/api/packages_provider.dart';
import '../providers/stats_provider.dart'; import '../providers/api/stats_provider.dart';
import 'confirm_popup.dart'; import 'confirm_popup.dart';
import 'dashboard/your_packages.dart'; import 'dashboard/your_packages.dart';

View File

@ -1,7 +1,7 @@
import 'package:aurcache/api/aur.dart'; import 'package:aurcache/api/aur.dart';
import 'package:aurcache/models/aur_package.dart'; import 'package:aurcache/models/aur_package.dart';
import '../api/API.dart'; import '../../api/API.dart';
import 'BaseProvider.dart'; import 'BaseProvider.dart';
class AurSearchDTO { class AurSearchDTO {

View File

@ -1,7 +1,7 @@
import 'package:aurcache/api/builds.dart'; import 'package:aurcache/api/builds.dart';
import '../api/API.dart'; import '../../api/API.dart';
import '../models/build.dart'; import '../../models/build.dart';
import 'BaseProvider.dart'; import 'BaseProvider.dart';
class BuildDTO { class BuildDTO {

View File

@ -1,7 +1,7 @@
import 'package:aurcache/api/builds.dart'; import 'package:aurcache/api/builds.dart';
import '../api/API.dart'; import '../../api/API.dart';
import '../models/build.dart'; import '../../models/build.dart';
import 'BaseProvider.dart'; import 'BaseProvider.dart';
class BuildsDTO { class BuildsDTO {

View File

@ -1,7 +1,7 @@
import 'package:aurcache/api/packages.dart'; import 'package:aurcache/api/packages.dart';
import '../api/API.dart'; import '../../api/API.dart';
import '../models/package.dart'; import '../../models/package.dart';
import 'BaseProvider.dart'; import 'BaseProvider.dart';
class PackageDTO { class PackageDTO {

View File

@ -1,8 +1,8 @@
import 'package:aurcache/api/packages.dart'; import 'package:aurcache/api/packages.dart';
import 'package:aurcache/providers/BaseProvider.dart'; import 'package:aurcache/providers/api/BaseProvider.dart';
import '../api/API.dart'; import '../../api/API.dart';
import '../models/package.dart'; import '../../models/package.dart';
class PackagesDTO { class PackagesDTO {
final int limit; final int limit;

View File

@ -1,6 +1,6 @@
import 'package:aurcache/api/statistics.dart'; import 'package:aurcache/api/statistics.dart';
import '../api/API.dart'; import '../../api/API.dart';
import 'BaseProvider.dart'; import 'BaseProvider.dart';
class StatsProvider extends BaseProvider { class StatsProvider extends BaseProvider {

View File

@ -0,0 +1,39 @@
import 'package:flutter/widgets.dart';
class BuildLogProvider with ChangeNotifier {
final scrollController = ScrollController();
bool followLog = true;
set follow_log(bool value) {
followLog = value;
notifyListeners();
}
void go_to_bottom() {
_scrollToBottom();
}
void go_to_top() {
final scrollPosition = scrollController.position;
scrollController.animateTo(
scrollPosition.minScrollExtent,
duration: const Duration(milliseconds: 200),
curve: Curves.easeOut,
);
}
void _scrollToBottom() {
WidgetsBinding.instance.addPostFrameCallback((_) {
// scroll to bottom
final scrollPosition = scrollController.position;
if (scrollPosition.viewportDimension < scrollPosition.maxScrollExtent) {
scrollController.animateTo(
scrollPosition.maxScrollExtent,
duration: const Duration(milliseconds: 200),
curve: Curves.easeOut,
);
}
});
}
}

View File

@ -2,13 +2,13 @@ import 'dart:async';
import 'package:aurcache/components/aur_search_table.dart'; import 'package:aurcache/components/aur_search_table.dart';
import 'package:aurcache/models/aur_package.dart'; import 'package:aurcache/models/aur_package.dart';
import 'package:aurcache/providers/aur_search_provider.dart'; import 'package:aurcache/providers/api/aur_search_provider.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import '../components/api/APIBuilder.dart'; import '../components/api/APIBuilder.dart';
import '../constants/color_constants.dart'; import '../constants/color_constants.dart';
import '../providers/packages_provider.dart'; import '../providers/api/packages_provider.dart';
class AurScreen extends StatefulWidget { class AurScreen extends StatefulWidget {
const AurScreen({super.key, this.initalQuery}); const AurScreen({super.key, this.initalQuery});

View File

@ -1,7 +1,7 @@
import 'package:aurcache/components/build_output.dart'; import 'package:aurcache/components/build_output.dart';
import 'package:aurcache/models/build.dart'; import 'package:aurcache/models/build.dart';
import 'package:aurcache/components/api/APIBuilder.dart'; import 'package:aurcache/components/api/APIBuilder.dart';
import 'package:aurcache/providers/build_provider.dart'; import 'package:aurcache/providers/build_log_provider.dart';
import 'package:aurcache/utils/time_formatter.dart'; import 'package:aurcache/utils/time_formatter.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
@ -11,6 +11,7 @@ import '../components/confirm_popup.dart';
import '../components/dashboard/chart_card.dart'; import '../components/dashboard/chart_card.dart';
import '../components/dashboard/your_packages.dart'; import '../components/dashboard/your_packages.dart';
import '../constants/color_constants.dart'; import '../constants/color_constants.dart';
import '../providers/api/build_provider.dart';
class BuildScreen extends StatefulWidget { class BuildScreen extends StatefulWidget {
const BuildScreen({super.key, required this.buildID}); const BuildScreen({super.key, required this.buildID});
@ -22,44 +23,50 @@ class BuildScreen extends StatefulWidget {
} }
class _BuildScreenState extends State<BuildScreen> { class _BuildScreenState extends State<BuildScreen> {
bool scrollFollowActive = true;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
body: MultiProvider( body: MultiProvider(
providers: [ providers: [
ChangeNotifierProvider<BuildProvider>(create: (_) => BuildProvider()), ChangeNotifierProvider<BuildProvider>(create: (_) => BuildProvider()),
ChangeNotifierProvider<BuildLogProvider>(
create: (_) => BuildLogProvider()),
], ],
child: APIBuilder<BuildProvider, Build, BuildDTO>( builder: (context, child) {
key: const Key("Build on seperate page"), return APIBuilder<BuildProvider, Build, BuildDTO>(
dto: BuildDTO(buildID: widget.buildID), key: const Key("Build on seperate page"),
interval: const Duration(seconds: 10), dto: BuildDTO(buildID: widget.buildID),
onLoad: () => const Text("loading"), interval: const Duration(seconds: 10),
onData: (buildData) { onLoad: () => const Text("loading"),
return Row( onData: (buildData) {
mainAxisAlignment: MainAxisAlignment.spaceBetween, return Row(
children: [ mainAxisAlignment: MainAxisAlignment.spaceBetween,
Expanded( children: [
child: Column( Expanded(
crossAxisAlignment: CrossAxisAlignment.start, child: Column(
mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ mainAxisAlignment: MainAxisAlignment.start,
_buildTopBar(buildData), children: [
const SizedBox( _buildTopBar(buildData, context),
height: 15, const SizedBox(
), height: 15,
_buildPage(buildData) ),
], _buildPage(buildData)
],
),
), ),
), _buildSideBar(),
_buildSideBar(), ],
], );
); });
}), },
), ),
); );
} }
Widget _buildTopBar(Build buildData) { Widget _buildTopBar(Build buildData, BuildContext context) {
final start_time = final start_time =
DateTime.fromMillisecondsSinceEpoch((buildData.start_time ?? 0) * 1000); DateTime.fromMillisecondsSinceEpoch((buildData.start_time ?? 0) * 1000);
@ -113,18 +120,32 @@ class _BuildScreenState extends State<BuildScreen> {
Row( Row(
children: [ children: [
IconButton( IconButton(
onPressed: () {}, onPressed: () {
setState(() {
scrollFollowActive = !scrollFollowActive;
Provider.of<BuildLogProvider>(context, listen: false)
.followLog = scrollFollowActive;
});
},
isSelected: scrollFollowActive,
icon: const Icon(Icons.read_more), icon: const Icon(Icons.read_more),
selectedIcon: const Icon(Icons.read_more),
tooltip: "Follow log", tooltip: "Follow log",
), ),
IconButton( IconButton(
onPressed: () {}, onPressed: () {
Provider.of<BuildLogProvider>(context, listen: false)
.go_to_top();
},
icon: const Icon(Icons.vertical_align_top_rounded), icon: const Icon(Icons.vertical_align_top_rounded),
tooltip: "Go to Top", tooltip: "Go to Top",
), ),
IconButton( IconButton(
onPressed: () {}, onPressed: () {
icon: Icon(Icons.vertical_align_bottom_rounded), Provider.of<BuildLogProvider>(context, listen: false)
.go_to_bottom();
},
icon: const Icon(Icons.vertical_align_bottom_rounded),
tooltip: "Go to Bottom", tooltip: "Go to Bottom",
), ),
const SizedBox( const SizedBox(
@ -164,12 +185,11 @@ class _BuildScreenState extends State<BuildScreen> {
ElevatedButton( ElevatedButton(
onPressed: () async { onPressed: () async {
final confirmResult = await showConfirmationDialog( final confirmResult = await showConfirmationDialog(
context, context,
"Delete Build", "Delete Build",
"Are you sure to delete this Package?", "Are you sure to delete this Package?", () async {
() async {}, // todo delete build
() {}, }, null);
);
}, },
child: const Text( child: const Text(
"Delete", "Delete",
@ -181,13 +201,7 @@ class _BuildScreenState extends State<BuildScreen> {
), ),
ElevatedButton( ElevatedButton(
onPressed: () async { onPressed: () async {
final confirmResult = await showConfirmationDialog( // todo api call and page redirect
context,
"Delete Build",
"Are you sure to delete this Package?",
() async {},
() {},
);
}, },
child: const Text( child: const Text(
"Retry", "Retry",

View File

@ -1,6 +1,6 @@
import 'package:aurcache/components/builds_table.dart'; import 'package:aurcache/components/builds_table.dart';
import 'package:aurcache/components/api/APIBuilder.dart'; import 'package:aurcache/components/api/APIBuilder.dart';
import 'package:aurcache/providers/builds_provider.dart'; import 'package:aurcache/providers/api/builds_provider.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import '../constants/color_constants.dart'; import '../constants/color_constants.dart';

View File

@ -1,12 +1,12 @@
import 'package:aurcache/components/api/APIBuilder.dart'; import 'package:aurcache/components/api/APIBuilder.dart';
import 'package:aurcache/providers/stats_provider.dart'; import 'package:aurcache/providers/api/stats_provider.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import '../components/dashboard/header.dart'; import '../components/dashboard/header.dart';
import '../constants/color_constants.dart'; import '../constants/color_constants.dart';
import '../providers/builds_provider.dart'; import '../providers/api/builds_provider.dart';
import '../providers/packages_provider.dart'; import '../providers/api/packages_provider.dart';
import '../utils/responsive.dart'; import '../utils/responsive.dart';
import '../models/stats.dart'; import '../models/stats.dart';
import '../components/dashboard/quick_info_banner.dart'; import '../components/dashboard/quick_info_banner.dart';

View File

@ -1,6 +1,6 @@
import 'package:aurcache/api/packages.dart'; import 'package:aurcache/api/packages.dart';
import 'package:aurcache/components/api/APIBuilder.dart'; import 'package:aurcache/components/api/APIBuilder.dart';
import 'package:aurcache/providers/builds_provider.dart'; import 'package:aurcache/providers/api/builds_provider.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
@ -11,9 +11,9 @@ import '../components/confirm_popup.dart';
import '../constants/color_constants.dart'; import '../constants/color_constants.dart';
import '../models/build.dart'; import '../models/build.dart';
import '../models/package.dart'; import '../models/package.dart';
import '../providers/package_provider.dart'; import '../providers/api/package_provider.dart';
import '../providers/packages_provider.dart'; import '../providers/api/packages_provider.dart';
import '../providers/stats_provider.dart'; import '../providers/api/stats_provider.dart';
class PackageScreen extends StatefulWidget { class PackageScreen extends StatefulWidget {
const PackageScreen({super.key, required this.pkgID}); const PackageScreen({super.key, required this.pkgID});

View File

@ -1,5 +1,5 @@
import 'package:aurcache/components/packages_table.dart'; import 'package:aurcache/components/packages_table.dart';
import 'package:aurcache/providers/packages_provider.dart'; import 'package:aurcache/providers/api/packages_provider.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';