Skip to content

Commit

Permalink
Parser to String. #1
Browse files Browse the repository at this point in the history
  • Loading branch information
caduandrade committed Sep 25, 2023
1 parent 7029969 commit e4538d8
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/docking.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
library docking;

export 'src/layout/docking_layout.dart';
export 'src/layout/docking_area_type.dart';
export 'src/layout/drop_position.dart';
export 'src/layout/typedef_parsers.dart';
export 'src/layout/layout_parser.dart';
export 'src/on_item_selection.dart';
export 'src/on_item_close.dart';
export 'src/docking_buttons_builder.dart';
Expand Down
220 changes: 220 additions & 0 deletions lib/src/layout/layout_parser.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
import 'package:docking/src/layout/docking_layout.dart';
import 'package:docking/src/layout/typedef_parsers.dart';
import 'package:meta/meta.dart';

/// Parser from DockingLayout to String.
class LayoutParser {
/// Converts a layout into a String to be stored.
///
/// The String will have the following structure:
/// VERSION:AREAS_LENGTH:AREAS
///
/// The VERSION group has a fixed value: V1
///
/// The AREAS group separates each area with a comma and follows the
/// pattern: AREA_INDEX_1(AREA_CONFIGURATION),...,AREA_INDEX_N(AREA_CONFIGURATION)
/// Example: 1(AREA_CONFIGURATION),2(AREA_CONFIGURATION),3(AREA_CONFIGURATION)
///
/// The AREA_CONFIGURATION group represents all area data given the class:
///
/// * [DockingItem]
/// * AREA_ACRONYM
/// * WEIGHT
/// * MINIMAL_WEIGHT
/// * MINIMAL_SIZE
/// * ID_LENGTH
/// * ID
/// * NAME_LENGTH
/// * NAME
/// * VALUE_LENGTH
/// * VALUE
/// * CLOSABLE
/// * MAXIMIZABLE
/// * MAXIMIZED
/// * CHILDREN_INDEXES
/// * [DockingColumn]
/// * AREA_ACRONYM
/// * WEIGHT
/// * MINIMAL_WEIGHT
/// * MINIMAL_SIZE
/// * CHILDREN_INDEXES
/// * [DockingRow]
/// * AREA_ACRONYM
/// * WEIGHT
/// * MINIMAL_WEIGHT
/// * MINIMAL_SIZE
/// * CHILDREN_INDEXES
/// * [DockingTabs]
/// * AREA_ACRONYM
/// * WEIGHT
/// * MINIMAL_WEIGHT
/// * MINIMAL_SIZE
/// * MAXIMIZABLE
/// * MAXIMIZED
/// * CHILDREN_INDEXES
///
/// Example:
/// V1:1:1(1;I;.2;;100;5;my_id;7;my_name;8;my_value;T;;F)
String stringify(
{required DockingLayout layout,
IdToString? idToString,
ValueToString? valueToString}) {
final List<DockingArea> areas = layout.layoutAreas();

String str = 'V1:${areas.length}:';
for (int i = 0; i < areas.length; i++) {
if (i > 0) {
str += ',';
}
final DockingArea area = areas[i];
str += '${area.index}(';
str += '${area.areaAcronym};';
str += '${stringifyArea(area: area)};';

if (area is DockingItem) {
str += stringifyItem(
item: area,
idToString: idToString ?? defaultIdToString,
valueToString: valueToString ?? defaultValueToString);
} else if (area is DockingTabs) {
str += stringifyTabs(tabs: area);
}

if (area is DockingParentArea) {
str += ';';
str += stringifyParent(parent: area);
}
str += ')';
}

return str;
}

/// Converts the [DockingArea] configuration into a String with the
/// following data separated by semicolons:
/// * WEIGHT
/// * MINIMAL_WEIGHT
/// * MINIMAL_SIZE
///
/// Example: .2;;100
@visibleForTesting
String stringifyArea({required DockingArea area}) {
List<String> data = [];
// WEIGHT
data.add('${area.minimalWeight?.toString()}');
// MINIMAL_SIZE
data.add('${area.minimalWeight?.toString()}');
// MINIMAL_SIZE
data.add('${area.minimalSize?.toString()}');

return data.join(';');
}

/// Converts the [DockingItem] configuration into a String with the
/// following data separated by semicolons:
/// * ID_LENGTH
/// * ID
/// * NAME_LENGTH
/// * NAME
/// * VALUE_LENGTH
/// * VALUE
/// * CLOSABLE
/// * MAXIMIZABLE
/// * MAXIMIZED
///
/// [T] is [TRUE] and [F] is [FALSE].
///
/// Example:
/// 5;my_id;7;my_name;8;my_value;T;;F
@visibleForTesting
String stringifyItem(
{required DockingItem item,
required IdToString idToString,
required ValueToString valueToString}) {
List<String> data = [];

// ID_LENGTH and ID
final String id = idToString(item.id);
data.add(id.length.toString());
data.add(id);

// NAME_LENGTH and NAME
final String name = item.name ?? '';
data.add(name.length.toString());
data.add(name);

// VALUE_LENGTH and VALUE
final String value = valueToString(item.value);
data.add(value.length.toString());
data.add(value);

// CLOSABLE
data.add(item.closable ? 'T' : 'F');

// MAXIMIZABLE
if (item.maximizable != null) {
data.add(item.maximizable! ? 'T' : 'F');
} else {
data.add('');
}

// MAXIMIZED
data.add(item.maximized ? 'T' : 'F');

return data.join(';');
}

/// Converts the [DockingTabs] configuration into a String with the
/// following data separated by semicolons:
/// * MAXIMIZABLE
/// * MAXIMIZED
///
/// Example:
/// T;T
@visibleForTesting
String stringifyTabs({required DockingTabs tabs}) {
List<String> data = [];

// MAXIMIZABLE
if (tabs.maximizable != null) {
data.add(tabs.maximizable! ? 'T' : 'F');
} else {
data.add('');
}

// MAXIMIZED
data.add(tabs.maximized ? 'T' : 'F');

return data.join(';');
}

/// Converts the children indexes to String separated by colon.
///
/// Example:
/// 4,5,6
@visibleForTesting
String stringifyParent({required DockingParentArea parent}) {
List<String> indexes = [];
for (int i = 0; i < parent.childrenCount; i++) {
final DockingArea child = parent.childAt(i);
indexes.add(child.index.toString());
}
return indexes.join(',');
}

/// Default conversion from ID to String.
static String defaultIdToString(dynamic id) {
if (id == null) {
return '';
}
return id.toString();
}

/// Default conversion from ID to String.
static String defaultValueToString(dynamic value) {
if (value == null) {
return '';
}
return value.toString();
}
}
11 changes: 11 additions & 0 deletions lib/src/layout/typedef_parsers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// Converts ID to String.
typedef IdToString = String Function(dynamic id);

/// Converts String to ID.
typedef StringToId = dynamic Function(String id);

/// Converts value to String.
typedef ValueToString = String Function(dynamic value);

/// Converts String to value.
typedef StringToValue = dynamic Function(String value);

0 comments on commit e4538d8

Please sign in to comment.