Skip to content

Commit

Permalink
feat: enable launch_url from repository.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
kkweon committed Jun 21, 2021
1 parent a2619d7 commit a201661
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 18 deletions.
9 changes: 8 additions & 1 deletion client/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,12 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

<!-- The following schemes are recognized to open from the application -->
<key>LSApplicationQueriesSchemes</key>
<array>
<string>https</string>
<string>http</string>
</array>
</dict>
</plist>
</plist>
11 changes: 11 additions & 0 deletions client/lib/utils/extractor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@ String extractYoutubeId(String url) {
final int index = url.lastIndexOf('/');
return url.substring(index + 1);
}

/// extractRepoNameFromURL returns user/repo from https://github.com/user/repo.
///
/// WARNING:
/// It simply gets the last two path from the URL.
/// It does not verify the URL is well formed.
String extractRepoNameFromURL(String url) {
final sublist = url.split("/").toList(growable: false);
if (sublist.length < 2) return url;
return sublist.sublist(sublist.length - 2, sublist.length).join("/");
}
48 changes: 33 additions & 15 deletions client/lib/widgets/detail/repository.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'package:pr12er/protos/pkg/pr12er/messages.pb.dart';
import 'package:pr12er/utils/extractor.dart';
import 'package:url_launcher/url_launcher.dart';

// ignore: must_be_immutable
class RepositoryWidget extends StatelessWidget {
Expand Down Expand Up @@ -30,7 +32,7 @@ class RepositoryWidget extends StatelessWidget {
),
itemCount: visibleRepositories.length,
itemBuilder: (BuildContext context, int index) =>
getItemCard(visibleRepositories[index])))
getItemCard(context, visibleRepositories[index])))
]),
);
}
Expand All @@ -47,22 +49,38 @@ class RepositoryWidget extends StatelessWidget {
}
}

Widget getItemCard(Repository repository) {
return Card(
child: ListTile(
leading: CircleAvatar(
backgroundImage: getImageFramework(repository.framework),
backgroundColor: Colors.transparent,
),
title: Text(
repository.owner,
maxLines: 1,
overflow: TextOverflow.clip,
),
));
Widget getItemCard(BuildContext context, Repository repo) {
return GestureDetector(
onTap: () async {
if (await canLaunch(repo.url)) {
await launch(repo.url);
return;
}
ScaffoldMessenger.of(context).removeCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
backgroundColor: Theme.of(context).colorScheme.error,
content: Text("${repo.url} is not a valid URL"),
action: SnackBarAction(
label: "OK",
onPressed: () {},
textColor: Theme.of(context).colorScheme.onError,
),
));
},
child: Card(
child: ListTile(
leading: CircleAvatar(
backgroundImage: getImageFramework(repo.framework),
backgroundColor: Colors.transparent,
),
title: Text(
extractRepoNameFromURL(repo.url),
),
)),
);
}

List<Repository> getSubsetRepositories(int size) {
return repositories.sublist(0, size);
}
}
}
2 changes: 1 addition & 1 deletion client/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ packages:
source: hosted
version: "1.3.0"
url_launcher:
dependency: transitive
dependency: "direct main"
description:
name: url_launcher
url: "https://pub.dartlang.org"
Expand Down
1 change: 1 addition & 0 deletions client/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies:
protobuf: ^2.0.0
provider: ^5.0.0
share_plus: ^2.1.4
url_launcher: ^6.0.6
youtube_player_flutter: ^8.0.0
youtube_player_iframe: ^2.0.0

Expand Down
14 changes: 13 additions & 1 deletion client/test/utils/extractor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@ void main() {
expect(extractYoutubeId(url), "video-id");
});
});
}

group("extractRepoNameFromURL", () {
test("returns user/name for github URL", () {
const url = "https://github.com/codingpot/pr12er";
expect(extractRepoNameFromURL(url), "codingpot/pr12er");
});

test("returns the input string for invalid URL", () {
const url = "pr12er";
expect(extractRepoNameFromURL(url), url);
});
});
}
4 changes: 4 additions & 0 deletions server/pkg/serv/serv.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@ func (s Server) GetDetails(_ context.Context, in *pr12er.GetDetailsRequest) (*pr
{
Framework: pr12er.Framework_FRAMEWORK_TENSORFLOW,
Owner: "goodfeli",
Url: "https://github.com/tensorflow/tensorflow",
},
{
Framework: pr12er.Framework_FRAMEWORK_PYTORCH,
Owner: "eriklindernoren",
Url: "https://github.com/pytorch/pytorch",
},
{
Framework: pr12er.Framework_FRAMEWORK_TENSORFLOW,
Owner: "google-research",
Url: "https://github.com/tensorflow/tensorflow",
},
{
Framework: pr12er.Framework_FRAMEWORK_PYTORCH,
Owner: "eriklindernoren",
Url: "https://github.com/pytorch/pytorch",
},
},
},
Expand Down

0 comments on commit a201661

Please sign in to comment.