Skip to content

Commit cc471ed

Browse files
committed
Add initialPath to enable browsing files on the root directory of the POD
1 parent 482db53 commit cc471ed

3 files changed

Lines changed: 57 additions & 19 deletions

File tree

lib/src/widgets/solid_file.dart

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,23 +202,32 @@ class _SolidFileState extends State<SolidFile> {
202202

203203
/// Resolves the base path asynchronously.
204204
///
205-
/// Defaults to the app data directory path via [getDataDirPath].
206-
/// Falls back to POD root if the app data directory cannot be determined.
205+
/// If [widget.currentPath] is explicitly provided, it is used as the base
206+
/// path. This allows the widget to start from any location on the POD,
207+
/// including the root. Otherwise, defaults to the app data directory path
208+
/// via [getDataDirPath]. Falls back to POD root if the app data directory
209+
/// cannot be determined.
207210
208211
Future<void> _resolveBasePath() async {
209-
try {
210-
final appDataPath = await getDataDirPath();
211-
_resolvedBasePath = PathUtils.normalise(appDataPath);
212-
} catch (e) {
213-
// Fall back to POD root if getDataDirPath fails.
214-
215-
debugPrint('Failed to get app data path, falling back to POD root: $e');
216-
_resolvedBasePath = SolidFile.podRoot;
212+
if (widget.currentPath != null) {
213+
// Use the explicitly provided path as the base.
214+
215+
_resolvedBasePath = PathUtils.normalise(widget.currentPath!);
216+
} else {
217+
try {
218+
final appDataPath = await getDataDirPath();
219+
_resolvedBasePath = PathUtils.normalise(appDataPath);
220+
} catch (e) {
221+
// Fall back to POD root if getDataDirPath fails.
222+
223+
debugPrint(
224+
'Failed to get app data path, falling back to POD root: $e',
225+
);
226+
_resolvedBasePath = SolidFile.podRoot;
227+
}
217228
}
218229

219-
_currentPath = widget.currentPath != null
220-
? PathUtils.normalise(widget.currentPath!)
221-
: _resolvedBasePath!;
230+
_currentPath = _resolvedBasePath!;
222231
setState(() {
223232
_isResolvingBasePath = false;
224233
});
@@ -379,6 +388,9 @@ class _SolidFileState extends State<SolidFile> {
379388
}
380389

381390
/// Builds the file browser widget.
391+
///
392+
/// Passes the resolved [_currentPath] as the initial path so the browser
393+
/// starts from the correct location (e.g., POD root for "All POD Files").
382394
383395
Widget _buildFileBrowser() {
384396
return SolidFileBrowserBuilder.build(
@@ -389,6 +401,7 @@ class _SolidFileState extends State<SolidFile> {
389401
widget.autoConfig,
390402
widget.friendlyFolderName,
391403
),
404+
initialPath: widget.currentPath,
392405
onFileSelected: widget.onFileSelected,
393406
onFileDownload: widget.onFileDownload,
394407
onFileDelete: widget.onFileDelete,

lib/src/widgets/solid_file_browser.dart

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ class SolidFileBrowser extends StatefulWidget {
7373
7474
final String friendlyFolderName;
7575

76+
/// Optional initial path for the browser to start from.
77+
///
78+
/// If provided, the browser will start from this path instead of the
79+
/// default app data directory path. Use an empty string to start from
80+
/// the POD root.
81+
82+
final String? initialPath;
83+
7684
const SolidFileBrowser({
7785
super.key,
7886
required this.onFileSelected,
@@ -82,6 +90,7 @@ class SolidFileBrowser extends StatefulWidget {
8290
required this.onImportCsv,
8391
required this.onDirectoryChanged,
8492
required this.friendlyFolderName,
93+
this.initialPath,
8594
});
8695

8796
@override
@@ -142,14 +151,28 @@ class SolidFileBrowserState extends State<SolidFileBrowser> {
142151
}
143152

144153
/// Resolves the home path internally via [getDataDirPath].
154+
///
155+
/// If [widget.initialPath] is provided, it is used as both the starting
156+
/// path and the home path for navigation. This allows browsing from any
157+
/// location on the POD, including the root.
145158
146159
Future<void> _resolveHomePath() async {
147-
try {
148-
final appDataPath = await getDataDirPath();
149-
_homePath = PathUtils.normalise(appDataPath);
150-
} catch (e) {
151-
debugPrint('Failed to get app data path, falling back to POD root: $e');
152-
_homePath = '';
160+
if (widget.initialPath != null) {
161+
// Use the explicitly provided initial path as both the home path and
162+
// the starting path. This ensures the "back to root" navigation and
163+
// path history work correctly for non-default starting locations.
164+
165+
_homePath = PathUtils.normalise(widget.initialPath!);
166+
} else {
167+
try {
168+
final appDataPath = await getDataDirPath();
169+
_homePath = PathUtils.normalise(appDataPath);
170+
} catch (e) {
171+
debugPrint(
172+
'Failed to get app data path, falling back to POD root: $e',
173+
);
174+
_homePath = '';
175+
}
153176
}
154177

155178
currentPath = _homePath;

lib/src/widgets/solid_file_browser_builder.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class SolidFileBrowserBuilder {
4242
static Widget build({
4343
required GlobalKey<SolidFileBrowserState> browserKey,
4444
required String friendlyFolderName,
45+
String? initialPath,
4546
Function(String fileName, String filePath)? onFileSelected,
4647
Function(String fileName, String filePath)? onFileDownload,
4748
Function(String fileName, String filePath)? onFileDelete,
@@ -53,6 +54,7 @@ class SolidFileBrowserBuilder {
5354
key: browserKey,
5455
browserKey: browserKey,
5556
friendlyFolderName: friendlyFolderName,
57+
initialPath: initialPath,
5658
onFileSelected: onFileSelected ??
5759
(fileName, filePath) {
5860
debugPrint('File selected: $fileName at $filePath');

0 commit comments

Comments
 (0)