remove builds from db if pkg is deleted
fix apibuilder interval refreshes refreshing widgets not visible
This commit is contained in:
		
							
								
								
									
										82
									
								
								frontend/lib/components/api/APIBuilder.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								frontend/lib/components/api/APIBuilder.dart
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,82 @@
 | 
			
		||||
import 'dart:async';
 | 
			
		||||
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:provider/provider.dart';
 | 
			
		||||
import 'package:visibility_detector/visibility_detector.dart';
 | 
			
		||||
 | 
			
		||||
import '../../providers/BaseProvider.dart';
 | 
			
		||||
 | 
			
		||||
class APIBuilder<T extends BaseProvider, K, DTO> extends StatefulWidget {
 | 
			
		||||
  const APIBuilder(
 | 
			
		||||
      {super.key,
 | 
			
		||||
      required this.onLoad,
 | 
			
		||||
      required this.onData,
 | 
			
		||||
      this.interval,
 | 
			
		||||
      this.dto});
 | 
			
		||||
 | 
			
		||||
  final DTO? dto;
 | 
			
		||||
  final Duration? interval;
 | 
			
		||||
  final Widget Function() onLoad;
 | 
			
		||||
  final Widget Function(K t) onData;
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  State<APIBuilder<T, K, DTO>> createState() => _APIBuilderState<T, K, DTO>();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class _APIBuilderState<T extends BaseProvider, K, DTO>
 | 
			
		||||
    extends State<APIBuilder<T, K, DTO>> {
 | 
			
		||||
  Timer? timer;
 | 
			
		||||
  bool visible = true;
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  void initState() {
 | 
			
		||||
    super.initState();
 | 
			
		||||
    Provider.of<T>(context, listen: false).loadFuture(context, dto: widget.dto);
 | 
			
		||||
 | 
			
		||||
    if (widget.interval != null) {
 | 
			
		||||
      timer = Timer.periodic(widget.interval!, (Timer t) {
 | 
			
		||||
        if (visible) {
 | 
			
		||||
          Provider.of<T>(context, listen: false)
 | 
			
		||||
              .refresh(context, dto: widget.dto);
 | 
			
		||||
        }
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  void dispose() {
 | 
			
		||||
    super.dispose();
 | 
			
		||||
    timer?.cancel();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  Widget build(BuildContext context) {
 | 
			
		||||
    final Future<K> fut = Provider.of<T>(context).data as Future<K>;
 | 
			
		||||
 | 
			
		||||
    return VisibilityDetector(
 | 
			
		||||
      key: widget.key ?? const Key("APIBuilder"),
 | 
			
		||||
      onVisibilityChanged: (visibilityInfo) {
 | 
			
		||||
        var visiblePercentage = visibilityInfo.visibleFraction * 100;
 | 
			
		||||
 | 
			
		||||
        if (mounted) {
 | 
			
		||||
          setState(() {
 | 
			
		||||
            visible = visiblePercentage != 0;
 | 
			
		||||
          });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        debugPrint(
 | 
			
		||||
            'Widget ${visibilityInfo.key} is ${visiblePercentage}% visible');
 | 
			
		||||
      },
 | 
			
		||||
      child: FutureBuilder<K>(
 | 
			
		||||
        future: fut,
 | 
			
		||||
        builder: (context, snapshot) {
 | 
			
		||||
          if (snapshot.hasData) {
 | 
			
		||||
            return widget.onData(snapshot.data!);
 | 
			
		||||
          } else {
 | 
			
		||||
            return widget.onLoad();
 | 
			
		||||
          }
 | 
			
		||||
        },
 | 
			
		||||
      ),
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -4,7 +4,7 @@ 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/components/api/APIBuilder.dart';
 | 
			
		||||
import 'package:aurcache/providers/builds_provider.dart';
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:go_router/go_router.dart';
 | 
			
		||||
@@ -41,6 +41,7 @@ class _RecentBuildsState extends State<RecentBuilds> {
 | 
			
		||||
          SizedBox(
 | 
			
		||||
            width: double.infinity,
 | 
			
		||||
            child: APIBuilder<BuildsProvider, List<Build>, BuildsDTO>(
 | 
			
		||||
              key: const Key("Builds on dashboard"),
 | 
			
		||||
              dto: BuildsDTO(limit: 10),
 | 
			
		||||
              interval: const Duration(seconds: 10),
 | 
			
		||||
              onLoad: () => const Text("no data"),
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
import 'dart:async';
 | 
			
		||||
 | 
			
		||||
import 'package:aurcache/api/packages.dart';
 | 
			
		||||
import 'package:aurcache/providers/APIBuilder.dart';
 | 
			
		||||
import 'package:aurcache/components/api/APIBuilder.dart';
 | 
			
		||||
import 'package:aurcache/providers/packages_provider.dart';
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:go_router/go_router.dart';
 | 
			
		||||
@@ -44,7 +44,7 @@ class _YourPackagesState extends State<YourPackages> {
 | 
			
		||||
            child: SizedBox(
 | 
			
		||||
              width: double.infinity,
 | 
			
		||||
              child: APIBuilder<PackagesProvider, List<Package>, Object>(
 | 
			
		||||
                key: GlobalKey(),
 | 
			
		||||
                key: Key("Packages on dashboard"),
 | 
			
		||||
                interval: const Duration(seconds: 10),
 | 
			
		||||
                onData: (data) {
 | 
			
		||||
                  return DataTable(
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
 | 
			
		||||
import '../utils/responsive.dart';
 | 
			
		||||
import '../screens/dashboard_screen.dart';
 | 
			
		||||
import '../../utils/responsive.dart';
 | 
			
		||||
import '../../screens/dashboard_screen.dart';
 | 
			
		||||
import 'side_menu.dart';
 | 
			
		||||
 | 
			
		||||
class MenuShell extends StatelessWidget {
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
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/components/routing/menu_shell.dart';
 | 
			
		||||
import 'package:aurcache/screens/package_screen.dart';
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:go_router/go_router.dart';
 | 
			
		||||
@@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:flutter_svg/flutter_svg.dart';
 | 
			
		||||
import 'package:go_router/go_router.dart';
 | 
			
		||||
 | 
			
		||||
import '../constants/color_constants.dart';
 | 
			
		||||
import '../../constants/color_constants.dart';
 | 
			
		||||
 | 
			
		||||
class SideMenu extends StatelessWidget {
 | 
			
		||||
  const SideMenu({
 | 
			
		||||
@@ -86,7 +86,7 @@ class DrawerListTile extends StatelessWidget {
 | 
			
		||||
      ),
 | 
			
		||||
      title: Text(
 | 
			
		||||
        title,
 | 
			
		||||
        style: TextStyle(color: Colors.white54),
 | 
			
		||||
        style: const TextStyle(color: Colors.white54),
 | 
			
		||||
      ),
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
		Reference in New Issue
	
	Block a user