add apibuilder widget to make api callsmore straightforward and data updateable from everywhere through providers
This commit is contained in:
95
frontend/lib/components/build_output.dart
Normal file
95
frontend/lib/components/build_output.dart
Normal file
@ -0,0 +1,95 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:aurcache/api/builds.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../api/API.dart';
|
||||
import '../models/build.dart';
|
||||
|
||||
class BuildOutput extends StatefulWidget {
|
||||
const BuildOutput({super.key, required this.build});
|
||||
|
||||
final Build build;
|
||||
|
||||
@override
|
||||
State<BuildOutput> createState() => _BuildOutputState();
|
||||
}
|
||||
|
||||
class _BuildOutputState extends State<BuildOutput> {
|
||||
late Future<String> initialOutput;
|
||||
|
||||
String output = "";
|
||||
Timer? outputTimer;
|
||||
final scrollController = ScrollController();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Expanded(
|
||||
flex: 1,
|
||||
child: SingleChildScrollView(
|
||||
controller: scrollController,
|
||||
scrollDirection: Axis.vertical, //.horizontal
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(left: 30, right: 15),
|
||||
child: Text(
|
||||
output,
|
||||
style: const TextStyle(
|
||||
fontSize: 16.0,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
initOutputLoader();
|
||||
}
|
||||
|
||||
void initOutputLoader() {
|
||||
initialOutput = API.getOutput(buildID: widget.build.id);
|
||||
initialOutput.then((value) {
|
||||
setState(() {
|
||||
output = value;
|
||||
});
|
||||
_scrollToBottom();
|
||||
});
|
||||
|
||||
// poll new output only if not finished
|
||||
if (widget.build.status == 0) {
|
||||
outputTimer = Timer.periodic(const Duration(seconds: 3), (Timer t) async {
|
||||
print("refreshing output");
|
||||
final value = await API.getOutput(
|
||||
buildID: widget.build.id, line: output.split("\n").length);
|
||||
setState(() {
|
||||
output += value;
|
||||
});
|
||||
|
||||
_scrollToBottom();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
outputTimer?.cancel();
|
||||
}
|
||||
}
|
53
frontend/lib/components/builds_table.dart
Normal file
53
frontend/lib/components/builds_table.dart
Normal file
@ -0,0 +1,53 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../constants/color_constants.dart';
|
||||
import '../models/build.dart';
|
||||
import 'dashboard/your_packages.dart';
|
||||
|
||||
class BuildsTable extends StatelessWidget {
|
||||
const BuildsTable({super.key, required this.data});
|
||||
final List<Build> data;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return DataTable(
|
||||
horizontalMargin: 0,
|
||||
columnSpacing: defaultPadding,
|
||||
columns: const [
|
||||
DataColumn(
|
||||
label: Text("Build ID"),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text("Package Name"),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text("Version"),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text("Status"),
|
||||
),
|
||||
],
|
||||
rows: data.map((e) => buildDataRow(context, e)).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
DataRow buildDataRow(BuildContext context, Build build) {
|
||||
return DataRow(
|
||||
cells: [
|
||||
DataCell(Text(build.id.toString())),
|
||||
DataCell(Text(build.pkg_name)),
|
||||
DataCell(Text(build.version)),
|
||||
DataCell(IconButton(
|
||||
icon: Icon(
|
||||
switchSuccessIcon(build.status),
|
||||
color: switchSuccessColor(build.status),
|
||||
),
|
||||
onPressed: () {
|
||||
context.push("/build/${build.id}");
|
||||
},
|
||||
)),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
41
frontend/lib/components/confirm_popup.dart
Normal file
41
frontend/lib/components/confirm_popup.dart
Normal file
@ -0,0 +1,41 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
Future<bool> showDeleteConfirmationDialog(BuildContext context) async {
|
||||
return (await showDialog<bool>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (BuildContext context) {
|
||||
return Stack(
|
||||
children: <Widget>[
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
Navigator.of(context).pop(false); // Dismiss dialog on outside tap
|
||||
},
|
||||
child: Container(
|
||||
color: Colors.black.withOpacity(0.5), // Adjust opacity for blur
|
||||
),
|
||||
),
|
||||
// Delete confirmation dialog
|
||||
AlertDialog(
|
||||
title: Text('Confirm Delete'),
|
||||
content: Text('Are you sure you want to delete this item?'),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop(true);
|
||||
},
|
||||
child: Text('Yes, Delete'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop(false); // Dismiss dialog
|
||||
},
|
||||
child: Text('Cancel'),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
))!;
|
||||
}
|
@ -1,10 +1,14 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:aurcache/api/builds.dart';
|
||||
import 'package:aurcache/components/builds_table.dart';
|
||||
import 'package:aurcache/models/build.dart';
|
||||
import 'package:aurcache/components/dashboard/your_packages.dart';
|
||||
import 'package:aurcache/providers/APIBuilder.dart';
|
||||
import 'package:aurcache/providers/builds_provider.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../api/API.dart';
|
||||
import '../../constants/color_constants.dart';
|
||||
@ -19,27 +23,6 @@ class RecentBuilds extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _RecentBuildsState extends State<RecentBuilds> {
|
||||
late Future<List<Build>> dataFuture;
|
||||
Timer? timer;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
dataFuture = API.listAllBuilds();
|
||||
|
||||
timer = Timer.periodic(
|
||||
const Duration(seconds: 10),
|
||||
(Timer t) => setState(() {
|
||||
dataFuture = API.listAllBuilds();
|
||||
}));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
timer?.cancel();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
@ -57,57 +40,26 @@ class _RecentBuildsState extends State<RecentBuilds> {
|
||||
),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: FutureBuilder(
|
||||
future: dataFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return DataTable(
|
||||
horizontalMargin: 0,
|
||||
columnSpacing: defaultPadding,
|
||||
columns: const [
|
||||
DataColumn(
|
||||
label: Text("Build ID"),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text("Package Name"),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text("Version"),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text("Status"),
|
||||
),
|
||||
],
|
||||
rows: snapshot.data!
|
||||
.map((e) => recentUserDataRow(e))
|
||||
.toList(),
|
||||
);
|
||||
} else {
|
||||
return const Text("no data");
|
||||
}
|
||||
}),
|
||||
child: APIBuilder<BuildsProvider, List<Build>, BuildsDTO>(
|
||||
dto: BuildsDTO(limit: 10),
|
||||
interval: const Duration(seconds: 10),
|
||||
onLoad: () => const Text("no data"),
|
||||
onData: (t) {
|
||||
return BuildsTable(data: t);
|
||||
},
|
||||
),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.push("/builds");
|
||||
},
|
||||
child: Text(
|
||||
"List all Builds",
|
||||
style: TextStyle(color: Colors.white.withOpacity(0.8)),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
DataRow recentUserDataRow(Build build) {
|
||||
return DataRow(
|
||||
cells: [
|
||||
DataCell(Text(build.id.toString())),
|
||||
DataCell(Text(build.pkg_name)),
|
||||
DataCell(Text(build.version)),
|
||||
DataCell(IconButton(
|
||||
icon: Icon(
|
||||
switchSuccessIcon(build.status),
|
||||
color: switchSuccessColor(build.status),
|
||||
),
|
||||
onPressed: () {
|
||||
context.push("/build/${build.id}");
|
||||
},
|
||||
)),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,13 @@
|
||||
import 'package:aurcache/api/packages.dart';
|
||||
import 'package:aurcache/providers/builds_provider.dart';
|
||||
import 'package:aurcache/providers/stats_provider.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_svg/svg.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../api/API.dart';
|
||||
import '../../constants/color_constants.dart';
|
||||
import '../../providers/packages_provider.dart';
|
||||
|
||||
class SearchField extends StatelessWidget {
|
||||
SearchField({
|
||||
@ -25,9 +29,14 @@ class SearchField extends StatelessWidget {
|
||||
borderRadius: BorderRadius.all(Radius.circular(10)),
|
||||
),
|
||||
suffixIcon: InkWell(
|
||||
onTap: () {
|
||||
onTap: () async {
|
||||
// todo this is only temporary -> add this to a proper page
|
||||
API.addPackage(name: controller.text);
|
||||
await API.addPackage(name: controller.text, force: true);
|
||||
Provider.of<PackagesProvider>(context, listen: false)
|
||||
.refresh(context);
|
||||
Provider.of<BuildsProvider>(context, listen: false)
|
||||
.refresh(context);
|
||||
Provider.of<StatsProvider>(context, listen: false).refresh(context);
|
||||
},
|
||||
child: Container(
|
||||
padding: EdgeInsets.all(defaultPadding * 0.75),
|
||||
|
@ -1,11 +1,18 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:aurcache/api/packages.dart';
|
||||
import 'package:aurcache/providers/APIBuilder.dart';
|
||||
import 'package:aurcache/providers/packages_provider.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../api/API.dart';
|
||||
import '../../constants/color_constants.dart';
|
||||
import '../../models/package.dart';
|
||||
import '../../providers/builds_provider.dart';
|
||||
import '../../providers/stats_provider.dart';
|
||||
import '../confirm_popup.dart';
|
||||
|
||||
class YourPackages extends StatefulWidget {
|
||||
const YourPackages({
|
||||
@ -17,27 +24,6 @@ class YourPackages extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _YourPackagesState extends State<YourPackages> {
|
||||
late Future<List<Package>> dataFuture;
|
||||
Timer? timer;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
dataFuture = API.listPackages();
|
||||
|
||||
timer = Timer.periodic(
|
||||
const Duration(seconds: 10),
|
||||
(Timer t) => setState(() {
|
||||
dataFuture = API.listPackages();
|
||||
}));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
timer?.cancel();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
@ -57,10 +43,11 @@ class _YourPackagesState extends State<YourPackages> {
|
||||
//scrollDirection: Axis.horizontal,
|
||||
child: SizedBox(
|
||||
width: double.infinity,
|
||||
child: FutureBuilder(
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return DataTable(
|
||||
child: APIBuilder<PackagesProvider, List<Package>, Object>(
|
||||
key: GlobalKey(),
|
||||
interval: const Duration(seconds: 10),
|
||||
onData: (data) {
|
||||
return DataTable(
|
||||
horizontalMargin: 0,
|
||||
columnSpacing: defaultPadding,
|
||||
columns: const [
|
||||
@ -80,15 +67,11 @@ class _YourPackagesState extends State<YourPackages> {
|
||||
label: Text("Action"),
|
||||
),
|
||||
],
|
||||
rows: snapshot.data!
|
||||
rows: data
|
||||
.map((e) => buildDataRow(e))
|
||||
.toList(growable: false),
|
||||
);
|
||||
} else {
|
||||
return const Text("No data");
|
||||
}
|
||||
.toList(growable: false));
|
||||
},
|
||||
future: dataFuture,
|
||||
onLoad: () => const Text("No data"),
|
||||
),
|
||||
),
|
||||
),
|
||||
@ -117,7 +100,9 @@ class _YourPackagesState extends State<YourPackages> {
|
||||
children: [
|
||||
TextButton(
|
||||
child: const Text('View', style: TextStyle(color: greenColor)),
|
||||
onPressed: () {},
|
||||
onPressed: () {
|
||||
context.push("/package/${package.id}");
|
||||
},
|
||||
),
|
||||
const SizedBox(
|
||||
width: 6,
|
||||
@ -125,7 +110,21 @@ class _YourPackagesState extends State<YourPackages> {
|
||||
TextButton(
|
||||
child: const Text("Delete",
|
||||
style: TextStyle(color: Colors.redAccent)),
|
||||
onPressed: () {},
|
||||
onPressed: () async {
|
||||
final confirmResult =
|
||||
await showDeleteConfirmationDialog(context);
|
||||
if (!confirmResult) return;
|
||||
|
||||
final succ = await API.deletePackage(package.id);
|
||||
if (succ) {
|
||||
Provider.of<PackagesProvider>(context, listen: false)
|
||||
.refresh(context);
|
||||
Provider.of<BuildsProvider>(context, listen: false)
|
||||
.refresh(context);
|
||||
Provider.of<StatsProvider>(context, listen: false)
|
||||
.refresh(context);
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
|
@ -1,6 +1,8 @@
|
||||
import 'package:aurcache/screens/build_screen.dart';
|
||||
import 'package:aurcache/screens/builds_screen.dart';
|
||||
import 'package:aurcache/screens/dashboard_screen.dart';
|
||||
import 'package:aurcache/components/menu_shell.dart';
|
||||
import 'package:aurcache/screens/package_screen.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
@ -21,15 +23,24 @@ final appRouter = GoRouter(
|
||||
GoRoute(
|
||||
path: '/',
|
||||
builder: (context, state) => DashboardScreen(),
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: 'build/:id',
|
||||
builder: (context, state) {
|
||||
final id = int.parse(state.pathParameters['id']!);
|
||||
return BuildScreen(buildID: id);
|
||||
},
|
||||
),
|
||||
]
|
||||
),
|
||||
GoRoute(
|
||||
path: '/build/:id',
|
||||
builder: (context, state) {
|
||||
final id = int.parse(state.pathParameters['id']!);
|
||||
return BuildScreen(buildID: id);
|
||||
},
|
||||
),
|
||||
GoRoute(
|
||||
path: '/builds',
|
||||
builder: (context, state) => const BuildsScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/package/:id',
|
||||
builder: (context, state) {
|
||||
final id = int.parse(state.pathParameters['id']!);
|
||||
return PackageScreen(pkgID: id);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
|
Reference in New Issue
Block a user