Skip to content

Commit

Permalink
Merge pull request #29 from Open-Resin-Alliance/custom_name
Browse files Browse the repository at this point in the history
feat(settings, util, home): vendor configuration and dynamic machine name support
  • Loading branch information
PaulGD03 authored Jan 7, 2025
2 parents 86de8f7 + e2e2c54 commit 2a27c6d
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 28 deletions.
4 changes: 2 additions & 2 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
NOTICE

This product includes software developed by [TheContrappostoShop](https://github.com/TheContrappostoShop).
This product includes software developed by [Open Resin Alliance](https://github.com/Open-Resin-Alliance).

Orion is licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Expand All @@ -10,4 +10,4 @@ Unless required by applicable law or agreed to in writing, software distributed

This product also includes third-party software components. Please refer to the LICENSE file for more information.

© 2024 TheContrappostoShop. All rights reserved.
© 2024 Open Resin Alliance. All rights reserved.
8 changes: 8 additions & 0 deletions example_vendor.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"vendor": {
"vendorName": "Open Resin Alliance",
"vendorMachineName": "Ares V2",
"vendorUrl": "https://openresin.org",
"vendorThemeSeed": "#FF00FA00"
}
}
5 changes: 3 additions & 2 deletions lib/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:orion/api_services/api_services.dart';
import 'package:orion/settings/about_screen.dart';
import 'package:orion/util/hold_button.dart';
import 'package:orion/util/orion_config.dart';

Expand Down Expand Up @@ -61,8 +62,8 @@ class HomeScreenState extends State<HomeScreen> {

return Scaffold(
appBar: AppBar(
title: const Text(
'3D Printer',
title: Text(
config.getString('machineName', category: 'machine'),
textAlign: TextAlign.center,
),
centerTitle: true,
Expand Down
102 changes: 94 additions & 8 deletions lib/settings/about_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ import 'package:logging/logging.dart';
import 'package:orion/pubspec.dart';
import 'package:orion/themes/themes.dart';
import 'package:orion/util/orion_config.dart';
import 'package:orion/util/orion_kb/orion_keyboard_expander.dart';
import 'package:orion/util/orion_kb/orion_textfield_spawn.dart';
import 'package:phosphor_flutter/phosphor_flutter.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:toastification/toastification.dart';
import 'dart:io';

Logger _logger = Logger('AboutScreen');
OrionConfig config = OrionConfig();

Future<String> executeCommand(String command, List<String> arguments) async {
final result = await Process.run(command, arguments);
Expand Down Expand Up @@ -77,6 +81,12 @@ class AboutScreenState extends State<AboutScreen> {
int qrTapCount = 0;
Toastification toastification = Toastification();

late String customName;

final ScrollController _scrollController = ScrollController();
final GlobalKey<SpawnOrionTextFieldState> cNameTextFieldKey =
GlobalKey<SpawnOrionTextFieldState>();

@override
Widget build(BuildContext context) {
bool isLandscape =
Expand All @@ -99,7 +109,7 @@ class AboutScreenState extends State<AboutScreen> {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
buildNameCard('3D Printer'),
buildNameCard(config.getString('machineName', category: 'machine')),
buildInfoCard('Serial Number', kDebugMode ? 'DBG-0001-001' : 'N/A'),
buildVersionCard(),
buildHardwareCard(),
Expand All @@ -116,7 +126,8 @@ class AboutScreenState extends State<AboutScreen> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
buildNameCard('3D Printer'),
buildNameCard(
config.getString('machineName', category: 'machine')),
buildInfoCard('Serial Number',
kDebugMode ? 'DBG-0001-001' : 'Currently Unavailable'),
buildVersionCard(),
Expand Down Expand Up @@ -178,12 +189,87 @@ class AboutScreenState extends State<AboutScreen> {
return Card.outlined(
elevation: 1.0,
child: ListTile(
title: Text(
title,
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.primary),
contentPadding: const EdgeInsets.symmetric(horizontal: 16.0),
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
title,
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.primary),
overflow: TextOverflow.fade,
softWrap: false,
),
),
ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Center(child: Text('Custom Machine Name')),
content: SizedBox(
width: MediaQuery.of(context).size.width * 0.5,
child: SingleChildScrollView(
child: Column(
children: [
SpawnOrionTextField(
key: cNameTextFieldKey,
keyboardHint: 'Enter a custom name',
locale:
Localizations.localeOf(context).toString(),
scrollController: _scrollController,
presetText: config.getString('machineName',
category: 'machine'),
),
OrionKbExpander(textFieldKey: cNameTextFieldKey),
],
),
),
),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Close',
style: TextStyle(fontSize: 20)),
),
TextButton(
onPressed: () {
setState(() {
customName = cNameTextFieldKey.currentState!
.getCurrentText();
config.setString('machineName', customName,
category: 'machine');
});
Navigator.of(context).pop();
},
child: const Text('Confirm',
style: TextStyle(fontSize: 20)),
),
],
);
},
);
},
child: Row(
children: [
const Text(
'Edit',
style: TextStyle(
fontSize: 20,
),
),
const SizedBox(width: 10),
PhosphorIcon(PhosphorIcons.notePencil()),
],
),
),
],
),
),
);
Expand Down
3 changes: 2 additions & 1 deletion lib/settings/general_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class GeneralCfgScreenState extends State<GeneralCfgScreen> {
late String overrideRelease;
late bool verboseLogging;
late bool selfDestructMode;
late String machineName;

late String originalRotation;

Expand Down Expand Up @@ -82,10 +83,10 @@ class GeneralCfgScreenState extends State<GeneralCfgScreen> {
verboseLogging = config.getFlag('verboseLogging', category: 'developer');
selfDestructMode =
config.getFlag('selfDestructMode', category: 'topsecret');

screenRotation = screenRotation == '' ? '0' : screenRotation;
config.setString('screenRotation', screenRotation, category: 'advanced');
originalRotation = screenRotation;
machineName = config.getString('machineName', category: 'machine');
}

bool shouldDestruct() {
Expand Down
2 changes: 1 addition & 1 deletion lib/settings/settings_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class SettingsScreenState extends State<SettingsScreen> {
'Version {{ version }} - {{ commit }}',
applicationName: 'Orion',
applicationLegalese:
'Apache License 2.0 - Copyright © TheContrappostoShop {{ year }}',
'Apache License 2.0 - Copyright © {{ year }} Open Resin Alliance',
children: <Widget>[
Padding(
padding:
Expand Down
15 changes: 11 additions & 4 deletions lib/themes/themes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@
*/

import 'package:flutter/material.dart';
import 'package:flex_seed_scheme/flex_seed_scheme.dart';
import '../util/orion_config.dart';

final _config = OrionConfig();
final _seedColor = _config.getVendorThemeSeed();

final ThemeData themeLight = ThemeData(
fontFamily: 'AtkinsonHyperlegible',
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xff6750a4),
colorScheme: SeedColorScheme.fromSeeds(
primaryKey: _seedColor,
brightness: Brightness.light,
variant: FlexSchemeVariant.soft,
),
appBarTheme: const AppBarTheme(
titleTextStyle: TextStyle(
Expand Down Expand Up @@ -57,9 +63,10 @@ final ThemeData themeLight = ThemeData(

final ThemeData themeDark = ThemeData(
fontFamily: 'AtkinsonHyperlegible',
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xff6750a4),
colorScheme: SeedColorScheme.fromSeeds(
primaryKey: _seedColor,
brightness: Brightness.dark,
variant: FlexSchemeVariant.soft,
),
appBarTheme: const AppBarTheme(
titleTextStyle: TextStyle(
Expand Down
85 changes: 75 additions & 10 deletions lib/util/orion_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,76 @@ class OrionConfig {
setFlag(flagName, !currentValue, category: category);
}

Color getVendorThemeSeed() {
var config = _getConfig();
var seedHex = config['vendor']?['vendorThemeSeed'] ?? '#ff6750a4';
// Remove the '#' and parse the hex color
_logger.config('Vendor theme seed: $seedHex');
return Color(int.parse('${seedHex.replaceAll('#', '')}', radix: 16));
}

Map<String, dynamic> _getVendorConfig() {
var fullPath = path.join(_configPath, 'vendor.cfg');
var vendorFile = File(fullPath);

if (!vendorFile.existsSync() || vendorFile.readAsStringSync().isEmpty) {
return {};
}

try {
return Map<String, dynamic>.from(
json.decode(vendorFile.readAsStringSync()));
} catch (e) {
_logger.warning('Failed to parse vendor.cfg: $e');
return {};
}
}

Map<String, dynamic> _getConfig() {
var fullPath = path.join(_configPath, 'orion.cfg');
var configFile = File(fullPath);
var vendorConfig = _getVendorConfig();

// Get vendor machine name if available
var defaultMachineName =
vendorConfig['vendor']?['vendorMachineName'] ?? '3D Printer';

var defaultConfig = {
'general': {
'themeMode': 'dark',
},
'advanced': {},
'machine': {
'machineName': defaultMachineName,
'firstRun': true,
},
};

if (!configFile.existsSync() || configFile.readAsStringSync().isEmpty) {
var defaultConfig = {
'general': {
'themeMode': 'dark',
},
'advanced': {},
};
_writeConfig(defaultConfig);
return defaultConfig;
// Remove vendor section before writing
var configToWrite = Map<String, dynamic>.from(defaultConfig);
_writeConfig(configToWrite);
// Return merged view for reading
return _mergeConfigs(defaultConfig, vendorConfig);
}

return json.decode(configFile.readAsStringSync());
var userConfig =
Map<String, dynamic>.from(json.decode(configFile.readAsStringSync()));

// Return merged view for reading
return _mergeConfigs(
_mergeConfigs(defaultConfig, vendorConfig), userConfig);
}

void _writeConfig(Map<String, dynamic> config) {
// Remove any vendor section before writing to orion.cfg
var configToWrite = Map<String, dynamic>.from(config);
configToWrite.remove('vendor');

var fullPath = path.join(_configPath, 'orion.cfg');
var configFile = File(fullPath);
var encoder = const JsonEncoder.withIndent(' ');
configFile.writeAsStringSync(encoder.convert(config));
configFile.writeAsStringSync(encoder.convert(configToWrite));
}

void blowUp(BuildContext context, String imagePath) {
Expand Down Expand Up @@ -157,4 +204,22 @@ class OrionConfig {
},
);
}

Map<String, dynamic> _mergeConfigs(
Map<String, dynamic> base, Map<String, dynamic> overlay) {
var result = Map<String, dynamic>.from(base);

overlay.forEach((key, value) {
if (value is Map) {
result[key] = result.containsKey(key)
? _mergeConfigs(Map<String, dynamic>.from(result[key] ?? {}),
Map<String, dynamic>.from(value))
: Map<String, dynamic>.from(value);
} else {
result[key] = value;
}
});

return result;
}
}
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.0"
flex_seed_scheme:
dependency: "direct main"
description:
name: flex_seed_scheme
sha256: "7639d2c86268eff84a909026eb169f008064af0fb3696a651b24b0fa24a40334"
url: "https://pub.dev"
source: hosted
version: "3.4.1"
flutter:
dependency: "direct main"
description: flutter
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ dependencies:
toastification: ^2.0.0
async: ^2.11.0
fading_edge_scrollview: ^4.1.1
flex_seed_scheme: ^3.4.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 2a27c6d

Please sign in to comment.