Skip to content

Commit

Permalink
Small code and UI fixes and improvements
Browse files Browse the repository at this point in the history
Fixing many errors caused in the last commit. I must have been really confused, apologies for the mess
  • Loading branch information
WilliamKarolDiCioccio committed Jun 7, 2024
1 parent 7ef7d2a commit 575243e
Show file tree
Hide file tree
Showing 19 changed files with 394 additions and 235 deletions.
98 changes: 55 additions & 43 deletions app/lib/dialogs/create_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import 'package:flutter/material.dart';

import 'package:flutter_animate/flutter_animate.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:provider/provider.dart';

import 'package:open_local_ui/models/ollama_responses.dart';
import 'package:open_local_ui/providers/model.dart';
import 'package:provider/provider.dart';

class CreateModelDialog extends StatefulWidget {
const CreateModelDialog({super.key});
Expand All @@ -24,6 +23,14 @@ class _CreateModelDialogState extends State<CreateModelDialog> {
double _progressValue = 0.0;
String _progressBarText = '';

@override
void dispose() {
_nameEditingController.dispose();
_fileEditingController.dispose();
_modelSelectionController.dispose();
super.dispose();
}

void _updateProgress(OllamaCreateResponse response) {
setState(() {
_stepsCount += 1;
Expand All @@ -39,12 +46,39 @@ class _CreateModelDialogState extends State<CreateModelDialog> {
});
}

@override
void dispose() {
_nameEditingController.dispose();
_fileEditingController.dispose();
_modelSelectionController.dispose();
super.dispose();
void _createModel() async {
setState(() => _isCreating = true);

final splitIndex = _modelSelectionController.text.indexOf(':');
String modelBaseName;

if (splitIndex != -1) {
modelBaseName = _modelSelectionController.text.substring(
0,
splitIndex,
);
} else {
modelBaseName = _modelSelectionController.text;
}

final stream = context.read<ModelProvider>().create(
_nameEditingController.text.toLowerCase(),
"FROM $modelBaseName\nSYSTEM ${_fileEditingController.text}",
);

await for (final data in stream) {
if (context.mounted) _updateProgress(data);
}

if (context.mounted) {
setState(() {
_isCreating = false;
_progressValue = 0.0;
_progressBarText = '';
_nameEditingController.clear();
_fileEditingController.clear();
});
}
}

@override
Expand Down Expand Up @@ -76,6 +110,17 @@ class _CreateModelDialogState extends State<CreateModelDialog> {
AppLocalizations.of(context)!.createModelDialogGuideText1,
),
DropdownMenu(
menuHeight: 128,
menuStyle: MenuStyle(
elevation: WidgetStateProperty.all(
8.0,
),
shape: WidgetStateProperty.all(
const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(16.0)),
),
),
),
controller: _modelSelectionController,
inputDecorationTheme: const InputDecorationTheme(
border: OutlineInputBorder(
Expand Down Expand Up @@ -134,7 +179,7 @@ class _CreateModelDialogState extends State<CreateModelDialog> {
LinearProgressIndicator(
value: _progressValue,
minHeight: 20.0,
borderRadius: BorderRadius.circular(10.0),
borderRadius: BorderRadius.circular(16.0),
),
],
),
Expand All @@ -153,40 +198,7 @@ class _CreateModelDialogState extends State<CreateModelDialog> {
),
if (!_isCreating)
TextButton(
onPressed: () async {
setState(() => _isCreating = true);

final splitIndex = _modelSelectionController.text.indexOf(':');
String modelBaseName;

if (splitIndex != -1) {
modelBaseName = _modelSelectionController.text.substring(
0,
splitIndex,
);
} else {
modelBaseName = _modelSelectionController.text;
}

final stream = context.read<ModelProvider>().create(
_nameEditingController.text.toLowerCase(),
"FROM $modelBaseName\nSYSTEM ${_fileEditingController.text}",
);

await for (final data in stream) {
if (context.mounted) _updateProgress(data);
}

if (context.mounted) {
setState(() {
_isCreating = false;
_progressValue = 0.0;
_progressBarText = '';
_nameEditingController.clear();
_fileEditingController.clear();
});
}
},
onPressed: () => _createModel(),
child: Text(
AppLocalizations.of(context)!.dialogCreateButtonShared,
),
Expand Down
52 changes: 27 additions & 25 deletions app/lib/dialogs/pull_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class _PullModelDialogState extends State<PullModelDialog> {
double _progressValue = 0.0;
String _progressBarText = '';

@override
void dispose() {
_textEditingController.dispose();
super.dispose();
}

void _updateProgress(OllamaPullResponse response) {
setState(() {
_progressValue = response.completed / response.total;
Expand All @@ -40,10 +46,25 @@ class _PullModelDialogState extends State<PullModelDialog> {
});
}

@override
void dispose() {
_textEditingController.dispose();
super.dispose();
void _pullModel() async {
setState(() => _isPulling = true);

final stream = context
.read<ModelProvider>()
.pull(_textEditingController.text.toLowerCase());

await for (final response in stream) {
if (context.mounted) _updateProgress(response);
}

if (context.mounted) {
setState(() {
_isPulling = false;
_progressValue = 0.0;
_progressBarText = '';
_textEditingController.clear();
});
}
}

@override
Expand Down Expand Up @@ -86,7 +107,7 @@ class _PullModelDialogState extends State<PullModelDialog> {
LinearProgressIndicator(
value: _progressValue,
minHeight: 20.0,
borderRadius: BorderRadius.circular(10.0),
borderRadius: BorderRadius.circular(16.0),
),
],
),
Expand All @@ -105,26 +126,7 @@ class _PullModelDialogState extends State<PullModelDialog> {
),
if (!_isPulling)
TextButton(
onPressed: () async {
setState(() => _isPulling = true);

final stream = context
.read<ModelProvider>()
.pull(_textEditingController.text.toLowerCase());

await for (final response in stream) {
if (context.mounted) _updateProgress(response);
}

if (context.mounted) {
setState(() {
_isPulling = false;
_progressValue = 0.0;
_progressBarText = '';
_textEditingController.clear();
});
}
},
onPressed: () => _pullModel(),
child: Text(
AppLocalizations.of(context)!.dialogStartButtonShared,
),
Expand Down
63 changes: 38 additions & 25 deletions app/lib/dialogs/push_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class _PushModelDialogState extends State<PushModelDialog> {
double _progressValue = 0.0;
String _progressBarText = '';

@override
void dispose() {
_modelSelectionController.dispose();
super.dispose();
}

void _updateProgress(OllamaPushResponse response) {
setState(() {
_progressValue = response.completed / response.total;
Expand All @@ -38,10 +44,25 @@ class _PushModelDialogState extends State<PushModelDialog> {
});
}

@override
void dispose() {
_modelSelectionController.dispose();
super.dispose();
void _pushModel() async {
setState(() => _isPushing = true);

final stream = context
.read<ModelProvider>()
.push(_modelSelectionController.text.toLowerCase());

await for (final data in stream) {
if (context.mounted) _updateProgress(data);
}

if (context.mounted) {
setState(() {
_isPushing = false;
_progressValue = 0.0;
_progressBarText = '';
_modelSelectionController.clear();
});
}
}

@override
Expand Down Expand Up @@ -74,6 +95,17 @@ class _PushModelDialogState extends State<PushModelDialog> {
),
const SizedBox(width: 8.0),
DropdownMenu(
menuHeight: 128,
menuStyle: MenuStyle(
elevation: WidgetStateProperty.all(
8.0,
),
shape: WidgetStateProperty.all(
const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(16.0)),
),
),
),
controller: _modelSelectionController,
inputDecorationTheme: const InputDecorationTheme(
border: OutlineInputBorder(
Expand Down Expand Up @@ -101,7 +133,7 @@ class _PushModelDialogState extends State<PushModelDialog> {
LinearProgressIndicator(
value: _progressValue,
minHeight: 20.0,
borderRadius: BorderRadius.circular(10.0),
borderRadius: BorderRadius.circular(16.0),
),
],
),
Expand All @@ -120,26 +152,7 @@ class _PushModelDialogState extends State<PushModelDialog> {
),
if (!_isPushing)
TextButton(
onPressed: () async {
setState(() => _isPushing = true);

final stream = context
.read<ModelProvider>()
.push(_modelSelectionController.text.toLowerCase());

await for (final data in stream) {
if (context.mounted) _updateProgress(data);
}

if (context.mounted) {
setState(() {
_isPushing = false;
_progressValue = 0.0;
_progressBarText = '';
_modelSelectionController.clear();
});
}
},
onPressed: () => _pushModel(),
child: Text(
AppLocalizations.of(context)!.dialogStartButtonShared,
),
Expand Down
2 changes: 1 addition & 1 deletion app/lib/helpers/github.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class GitHubRESTHelpers {
logger.e(
'Failed to list Contributors. Status code: ${response.statusCode}',
);

return [];
}

Expand Down
27 changes: 14 additions & 13 deletions app/lib/helpers/snackbar.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';

import 'package:open_local_ui/scaffold_messenger_key.dart';

enum SnackBarType {
Expand All @@ -8,25 +9,21 @@ enum SnackBarType {
error,
}

class SnackBarHelper {
static void showSnackBar(String message, SnackBarType type) {
late Color backgroundColor;

class SnackBarHelpers {
static Color _getColor(SnackBarType type) {
switch (type) {
case SnackBarType.success:
backgroundColor = Colors.green;
break;
return Colors.green;
case SnackBarType.info:
backgroundColor = Colors.blue;
break;
return Colors.blue;
case SnackBarType.warning:
backgroundColor = Colors.orange;
break;
return Colors.orange;
case SnackBarType.error:
backgroundColor = Colors.red;
break;
return Colors.red;
}
}

static void showSnackBar(String message, SnackBarType type) async {
final snackBar = SnackBar(
content: Text(
message,
Expand All @@ -36,8 +33,12 @@ class SnackBarHelper {
fontWeight: FontWeight.bold,
),
),
showCloseIcon: true,
duration: const Duration(seconds: 3),
backgroundColor: backgroundColor,
backgroundColor: _getColor(type),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0),
),
behavior: SnackBarBehavior.floating,
);

Expand Down
Loading

0 comments on commit 575243e

Please sign in to comment.