Commit b2c90635 authored by Administrator's avatar Administrator

.fit's cannot be downloaded via api, so I have to scrape the web page...

parent d5be7e7f
import 'package:encrateia/models/fit_download.dart';
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:encrateia/utils/db.dart';
......@@ -33,10 +34,11 @@ class Activity extends Model {
name = dbActivity.name,
movingTime = Duration(seconds: dbActivity.movingTime),
type = dbActivity.type,
distance = dbActivity.distance;
distance = dbActivity.distance,
state = dbActivity.state;
download() {
// TODO
download() async {
FitDownload.byId(stravaId.toString());
}
persist() async {
......@@ -66,7 +68,7 @@ class Activity extends Model {
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
final startDate = now - 2 * 86400;
final startDate = now - 20 * 86400;
List<SummaryActivity> summaryActivities =
await strava.getLoggedInAthleteActivities(now, startDate);
......@@ -75,7 +77,5 @@ class Activity extends Model {
Activity activity = Activity.fromStrava(summaryActivity);
activity.persist();
}
print("Hello");
}
}
import 'dart:io';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'package:path_provider/path_provider.dart';
abstract class FitDownload {
static byId(String id) async {
final uri = 'https://www.strava.com/activities/#{id}/export_original';
var rep = await http.get(uri, headers: _header);
if (rep.statusCode == 200) {
final file = await _localFile(id);
file.writeAsString(rep.body);
return rep.contentLength.toString() + " Bytes written";
} else {
return rep.statusCode.toString() + rep.reasonPhrase;
}
}
static Future<File> _localFile(id) async {
final directory = await getApplicationDocumentsDirectory();
File file = File(directory.path + id + '.fit');
return file;
}
}
......@@ -40,7 +40,9 @@ class _ListActivitiesScreenState extends State<ListActivitiesScreen> {
leading: Icon(Icons.cloud_download),
title: Text("Download Activities from Strava"),
onTap: () {
Activity.queryStrava();
setState(() {
Activity.queryStrava();
});
},
),
for (Activity activity in activities)
......@@ -61,7 +63,9 @@ class _ListActivitiesScreenState extends State<ListActivitiesScreen> {
leading: Icon(Icons.cloud_download),
title: Text("Download Activities from Strava"),
onTap: () {
Activity.queryStrava();
setState(() {
Activity.queryStrava();
});
},
),
],
......@@ -77,7 +81,7 @@ class _ListActivitiesScreenState extends State<ListActivitiesScreen> {
case "new":
return IconButton(
icon: Icon(Icons.cloud_download),
onPressed: () => activity.download,
onPressed: () => activity.download(),
);
break;
default:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment