Skip to content

Commit

Permalink
lets try and import on web
Browse files Browse the repository at this point in the history
  • Loading branch information
Desync-o-tron committed Oct 1, 2024
1 parent 9c22917 commit da161ea
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
17 changes: 13 additions & 4 deletions lib/DOM/history_importing_logic.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import 'dart:io';
import 'package:csv/csv.dart';
import 'package:flutter/foundation.dart';
import 'package:intl/intl.dart';
import 'training_metadata.dart';
import 'exercise_metadata.dart';

List<TrainingSession> importStrongCsv(String filePath, Units units) {
final List<String> rows = File(filePath).readAsLinesSync();
List<TrainingSession> importStrongCsv(String filepathORfileStr, Units units) {
final List<String> rows;
if (kIsWeb) {
rows = filepathORfileStr.split("\n");
if (rows.last.isEmpty) {
rows.removeLast();
}
} else {
rows = File(filepathORfileStr).readAsLinesSync();
}
rows.removeAt(0);

//todo we should save teh header & compare it to see if it ever changes and bricks shit
//todo save the header.. if it ever changes, lets make a pub/sub topic on gcs or some error medium to lmk!!!
List<TrainingSession> sessions = [];
Expand All @@ -26,8 +36,7 @@ List<TrainingSession> importStrongCsv(String filePath, Units units) {

for (int i = 0; i < rows.length; i++) {
var row = rows[i];
final rowList = const CsvToListConverter()
.convert(row, shouldParseNumbers: false); // as List<String>;
var rowList = const CsvToListConverter().convert(row, shouldParseNumbers: false);
final date = DateFormat("yyyy-MM-dd HH:mm:ss").parse(rowList[0][0]);
final workoutName = rowList[0][1];
final duration = parseStrongWorkoutDuration(rowList[0][2]);
Expand Down
2 changes: 1 addition & 1 deletion lib/cloud_io/firestore_sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class ExercisesCubit extends Cubit<ExercisesState> {
}

Future<void> removeExercises(List<Exercise> exercisesToRemove) async {
// Implement the method
//todo Implement the method
// For now, let's emit an error
emit(ExercisesError("removeExercises method not implemented"));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/importing/import_matching_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class _ImportInspectionPageState extends State<ImportInspectionPage> {
} else {
exercisesState = exercisesState as ExercisesLoaded;
}
matchPairs = _exerciseMatcher(newExs, exercisesState, 90);
matchPairs = _exerciseMatcher(newExs, exercisesState, 86);
return Scaffold(
appBar: AppBar(
title: Text(
Expand Down
15 changes: 9 additions & 6 deletions lib/importing/import_training_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class _ImportTrainingDataPageState extends State<ImportTrainingDataPage> {
late MassUnits selectedMassUnit;
late DistanceUnits selectedDistanceUnit;
bool setAsStandard = true;
String? filepath;
String? filepathORfileStr;

@override
void initState() {
Expand All @@ -76,15 +76,17 @@ class _ImportTrainingDataPageState extends State<ImportTrainingDataPage> {
mainAxisSize: MainAxisSize.min,
children: [
MyGenericButton(
label: (filepath == null) ? "Select file to Import" : "File Selected.",
label: (filepathORfileStr == null)
? "Select file to Import"
: "File Selected.",
onPressed: () async {
//todo for web?
// https://github.com/miguelpruivo/flutter_file_picker/wiki/FAQ
filepath =
filepathORfileStr =
await getFileWithSnackbarErrors(context, ['csv', 'txt', 'json']);
setState(() {});
},
color: (filepath == null) ? darkTan : mediumGreen,
color: (filepathORfileStr == null) ? darkTan : mediumGreen,
),
const SizedBox(height: 16),
DropdownButtonFormField<MassUnits>(
Expand Down Expand Up @@ -143,13 +145,14 @@ class _ImportTrainingDataPageState extends State<ImportTrainingDataPage> {
Navigator.of(context).pop();
},
onComplete: () {
if (filepath == null) {
if (filepathORfileStr == null) {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text('No file selected')));
return;
}

List<TrainingSession> sessions = importStrongCsv(filepath!, units);
List<TrainingSession> sessions =
importStrongCsv(filepathORfileStr!, units);

if (setAsStandard) {
var userInfoCubit = context.read<BasicUserInfoCubit>();
Expand Down
16 changes: 13 additions & 3 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'dart:convert';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:open_fitness_tracker/DOM/exercise_metadata.dart';

typedef Exercises = List<Exercise>;
Expand Down Expand Up @@ -167,25 +168,34 @@ String enumToReadableString(Object o) {
.toLowerCase();
}

/// will return the whole file as a string if on web.
Future<String?> getFileWithSnackbarErrors(
BuildContext context, List<String> allowedExtensions) async {
String? filePath;
var scaffoldMessenger = ScaffoldMessenger.of(context);

FilePickerResult? result = await FilePicker.platform
.pickFiles(type: FileType.custom, allowedExtensions: allowedExtensions);

if (result == null) {
if (result == null && result!.files.isNotEmpty) {
scaffoldMessenger.showSnackBar(
const SnackBar(content: Text('No file selected')),
);
return null;
}

String? filePath = result.files.single.path;
if (kIsWeb) {
final fileBytes = result.files.first.bytes;
return utf8.decode(fileBytes!);
}

filePath = result.files.single.path;
if (filePath == null) {
scaffoldMessenger.showSnackBar(
const SnackBar(content: Text('No file selected. Is the path accessable?')),
);
return null;
}

return filePath;
}

0 comments on commit da161ea

Please sign in to comment.