Skip to content

Commit

Permalink
change cpus_slider and ram_slider to be consumer widgets to be able t…
Browse files Browse the repository at this point in the history
…o use WidgetRef
  • Loading branch information
levkropp committed Jan 6, 2025
1 parent 330c8db commit bad74f2
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 60 deletions.
69 changes: 39 additions & 30 deletions src/client/gui/lib/catalogue/launch_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import '../vm_details/mount_points.dart';
import '../vm_details/ram_slider.dart';
import '../vm_details/spec_input.dart';

// Define the daemonInfoProvider here
final daemonInfoProvider = FutureProvider.autoDispose<DaemonInfoReply>((ref) {
return ref.watch(grpcClientProvider).daemonInfo();
});

final launchingImageProvider = StateProvider<ImageInfo>((_) => ImageInfo());

final randomNameProvider = Provider.autoDispose(
Expand Down Expand Up @@ -62,7 +67,6 @@ class _LaunchFormState extends ConsumerState<LaunchForm> {
final randomName = ref.watch(randomNameProvider);
final vmNames = ref.watch(vmNamesProvider);
final deletedVms = ref.watch(deletedVmsProvider);
final daemonInfo = ref.watch(daemonInfoProvider);

final closeButton = IconButton(
icon: const Icon(Icons.close),
Expand All @@ -85,19 +89,36 @@ class _LaunchFormState extends ConsumerState<LaunchForm> {
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w300),
);

final cpusSlider = CpusSlider(
initialValue: defaultCpus,
onSaved: (value) => launchRequest.numCores = value!,
final daemonInfo = ref.watch(daemonInfoProvider);

final cpusSlider = daemonInfo.when(
data: (info) => CpusSlider(
initialValue: defaultCpus,
maxCpus: info.cpus.toInt(), // Pass the max CPUs to the slider
onSaved: (value) => launchRequest.numCores = value!,
),
loading: () => CircularProgressIndicator(),
error: (error, stack) => Text('Error: $error'),
);

final memorySlider = RamSlider(
initialValue: defaultRam,
onSaved: (value) => launchRequest.memSize = '${value!}B',
final memorySlider = daemonInfo.when(
data: (info) => RamSlider(
initialValue: defaultRam,
maxRam: info.memory.toInt(), // Pass the max RAM to the slider
onSaved: (value) => launchRequest.memSize = '${value!}B',
),
loading: () => CircularProgressIndicator(),
error: (error, stack) => Text('Error: $error'),
);

final diskSlider = DiskSlider(
initialValue: defaultDisk,
onSaved: (value) => launchRequest.diskSpace = '${value!}B',
final diskSlider = daemonInfo.when(
data: (info) => DiskSlider(
initialValue: defaultRam,
maxDisk: info.availableSpace.toInt(), // Pass the available space to the slider
onSaved: (value) => launchRequest.memSize = '${value!}B',
),
loading: () => CircularProgressIndicator(),
error: (error, stack) => Text('Error: $error'),
);

final bridgedSwitch = FormField<bool>(
Expand Down Expand Up @@ -160,9 +181,9 @@ class _LaunchFormState extends ConsumerState<LaunchForm> {
final editableMountPoint = EditableMountPoint(
existingTargets: mountRequests.map((r) => r.targetPaths.first.targetPath),
initialSource:
mountRequests.any((r) => r.sourcePath == mpPlatform.homeDirectory)
? null
: mpPlatform.homeDirectory,
mountRequests.any((r) => r.sourcePath == mpPlatform.homeDirectory)
? null
: mpPlatform.homeDirectory,
onSaved: (request) => setState(() {
mountRequests.add(request);
addingMount = false;
Expand All @@ -182,26 +203,14 @@ class _LaunchFormState extends ConsumerState<LaunchForm> {
]),
);

final titleRow = Row(children: [
daemonInfo.when(
data: (info) => Text(
'Available Resources:\n'
'CPUs: ${info.cpus}\n'
'Memory: ${info.memory}\n'
'Storage: ${info.availableSpace}',
style: const TextStyle(fontSize: 16),
),
loading: () => const CircularProgressIndicator(),
error: (err, stack) => Text('Error: $err'),
),
const Spacer(),
closeButton,
]);

final formBody = Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
titleRow,
Row(children: [
const Text('Configure instance', style: TextStyle(fontSize: 24)),
const Spacer(),
closeButton,
]),
const SizedBox(height: 20),
const Text(
'Image',
Expand Down
4 changes: 0 additions & 4 deletions src/client/gui/lib/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,3 @@ final networksProvider = Provider.autoDispose((ref) {
}
return BuiltSet<String>();
});

final daemonInfoProvider = FutureProvider.autoDispose<DaemonInfoReply>((ref) {
return ref.watch(grpcClientProvider).daemonInfo();
});
25 changes: 12 additions & 13 deletions src/client/gui/lib/vm_details/cpus_slider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,30 @@ import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:system_info2/system_info2.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

final cpuCoreCountProvider = FutureProvider((ref) {
return ref
.watch(grpcClientProvider)
.daemonInfo()
.then((r) => r.cpus.toInt());
});

class CpusSlider extends StatefulWidget {
class CpusSlider extends ConsumerStatefulWidget {
final int? initialValue;
final FormFieldSetter<int> onSaved;

final int maxCpus;

const CpusSlider({
super.key,
this.initialValue,
required this.onSaved,
this.maxCpus=1,
});

@override
State<CpusSlider> createState() => _CpusSliderState();
ConsumerState<CpusSlider> createState() => _CpusSliderState();
}

class _CpusSliderState extends State<CpusSlider> {
static final cores = ref.watch(ramSizeProvider).valueOrNull ?? SysInfo.cores.length;
class _CpusSliderState extends ConsumerState<CpusSlider> {


final min = 1;
late final max = math.max(widget.initialValue ?? 0, cores);

late final controller = TextEditingController(
text: widget.initialValue?.toString(),
);
Expand Down Expand Up @@ -64,6 +60,9 @@ class _CpusSliderState extends State<CpusSlider> {

@override
Widget build(BuildContext context) {
final cores = widget.maxCpus;
final max = math.max(widget.initialValue ?? 0, cores);

final textField = TextField(
controller: controller,
focusNode: focusNode,
Expand Down
5 changes: 4 additions & 1 deletion src/client/gui/lib/vm_details/disk_slider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ class DiskSlider extends ConsumerWidget {
final int min;
final FormFieldSetter<int> onSaved;

final int maxDisk;

DiskSlider({
super.key,
int? min,
this.initialValue,
required this.onSaved,
this.maxDisk=1,
}) : min = min ?? 1.gibi;

@override
Widget build(BuildContext context, WidgetRef ref) {
final disk = ref.watch(diskSizeProvider).valueOrNull ?? min;
final disk = maxDisk ?? min;
final max = math.max(initialValue ?? 0, disk);
final enabled = min != max;

Expand Down
19 changes: 9 additions & 10 deletions src/client/gui/lib/vm_details/ram_slider.dart
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:system_info2/system_info2.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'mapping_slider.dart';
import 'memory_slider.dart';

final ramSizeProvider = FutureProvider((ref) {
return ref
.watch(grpcClientProvider)
.daemonInfo()
.then((r) => r.memory.toInt());
});
class RamSlider extends ConsumerWidget {

class RamSlider extends StatelessWidget {
static final ram = ref.watch(ramSizeProvider).valueOrNull ?? 512.mebi;//?? SysInfo.getTotalPhysicalMemory();

final int? initialValue;
final FormFieldSetter<int> onSaved;

final int maxRam;

const RamSlider({
super.key,
this.initialValue,
required this.onSaved,
this.maxRam=1,
});

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {

final ram = maxRam ?? 512.mebi;

return MemorySlider(
label: 'Memory',
initialValue: initialValue,
Expand Down
2 changes: 0 additions & 2 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3833,5 +3833,3 @@ mp::LinuxSystemInfo mp::read_linux_system_info()
#endif
return info;
}


0 comments on commit bad74f2

Please sign in to comment.