diff --git a/lib/apis/githost.dart b/lib/apis/githost.dart new file mode 100644 index 00000000..d4c12621 --- /dev/null +++ b/lib/apis/githost.dart @@ -0,0 +1,22 @@ +import 'dart:async'; + +abstract class GitHost { + void init(Function oAuthCallback); + Future launchOAuthScreen(); + + Future> listRepos(); + Future createRepo(String name); + Future addDeployKey(String sshPublicKey, String repo); +} + +class GitRepo { + String fullName; + String cloneUrl; + + GitRepo({this.fullName, this.cloneUrl}); + + @override + String toString() { + return 'GitRepo{fulleName: $fullName, cloneUrl: $cloneUrl}'; + } +} diff --git a/lib/apis/github.dart b/lib/apis/github.dart index 1e7e1859..785819a0 100644 --- a/lib/apis/github.dart +++ b/lib/apis/github.dart @@ -2,18 +2,20 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; -import 'package:http/http.dart' as http; import 'package:flutter/services.dart'; - +import 'package:http/http.dart' as http; import 'package:url_launcher/url_launcher.dart'; -class GitHub { +import 'githost.dart'; + +class GitHub implements GitHost { static const _clientID = "aa3072cbfb02b1db14ed"; static const _clientSecret = "010d303ea99f82330f2b228977cef9ddbf7af2cd"; var _platform = const MethodChannel('gitjournal.io/git'); var _accessCode = ""; + @override void init(Function callback) { Future _handleMessages(MethodCall call) async { if (call.method != "onURL") { @@ -57,6 +59,7 @@ class GitHub { return map["access_token"]; } + @override Future launchOAuthScreen() async { // FIXME: Add some 'state' over here! @@ -66,7 +69,8 @@ class GitHub { return launch(url); } - Future> listRepos() async { + @override + Future> listRepos() async { if (_accessCode.isEmpty) { throw "GitHub Access Code Missing"; } @@ -84,10 +88,10 @@ class GitHub { } List list = jsonDecode(response.body); - List repos = new List(); + var repos = new List(); list.forEach((dynamic d) { var map = Map.from(d); - var repo = Repo.fromJson(map); + var repo = _repoFromJson(map); repos.add(repo); }); @@ -95,8 +99,9 @@ class GitHub { return repos; } -// FIXME: Proper error when the repo exists! - Future createRepo(String name) async { + @override + Future createRepo(String name) async { + // FIXME: Proper error when the repo exists! if (_accessCode.isEmpty) { throw "GitHub Access Code Missing"; } @@ -123,9 +128,10 @@ class GitHub { print("GitHub createRepo: " + response.body); var map = json.decode(response.body); - return Repo.fromJson(map); + return _repoFromJson(map); } + // FIXME: Proper error when the repo exists! Future addDeployKey(String sshPublicKey, String repo) async { if (_accessCode.isEmpty) { throw "GitHub Access Code Missing"; @@ -157,22 +163,11 @@ class GitHub { print("GitHub addDeployKey: " + response.body); return json.decode(response.body); } -} -class Repo { - String fullName; - String cloneUrl; - - Repo({this.fullName, this.cloneUrl}); - factory Repo.fromJson(Map parsedJson) { - return new Repo( + GitRepo _repoFromJson(Map parsedJson) { + return new GitRepo( fullName: parsedJson['full_name'], cloneUrl: parsedJson['ssh_url'], ); } - - @override - String toString() { - return 'Repo{fulleName: $fullName}'; - } } diff --git a/lib/apis/gitlab.dart b/lib/apis/gitlab.dart index 1b7ec716..2fea1cad 100644 --- a/lib/apis/gitlab.dart +++ b/lib/apis/gitlab.dart @@ -1,24 +1,15 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; - -import 'package:http/http.dart' as http; -import 'package:flutter/services.dart'; - -import 'package:url_launcher/url_launcher.dart'; - import 'dart:math'; -String _randomString(int length) { - var rand = new Random(); - var codeUnits = new List.generate(length, (index) { - return rand.nextInt(33) + 89; - }); +import 'package:flutter/services.dart'; +import 'package:http/http.dart' as http; +import 'package:url_launcher/url_launcher.dart'; - return new String.fromCharCodes(codeUnits); -} +import 'githost.dart'; -class Gitlab { +class GitLab implements GitHost { static const _clientID = "faf33c3716faf05bfb701b1b31e36c83a23c3ec2d7161f4ff00fba2275524d09"; @@ -26,6 +17,7 @@ class Gitlab { var _accessCode = ""; var _stateOAuth = ""; + @override void init(Function callback) { Future _handleMessages(MethodCall call) async { if (call.method != "onURL") { @@ -57,6 +49,7 @@ class Gitlab { print("GitLab: Installed Handler"); } + @override Future launchOAuthScreen() async { _stateOAuth = _randomString(10); @@ -65,7 +58,8 @@ class Gitlab { return launch(url); } - Future> listRepos() async { + @override + Future> listRepos() async { if (_accessCode.isEmpty) { throw "GitHub Access Code Missing"; } @@ -84,10 +78,10 @@ class Gitlab { } List list = jsonDecode(response.body); - List repos = new List(); + var repos = new List(); list.forEach((dynamic d) { var map = Map.from(d); - var repo = GitLabRepo.fromJson(map); + var repo = _repoFromJson(map); repos.add(repo); }); @@ -95,8 +89,9 @@ class Gitlab { return repos; } -// FIXME: Proper error when the repo exists! - Future createRepo(String name) async { + @override + Future createRepo(String name) async { + // FIXME: Proper error when the repo exists! if (_accessCode.isEmpty) { throw "GitLab Access Code Missing"; } @@ -123,9 +118,10 @@ class Gitlab { print("GitLab createRepo: " + response.body); var map = json.decode(response.body); - return GitLabRepo.fromJson(map); + return _repoFromJson(map); } + @override Future addDeployKey(String sshPublicKey, String repo) async { if (_accessCode.isEmpty) { throw "GitLab Access Code Missing"; @@ -158,22 +154,20 @@ class Gitlab { print("GitLab addDeployKey: " + response.body); return json.decode(response.body); } -} -class GitLabRepo { - String fullName; - String cloneUrl; - - GitLabRepo({this.fullName, this.cloneUrl}); - factory GitLabRepo.fromJson(Map parsedJson) { - return new GitLabRepo( + GitRepo _repoFromJson(Map parsedJson) { + return new GitRepo( fullName: parsedJson['path_with_namespace'], cloneUrl: parsedJson['ssh_url_to_repo'], ); } - - @override - String toString() { - return 'GitLabRepo{fulleName: $fullName, cloneUrl: $cloneUrl}'; - } +} + +String _randomString(int length) { + var rand = new Random(); + var codeUnits = new List.generate(length, (index) { + return rand.nextInt(33) + 89; + }); + + return new String.fromCharCodes(codeUnits); } diff --git a/lib/oauthapp.dart b/lib/oauthapp.dart index d3163638..6ddc5d07 100644 --- a/lib/oauthapp.dart +++ b/lib/oauthapp.dart @@ -1,7 +1,8 @@ import 'package:flutter/material.dart'; -import 'package:journal/apis/github.dart'; -import 'package:journal/apis/gitlab.dart'; +import 'apis/githost.dart'; +import 'apis/github.dart'; +import 'apis/gitlab.dart'; class OAuthApp extends StatefulWidget { @override @@ -14,11 +15,18 @@ var key = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+VAh8r+vn0c+M+DacOo/szXcdMpxO1kIO3USkzgE5XdO83kQdDwh4Xc4P3dcc+FFSfVcEl3mSXGKbYC3G0ZoVcWd4ed40Gt3sLHSfNRQlRv+obnqKbzDLuOGfq65EkaJ90vrWBo/k7K8tBC2j1FZ/PUYy3DxeQkPEZXCMZDSG5P/+XoHn5IPcaxDpvlZjtOrx4H3pQ/YVI+XmyFAsZe+/Shy5sg4ilsdo4BQN2nODuBLwmgYu/hHmCcd8t4OxgBANVN8TMqHnZfRLixRSuXn0DbV4YOa/b2lBFQNvjkoBF6KhXOxZ+awyjyTpNp4AgF5c+3xptkNwUlwiQDCzcUmH your_email@example.com'; class OAuthAppState extends State { - var github = new Gitlab(); + GitHost githost; void initState() { super.initState(); - github.init(() { + + if (true) { + githost = new GitHub(); + } else { + githost = new GitLab(); + } + + githost.init(() { print("GitHub initialized and has access code"); }); } @@ -35,14 +43,14 @@ class OAuthAppState extends State { RaisedButton( child: Text("Open OAuth URL"), onPressed: () { - github.launchOAuthScreen(); + githost.launchOAuthScreen(); }, ), RaisedButton( child: Text("List Repos"), onPressed: () async { try { - var repos = await github.listRepos(); + var repos = await githost.listRepos(); for (var repo in repos) { print(repo); } @@ -55,7 +63,7 @@ class OAuthAppState extends State { child: Text("Create Repo"), onPressed: () async { try { - var repo = await github.createRepo("journal_test2"); + var repo = await githost.createRepo("journal_test2"); print(repo); } catch (err) { print("Create Repo: " + err.toString()); @@ -66,7 +74,7 @@ class OAuthAppState extends State { child: Text("Add Deploy Key"), onPressed: () async { try { - await github.addDeployKey(key, "vhanda/journal_test2"); + await githost.addDeployKey(key, "vhanda/journal_test2"); } catch (err) { print("Deploy Key: " + err.toString()); } diff --git a/lib/storage/notes_repository.dart b/lib/storage/notes_repository.dart index 675d273c..9acc2765 100644 --- a/lib/storage/notes_repository.dart +++ b/lib/storage/notes_repository.dart @@ -1,6 +1,6 @@ import 'dart:async'; -import 'package:flutter/foundation.dart'; +import 'package:flutter/foundation.dart'; import 'package:journal/note.dart'; class NoteRepoResult {