Skip to content

Commit

Permalink
Use path library, put more in api library
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Aug 15, 2024
1 parent a29a889 commit cb57586
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 68 deletions.
2 changes: 1 addition & 1 deletion packages/dart_leap/lib/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ library dart_leap;

export 'src/helpers/date_time.dart';
export 'src/helpers/enum.dart';
export 'src/helpers/int.dart';
export 'src/helpers/num.dart';
export 'src/helpers/string.dart';
3 changes: 0 additions & 3 deletions packages/dart_leap/lib/src/helpers/int.dart

This file was deleted.

23 changes: 23 additions & 0 deletions packages/dart_leap/lib/src/helpers/num.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
extension IntHelper on int {
static const int max = 9007199254740991;
}

extension NullableIntHelper on int? {
int? add(int? value) {
if (this == null || value == null) return null;
return this! + value;
}

int? subtract(int? value) {
if (this == null || value == null) return null;
return this! - value;
}
}

extension DoubleHelper on double {
double toPrecision(int n) => double.parse(toStringAsFixed(n));
}

extension NumHelper on num {
bool inRange(num min, num max) => this >= min && this <= max;
}
41 changes: 12 additions & 29 deletions packages/lw_file_system/lib/src/api/file_system_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'dart:async';
import 'package:archive/archive.dart';
import 'package:flutter/foundation.dart';
import 'package:lw_file_system/lw_file_system.dart';
import 'package:path/path.dart' as p;
import 'package:rxdart/rxdart.dart';
import 'package:web/web.dart';

import 'file_system_dav.dart';
import 'file_system_io.dart';
Expand All @@ -21,6 +23,8 @@ typedef CreateFileCallback = FutureOr<Uint8List> Function(

void defaultCreateDefault(GeneralFileSystem fileSystem) {}

final _pathContext = p.Context(style: p.Style.posix);

abstract class GeneralFileSystem {
final FileSystemConfig config;

Expand Down Expand Up @@ -49,20 +53,8 @@ abstract class GeneralFileSystem {

ExternalStorage? get storage => null;

String normalizePath(String path, {bool leadingSlash = true}) {
// Add leading slash
if (!path.startsWith('/') && leadingSlash) {
path = '/$path';
}
if (path.startsWith('/') && !leadingSlash) {
path = path.substring(1);
}
// Remove trailing slash
if (path.endsWith('/')) {
path = path.substring(0, path.length - 1);
}
return path;
}
String normalizePath(String path) =>
_pathContext.relative(_pathContext.canonicalize(path));

String convertNameToFile(String name) {
return name.replaceAll(RegExp(r'[\\/:\*\?"<>\|\n\0-\x1F\x7F-\xFF]'), '_');
Expand All @@ -73,30 +65,21 @@ abstract class GeneralFileSystem {
final slashIndex = path.lastIndexOf('/');
var dir = slashIndex < 0 ? '' : path.substring(0, slashIndex);
if (dir.isNotEmpty) dir = '$dir/';
final dotIndex = path.lastIndexOf('.');
var ext = dotIndex < 0 ? '' : path.substring(dotIndex + 1);
if (ext.isNotEmpty) ext = '.$ext';
var name = dotIndex < 0
? path.substring(dir.length)
: path.substring(slashIndex + 1, dotIndex);
final extension = p.extension(path);
final name = p.basenameWithoutExtension(path);
var newName = name;
var i = 1;
while (await hasAsset('$dir$newName$ext')) {
while (await hasAsset(p.join(dir, '$newName$extension'))) {
newName = '$name ($i)';
i++;
}
return '$dir$newName$ext';
return p.join(dir, '$newName$extension');
}

FutureOr<String> getAbsolutePath(String relativePath) async {
// Convert \ to /
relativePath = relativePath.replaceAll('\\', '/');
// Remove leading slash
if (relativePath.startsWith('/')) {
relativePath = relativePath.substring(1);
}
relativePath = normalizePath(relativePath);
final root = await getDirectory();
return '$root/$relativePath';
return p.join(root, relativePath);
}

Future<String> getDirectory() async => config.getDirectory(storage);
Expand Down
2 changes: 1 addition & 1 deletion packages/lw_file_system/lib/src/api/file_system_dav.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DavRemoteDirectoryFileSystem extends RemoteDirectoryFileSystem {
@override
Future<RawFileSystemEntity?> readAsset(String path,
{bool readData = true, bool forceRemote = false}) async {
path = normalizePath(path, leadingSlash: false);
path = normalizePath(path);
final cached = await getCachedContent(path);
if (cached != null && !forceRemote) {
return cached;
Expand Down
39 changes: 21 additions & 18 deletions packages/lw_file_system_api/lib/src/location.dart
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
import 'package:dart_mappable/dart_mappable.dart';
import 'package:path/path.dart' as p;

part 'location.mapper.dart';

@MappableClass()
class AssetLocation with AssetLocationMappable {
final String remote;
final String path;
final bool absolute;

const AssetLocation({
this.remote = '',
required this.path,
this.absolute = false,
});

factory AssetLocation.local(String path, [bool absolute = false]) =>
AssetLocation(path: path, absolute: absolute);
AssetLocation(path: path);

static const empty = AssetLocation(path: '');

bool get isEmpty => path.isEmpty;

bool get isRemote => remote.isNotEmpty;

String get identifier =>
isRemote ? '$pathWithLeadingSlash@$remote' : pathWithLeadingSlash;
String get identifier => isRemote ? '$path@$remote' : path;

String get fileExtension => p.extension(path);

String get fileName => p.basename(path);

String get pathWithLeadingSlash => path.startsWith('/') ? path : '/$path';
String get fileNameWithoutExtension => p.basenameWithoutExtension(path);

String get pathWithoutLeadingSlash =>
path.startsWith('/') ? path.substring(1) : path;
String get parent => p.dirname(path);

String get fileExtension =>
fileName.contains('.') ? fileName.split('.').last : '';
bool get absolute => p.isAbsolute(path);

String get fileName => path.split('/').last;
String get parent {
final lastSlash = path.lastIndexOf('/');
if (lastSlash < 0) return '';
return path.substring(0, lastSlash);
AssetLocation buildParentLocation() {
return AssetLocation(path: parent, remote: remote);
}

bool isSame(AssetLocation other) =>
pathWithLeadingSlash == other.pathWithLeadingSlash &&
remote == other.remote;
AssetLocation buildChildLocation(String child) {
return AssetLocation(path: p.join(path, child), remote: remote);
}

AssetLocation buildSiblingLocation(String sibling) {
return AssetLocation(path: p.join(parent, sibling), remote: remote);
}
}
1 change: 1 addition & 0 deletions packages/lw_file_system_api/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ environment:
dependencies:
archive: ^3.6.0
dart_mappable: ^4.2.2
path: ^1.9.0

dev_dependencies:
lints: ^4.0.0
Expand Down
16 changes: 0 additions & 16 deletions packages/material_leap/lib/src/helpers/num.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
import 'dart:ui';

extension NullableIntHelper on int? {
int? add(int? value) {
if (this == null || value == null) return null;
return this! + value;
}

int? subtract(int? value) {
if (this == null || value == null) return null;
return this! - value;
}
}

extension DoubleHelper on double {
double toPrecision(int n) => double.parse(toStringAsFixed(n));
}

int convertColor(int color, int alpha) => Color(color).withAlpha(alpha).value;
int convertOldColor(int color, int old) =>
convertColor(color, Color(old).alpha);

0 comments on commit cb57586

Please sign in to comment.