Skip to content

Commit

Permalink
Build 3 modifications to increase pub score
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick FINKELSTEIN committed Mar 7, 2019
1 parent 4eb189f commit f5631dc
Show file tree
Hide file tree
Showing 23 changed files with 239 additions and 451 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# strava_flutter specific
secret.dart
todo.txt
# temp
starSegment.dart

.DS_Store
.dart_tool/
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ And Javier for https://javiercbk.github.io/json_to_dart/


License:
strava-flutter is provided under a MIT License. Copyright (c) 2019 Patrick FINKELSTEIN
strava-flutter is provided under a MIT License. Copyright (c) 2019 Patrick FINK
4 changes: 2 additions & 2 deletions example/lib/examples.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import 'package:strava_flutter/Models/summaryActivity.dart';

bool isAuthOk = false;

isAuthOk = await strava.OAuth(clientID, 'activity:write', secret, 'auto');
isAuthOk = await strava.Oauth(clientId, 'activity:write', secret, 'auto');

print('---> Authentication result: $isAuthOk');

Expand Down Expand Up @@ -87,7 +87,7 @@ void example(String secret) async {
secret);
final prompt = 'auto';

isAuthOk = await strava.OAuth(clientID, 'activity:write,profile:read_all', secret, prompt);
isAuthOk = await strava.Oauth(clientId, 'activity:write,profile:read_all', secret, prompt);

if (isAuthOk) {

Expand Down
2 changes: 1 addition & 1 deletion example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// await tester.pumpWidget(MyApp());

// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
Expand Down
62 changes: 28 additions & 34 deletions lib/API/Oauth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class Auth {
// Save also in globals to get direct access
globals.token.accessToken = token;
globals.token.scope = scope;
globals.token.expiresAt =expire;
globals.token.expiresAt = expire;

globals.displayInfo('token saved!!!');
}
Expand All @@ -44,10 +44,10 @@ abstract class Auth {
localToken.expiresAt = prefs.getInt('expire');
localToken.scope = prefs.getString('scope');

// load the data in globals
globals.token.accessToken =localToken.accessToken;
globals.token.expiresAt =localToken.expiresAt;
globals.token.scope =localToken.scope;
// load the data in globals
globals.token.accessToken = localToken.accessToken;
globals.token.expiresAt = localToken.expiresAt;
globals.token.scope = localToken.scope;
} catch (error) {
globals.displayInfo('Error getting the key');
localToken.accessToken = null;
Expand All @@ -58,18 +58,20 @@ abstract class Auth {
if (localToken.expiresAt != null) {
var dateExpired =
DateTime.fromMillisecondsSinceEpoch(localToken.expiresAt);
var _disp = dateExpired.day.toString() + '/' +
dateExpired.month.toString() + ' ' +
dateExpired.hour.toString() + 'hours';

globals.displayInfo('stored token ${localToken.accessToken} expires: $_disp ');
var _disp = dateExpired.day.toString() +
'/' +
dateExpired.month.toString() +
' ' +
dateExpired.hour.toString() +
'hours';

globals.displayInfo(
'stored token ${localToken.accessToken} expires: $_disp ');
}

return (localToken);
}



// Get the code from Strava server
Future<void> getStravaCode(
String clientID, String scope, String prompt) async {
Expand Down Expand Up @@ -117,57 +119,55 @@ abstract class Auth {
});
}

/// Do Strava Authentication.
///
/// Do Strava Authentication.
///
/// Do not do/show the Strava login if a token has been stored previously
/// and is not expired
/// Do/show the Strava login if the scope has been changed since last storage of the token
/// return true if no problem in authentication has been found
Future<bool> OAuth(
Future<bool> Oauth(
String clientID, String scope, String secret, String prompt) async {
print('Welcome to Oauth');
bool isAuthOk = false;
bool isExpired = true;


final Token tokenStored = await getStoredToken();
final String _token = tokenStored.accessToken;

// Check if the token is not expired
// Check if the token is not expired
if (_token != "null") {
globals.displayInfo('token has been stored before! ${tokenStored.accessToken}');
globals.displayInfo(
'token has been stored before! ${tokenStored.accessToken}');

isExpired = isTokenExpired(tokenStored);
globals.displayInfo('isExpired $isExpired');
}


// Check if the scope has changed
// Check if the scope has changed
if ((tokenStored.scope != scope) || (_token == "null") || isExpired) {
// Ask for a new authorization
globals.displayInfo('Doing a new authorization');
isAuthOk = await newAuthorization(clientID, secret, scope, prompt);
isAuthOk = await newAuthorization(clientID, secret, scope, prompt);
} else {
isAuthOk = true;
}

return isAuthOk;
}


Future<bool> newAuthorization(
String clientID, String secret, String scope, String prompt) async {

Future<bool> newAuthorization(
String clientID, String secret, String scope, String prompt) async {
bool returnValue = false;

await getStravaCode(clientID, scope, prompt);

var stravaCode = await onCodeReceived.stream.first;
var stravaCode = await onCodeReceived.stream.first;

if (stravaCode != null) {
var answer = await getStravaToken(clientID, secret, stravaCode);

globals.displayInfo('answer ${answer.expiresAt} , ${answer.accessToken}');
globals
.displayInfo('answer ${answer.expiresAt} , ${answer.accessToken}');

// Save the token information
if (answer.accessToken != null && answer.expiresAt != null) {
Expand All @@ -180,8 +180,6 @@ Future<bool> newAuthorization(
return returnValue;
}



Future<Token> getStravaToken(
String clientID, String secret, String code) async {
Token _answer = Token();
Expand Down Expand Up @@ -230,14 +228,10 @@ Future<bool> newAuthorization(
DateTime.fromMillisecondsSinceEpoch(token.expiresAt);
return (_expiryDate.isBefore(DateTime.now()));
}


Future<void> deAuthorize() async {
String returnValue;

var _token = await getStoredToken();

var _header = globals.createHeader();
var _header = globals.createHeader();
if (_header != null) {
final reqDeAuthorize = "https://www.strava.com/oauth/deauthorize";
var rep = await http.post(reqDeAuthorize, headers: _header);
Expand Down
13 changes: 2 additions & 11 deletions lib/API/clubs.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
// clubs.dart
// clubs.dart
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:async';


import '../Models/summaryAthlete.dart';
import '../Models/summaryActivity.dart';
import '../Models/club.dart';

import 'globals.dart' as globals;



abstract class Clubs {


// Scope needed:
// id of the club
Future<List<SummaryAthlete>> getClubMembersById(String id) async {
Expand All @@ -24,7 +19,6 @@ abstract class Clubs {

var _header = globals.createHeader();


if (_header != null) {
final reqList = "https://www.strava.com/api/v3/clubs/" +
id +
Expand Down Expand Up @@ -55,8 +49,7 @@ abstract class Clubs {
return returnListMembers;
}


Future<Club> getClubById(String id) async {
Future<Club> getClubById(String id) async {
Club returnClub;

var _header = globals.createHeader();
Expand All @@ -81,7 +74,6 @@ Future<Club> getClubById(String id) async {
return returnClub;
}


Future<List<SummaryActivity>> getClubActivitiesById(String id) async {
List<SummaryActivity> returnSummary;

Expand Down Expand Up @@ -117,5 +109,4 @@ Future<Club> getClubById(String id) async {
}
return returnSummary;
}

}
10 changes: 1 addition & 9 deletions lib/API/constants.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
// Constants.dart




final tokenEndpoint = "https://www.strava.com/oauth/token";
final authorizationEndpoint = "https://www.strava.com/oauth/authorize";

final clientID = '32212';
final clientId = '32212';

final String redirectUrl = "http://localhost:8080";





30 changes: 14 additions & 16 deletions lib/API/globals.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
// globals.dart
// globals.dart

import 'package:flutter/foundation.dart';

import 'token.dart';

bool isInDebug = true; // set to true to see debug message in API
bool isInDebug = true; // set to true to see debug message in API

Token token = Token(); // Where the token info is stored when executing APIs
Token token = Token(); // Where the token info is stored when executing APIs

// To display debug info in Strava API
void displayInfo(String message) {
if (isInDebug) {
var msgToDisplay = '--> Strava_flutter: ' + message;
debugPrint(msgToDisplay);
}

if (isInDebug) {
var msgToDisplay = '--> Strava_flutter: ' + message;
debugPrint(msgToDisplay);
}

}

Map<String, String> createHeader() {
var _token = token;
if (_token != null) {
return {'Authorization': 'Bearer ${_token.accessToken}'};
} else {
return {null: null};
}
}
var _token = token;
if (_token != null) {
return {'Authorization': 'Bearer ${_token.accessToken}'};
} else {
return {null: null};
}
}
Loading

0 comments on commit f5631dc

Please sign in to comment.