Skip to content

Commit 4ad768c

Browse files
committed
feat: dart2 port
1 parent 737c3ed commit 4ad768c

19 files changed

+494
-222
lines changed

.analysis_option

-2
This file was deleted.

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ tmp.*
77
build
88
.idea/
99
.pub/
10+
11+
local.*

.travis.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: dart
2+
sudo: required
3+
dist: trusty
4+
dart:
5+
- '2.4.0'
6+
- stable
7+
- dev
8+
before_script:
9+
- source $(pub run chrome_travis:env_rc)
10+
script:
11+
- dart tool/travis.dart

analysis_options.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# tekartik analysis_options extension to pedantic
2+
include: package:tekartik_common_utils/pedantic/analysis_options.strong_mode.yaml

example/google_jsapi_picker_example.dart

+89-80
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,80 @@
11
library google_jsapi_example;
22

3+
import 'dart:async';
34
import 'dart:html';
5+
6+
import 'package:googleapis_auth/auth_browser.dart';
7+
import 'package:synchronized/synchronized.dart';
8+
import 'package:tekartik_browser_utils/browser_utils_import.dart';
49
import 'package:tekartik_google_jsapi/gapi.dart';
5-
import 'package:tekartik_google_jsapi/gapi_auth.dart';
10+
import 'package:tekartik_google_jsapi/gapi_auth2.dart';
611
import 'package:tekartik_google_jsapi_picker/picker.dart';
7-
import 'dart:async';
812

9-
GapiAuth gapiAuth;
13+
import 'test_config.dart';
14+
import 'test_setup.dart';
15+
16+
GapiAuth2 gapiAuth;
1017
GooglePicker gpicker;
1118

1219
Storage storage = window.localStorage;
1320

14-
String _STORAGE_KEY_PREF = 'com.tekartik.google_jsapi_picker_example';
15-
dynamic storageGet(String key) {
16-
return storage['$_STORAGE_KEY_PREF.$key'];
21+
String storageKeyPref = 'com.tekartik.google_jsapi_picker_example';
22+
23+
String storageGet(String key) {
24+
return storage['$storageKeyPref.$key'];
1725
}
1826

1927
void storageSet(String key, String value) {
28+
var prefKey = '$storageKeyPref.$key';
2029
if (value == null) {
21-
storage.remove(key);
30+
storage.remove(prefKey);
2231
} else {
23-
storage['$_STORAGE_KEY_PREF.$key'] = value;
32+
storage[prefKey] = value;
2433
}
2534
}
2635

27-
String AUTH_AUTO_AUTH = 'auth_autoauth'; // boolean
28-
String AUTH_APPROVAL_PROMPT = 'auth_approval_prompt'; // boolean
29-
String CLIENT_ID_KEY = 'client_id';
30-
String DEVELOPER_KEY_KEY = 'developer_key';
31-
String MIME_TYPES_KEY = 'mime_types';
32-
String SELECT_FOLDER_ENABLED_KEY = 'select_folder_enabled';
33-
String INCLUDE_FOLDERS_KEY = 'include_folders';
36+
String appAuthAutoAuth = 'auth_autoauth'; // boolean
37+
String appAuthApprovalPromptKey = 'auth_approval_prompt'; // boolean
38+
String appMimeTypesKey = 'mime_types';
39+
String selectFolderEnabledKey = 'select_folder_enabled';
40+
String includeFoldersKey = 'include_folders';
3441

35-
InputElement developerKeyInput;
3642
Element pickResult;
3743
String _authToken;
3844
InputElement mimeTypesInput;
3945

4046
void _pick() {
4147
String mimeTypesText = mimeTypesInput.value;
42-
storageSet(MIME_TYPES_KEY, mimeTypesText);
48+
storageSet(appMimeTypesKey, mimeTypesText);
4349

44-
String developerKey = developerKeyInput.value;
45-
storageSet(DEVELOPER_KEY_KEY, developerKey);
46-
47-
PickerBuilder builder = new PickerBuilder(gpicker);
50+
PickerBuilder builder = PickerBuilder(gpicker);
4851

4952
PickerView pickerView;
5053

5154
bool selectFolderEnabled =
52-
storageGet(SELECT_FOLDER_ENABLED_KEY) == true.toString();
53-
bool includeFolders = storageGet(INCLUDE_FOLDERS_KEY) == true.toString();
55+
storageGet(selectFolderEnabledKey) == true.toString();
56+
bool includeFolders = storageGet(includeFoldersKey) == true.toString();
5457

5558
print('selectFolderEnbled: $selectFolderEnabled');
5659
print('includeFolders: $includeFolders');
5760
// use docs view for folder
5861
if (selectFolderEnabled || includeFolders) {
5962
PickerDocsView pickerDocsView =
60-
new PickerDocsView(gpicker, gpicker.viewId.DOCS);
63+
PickerDocsView(gpicker, gpicker.viewId.docs);
6164
pickerDocsView.selectFolderEnabled = true;
6265
pickerDocsView.includeFolders = true;
6366
pickerView = pickerDocsView;
6467
} else {
65-
pickerView = new PickerView(gpicker, gpicker.viewId.DOCS);
68+
pickerView = PickerView(gpicker, gpicker.viewId.docs);
6669
}
6770
List<String> mimeTypes = mimeTypesText.split(',');
68-
if (mimeTypes.length >= 1 && mimeTypes[0].length > 0) {
71+
if (mimeTypes.isNotEmpty && mimeTypes[0].isNotEmpty) {
6972
pickerView.mimeTypes = mimeTypes;
7073
}
7174

7275
builder.addView(pickerView);
7376

74-
builder.developerKey = developerKey;
77+
builder.developerKey = appOptions.developerKey;
7578
builder.oauthToken = _authToken;
7679
Picker uiPicker = builder.build();
7780
uiPicker.pick().then((PickerDataDocuments docs) {
@@ -82,35 +85,36 @@ void _pick() {
8285
}
8386

8487
void pickerMain(String authToken) {
88+
authorizeResult.innerHtml = 'Authorize token $authToken';
89+
print('token: $authToken');
8590
_authToken = authToken;
8691

8792
Element pickerForm = querySelector('form.app-picker');
8893
pickResult = pickerForm.querySelector('.app-result');
8994
pickerForm.classes.remove('hidden');
90-
developerKeyInput = pickerForm.querySelector('input#appInputDeveloperKey');
91-
mimeTypesInput = pickerForm.querySelector('input#appInputMimeTypes');
95+
mimeTypesInput =
96+
pickerForm.querySelector('input#appInputMimeTypes') as InputElement;
9297
Element pickButton = pickerForm.querySelector('button.app-pick');
9398

94-
CheckboxInputElement selectFolderEnabledInput =
95-
pickerForm.querySelector('#appInputSelectFolderEnabled');
99+
final selectFolderEnabledInput = pickerForm
100+
.querySelector('#appInputSelectFolderEnabled') as CheckboxInputElement;
96101
bool selectFolderEnabled =
97-
storageGet(SELECT_FOLDER_ENABLED_KEY) == true.toString();
102+
storageGet(selectFolderEnabledKey) == true.toString();
98103
selectFolderEnabledInput.checked = selectFolderEnabled;
99104
selectFolderEnabledInput.onChange.listen((_) {
100105
storageSet(
101-
SELECT_FOLDER_ENABLED_KEY, selectFolderEnabledInput.checked.toString());
106+
selectFolderEnabledKey, selectFolderEnabledInput.checked.toString());
102107
});
103108

104-
CheckboxInputElement includeFoldersInput =
105-
pickerForm.querySelector('#appInputIncludeFolders');
106-
bool includeFolders = storageGet(INCLUDE_FOLDERS_KEY) == true.toString();
109+
final includeFoldersInput = pickerForm
110+
.querySelector('#appInputIncludeFolders') as CheckboxInputElement;
111+
bool includeFolders = storageGet(includeFoldersKey) == true.toString();
107112
includeFoldersInput.checked = includeFolders;
108113
includeFoldersInput.onChange.listen((_) {
109-
storageSet(INCLUDE_FOLDERS_KEY, includeFoldersInput.checked.toString());
114+
storageSet(includeFoldersKey, includeFoldersInput.checked.toString());
110115
});
111116

112-
developerKeyInput.value = storageGet(DEVELOPER_KEY_KEY);
113-
mimeTypesInput.value = storageGet(MIME_TYPES_KEY);
117+
mimeTypesInput.value = storageGet(appMimeTypesKey);
114118

115119
pickButton.onClick.listen((Event event) {
116120
event.preventDefault();
@@ -119,66 +123,71 @@ void pickerMain(String authToken) {
119123
}
120124

121125
Element authorizeResult;
122-
InputElement clientIdInput;
123-
124-
void _authorize() {
125-
String clientId = clientIdInput.value;
126-
if (clientId.length < 1) {
127-
authorizeResult.innerHtml = 'Missing CLIENT ID';
128-
return;
126+
BrowserOAuth2Flow auth2flow;
127+
AppOptions appOptions;
128+
final _setupLock = Lock();
129+
130+
Future configSetup() async {
131+
if (auth2flow == null) {
132+
await _setupLock.synchronized(() async {
133+
if (auth2flow == null) {
134+
appOptions = await setup();
135+
136+
String clientId = appOptions.clientId;
137+
String clientSecret = appOptions.clientSecret;
138+
if (clientId?.isNotEmpty != true ||
139+
clientSecret?.isNotEmpty != true ||
140+
appOptions.developerKey?.isNotEmpty != true) {
141+
authorizeResult.innerHtml =
142+
'Missing clientId, clientSecret or developerKey';
143+
authorizeResult.innerHtml =
144+
'Create local.config.yaml from sample.local.config.yaml';
145+
return;
146+
}
147+
148+
var authClientId =
149+
ClientId(appOptions.clientId, appOptions.clientSecret);
150+
List<String> scopes = [GooglePicker.scopeDriveAppFile];
151+
152+
auth2flow?.close();
153+
auth2flow = await createImplicitBrowserFlow(authClientId, scopes);
154+
}
155+
});
129156
}
130-
storageSet(CLIENT_ID_KEY, clientId);
131-
132-
String approvalPrompt = storageGet(AUTH_APPROVAL_PROMPT);
133-
List<String> scopes = [GooglePicker.SCOPE_DRIVE_APP_FILE];
134-
gapiAuth
135-
.authorize(clientId, scopes, approvalPrompt: approvalPrompt)
136-
.then((String oauthToken) {
137-
authorizeResult.innerHtml =
138-
"client id '$clientId' authorized for '$scopes'";
139-
pickerMain(oauthToken);
140-
});
157+
}
158+
159+
Future _authorize({bool auto}) async {
160+
auto ??= false;
161+
await configSetup();
162+
163+
var result = await auth2flow.runHybridFlow(immediate: auto);
164+
var oauthToken = result.credentials.accessToken.data;
165+
pickerMain(oauthToken);
141166
}
142167

143168
void authMain() {
144169
Element authForm = querySelector('form.app-auth');
145170
authForm.classes.remove('hidden');
146171
Element authorizeButton = authForm.querySelector('button.app-authorize');
147-
clientIdInput = authForm.querySelector('input#appInputClientId');
148172

149173
authorizeResult = authForm.querySelector('.app-result');
150-
CheckboxInputElement approvalPromptCheckbox =
151-
authForm.querySelector('.app-approval-prompt');
152-
CheckboxInputElement autoAuthCheckbox =
153-
authForm.querySelector('.app-autoauth');
154-
155-
clientIdInput.value = storageGet(CLIENT_ID_KEY);
156-
157-
String approvalPrompt = storageGet(AUTH_APPROVAL_PROMPT);
158-
159-
approvalPromptCheckbox.checked =
160-
(approvalPrompt == GapiAuth.APPROVAL_PROMPT_FORCE);
174+
final autoAuthCheckbox =
175+
authForm.querySelector('.app-autoauth') as CheckboxInputElement;
161176

162177
authorizeButton.onClick.listen((Event event) {
163178
event.preventDefault();
164179
_authorize();
165180
});
166181

167-
approvalPromptCheckbox.onChange.listen((_) {
168-
approvalPrompt =
169-
approvalPromptCheckbox.checked ? GapiAuth.APPROVAL_PROMPT_FORCE : null;
170-
storageSet(AUTH_APPROVAL_PROMPT, approvalPrompt);
171-
});
172-
173-
bool autoAuth = storageGet(AUTH_AUTO_AUTH) == true.toString();
182+
bool autoAuth = storageGet(appAuthAutoAuth) == true.toString();
174183

175184
autoAuthCheckbox.onChange.listen((_) {
176-
storageSet(AUTH_AUTO_AUTH, autoAuthCheckbox.checked.toString());
185+
storageSet(appAuthAutoAuth, autoAuthCheckbox.checked.toString());
177186
});
178187

179188
autoAuthCheckbox.checked = autoAuth;
180189
if (autoAuth) {
181-
_authorize();
190+
_authorize(auto: true);
182191
}
183192
}
184193

@@ -189,7 +198,7 @@ Future _loadPicker() async {
189198
try {
190199
Gapi gapi = await loadGapiPlatform();
191200
loadGapiResult.innerHtml = 'loading GapiAuth...';
192-
gapiAuth = await loadGapiAuth(gapi);
201+
gapiAuth = await loadGapiAuth2(gapi);
193202
loadGapiResult.innerHtml = 'loading GooglePicker...';
194203
gpicker = await loadPicker(gapi);
195204
loadGapiResult.innerHtml = 'GooglePicker loaded';
@@ -200,7 +209,7 @@ Future _loadPicker() async {
200209
authMain();
201210
}
202211

203-
main() async {
212+
Future main() async {
204213
Element loadGapiForm = querySelector('form.app-gapi');
205214
loadGapiResult = loadGapiForm.querySelector('.app-result');
206215

example/google_jsapi_picker_example.html

+6-30
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<!-- include the stylesheets -->
7-
<link rel="stylesheet" href="packages/bootstrap/css/bootstrap.css">
8-
<link rel="stylesheet" href="packages/bootstrap/css/bootstrap-theme.css">
7+
<link rel="stylesheet" href="packages/tekartik_bootstrap_asset/3.3.7/css/bootstrap.css">
8+
<link rel="stylesheet" href="packages/tekartik_bootstrap_asset/3.3.7/css/bootstrap-theme.css">
99
<title>Google jsapi picker example</title>
1010
</head>
1111

@@ -25,22 +25,7 @@ <h2>Auth</h2>
2525
on start
2626
</label>
2727
</div>
28-
<p>
29-
Make sure you have created a project in your <a
30-
href="https://console.developers.google.com/">developer
31-
console</a>. Ensure the APIs 'Drive API', 'Drive SDK' and 'Google Picker API' have been enabled for this project
32-
</p>
33-
<div class="form-group">
34-
<label for="appInputClientId">CLIENT ID</label> <input
35-
class="form-control" id="appInputClientId"
36-
placeholder="Enter client id from developer console">
37-
</div>
3828

39-
<div class="checkbox">
40-
<label> <input type="checkbox"
41-
class="app-approval-prompt">Force approval prompt
42-
</label>
43-
</div>
4429
<div class="form-group">
4530
<button class="btn btn-primary app-authorize">Authorize</button>
4631
</div>
@@ -56,15 +41,9 @@ <h2>Picker</h2>
5641
console</a>
5742
</p>
5843

59-
<div class="form-group">
60-
<label for="appInputDeveloperKey">DEVELOPER KEY</label> <input
61-
class="form-control" id="appInputDeveloperKey"
62-
placeholder="Your developer key from developer console">
63-
</div>
64-
6544
<div class="form-group">
6645
<label for="appInputMimeTypes">mime types (comma
67-
separated - plain/text)</label> <input class="form-control"
46+
separated - plain/text, application/pdf, ...)</label> <input class="form-control"
6847
id="appInputMimeTypes" placeholder="The mime types to open">
6948
</div>
7049

@@ -88,14 +67,11 @@ <h2>Picker</h2>
8867
</form>
8968
</div>
9069

91-
<script type="application/dart" src="google_jsapi_picker_example.dart"></script>
92-
<!-- for this next line to work, your pubspec.yaml file must have a dependency on 'browser' -->
93-
<script src="packages/browser/dart.js"></script>
94-
<script src="packages/browser/interop.js"></script>
70+
<script src="google_jsapi_picker_example.dart.js"></script>
9571

9672

9773
<!-- include the javascript -->
98-
<script src="packages/bootstrap/jquery/jquery.js"></script>
99-
<script src="packages/bootstrap/js/bootstrap.js"></script>
74+
<script src="packages/tekartik_jquery_asset/3.1.1/jquery.js"></script>
75+
<script src="packages/tekartik_bootstrap_asset/3.3.7/js/bootstrap.js"></script>
10076
</body>
10177
</html>

example/sample.local.config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copy this file as "local.config.yaml" in the same folder (.gitignored)
2+
3+
# The developer key needed for the picker API
4+
developerKey = [ENTER_YOUR_DEVELOPER_KEY];
5+
# The Client ID obtained from the Google Cloud Console.
6+
cliendId = [ENTER_YOUR_CLIENT_ID.apps.googleusercontent.com];

0 commit comments

Comments
 (0)