feat: load last commit time from Github API.

This commit is contained in:
Aunali321 2022-08-01 23:36:27 +05:30
parent d4dc845e00
commit f31641312b
2 changed files with 60 additions and 5 deletions

View file

@ -6,12 +6,42 @@ class GithubAPI {
Future latestRelease(String org, repoName) async {
var latestRelease = await github.repositories
.getLatestRelease(RepositorySlug(org, repoName));
var dlurl = latestRelease.assets?.first.browserDownloadUrl;
var dlurl = latestRelease.assets
?.firstWhere((element) =>
element.browserDownloadUrl!.contains(".jar") ||
element.browserDownloadUrl!.contains(".apk"))
.browserDownloadUrl;
print(dlurl);
return latestRelease;
}
Future latestCommitTime(String org, repoName) async {
var latestCommit =
await github.repositories.getRepository(RepositorySlug(org, repoName));
var commitTime = latestCommit.pushedAt?.difference(
DateTime.now().toLocal(),
);
final hours = commitTime!.inHours.abs();
if (hours > 24) {
var days = (commitTime.inDays).abs().toString();
print("$days days");
return "$days days";
} else if (hours > 1 && hours < 24) {
var hours = (commitTime.inHours).abs().toString();
print("$hours hours");
return "$hours hours";
} else {
var minutes = (commitTime.inMinutes).abs().toString();
print("$minutes minutes");
return "$minutes mins";
}
}
}
void main(List<String> args) {
GithubAPI().latestRelease('revanced', 'revanced-patches');
GithubAPI githubAPI = GithubAPI();
githubAPI.latestRelease('revanced', 'revanced-patches');
githubAPI.latestCommitTime("revanced", "revanced-patches");
}

View file

@ -1,11 +1,36 @@
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:revanced_manager_flutter/backend/api/github_api.dart';
import 'package:revanced_manager_flutter/constants.dart';
import 'package:revanced_manager_flutter/ui/widgets/patch_text_button.dart';
class LatestCommitWidget extends StatelessWidget {
class LatestCommitWidget extends StatefulWidget {
const LatestCommitWidget({Key? key}) : super(key: key);
@override
State<LatestCommitWidget> createState() => _LatestCommitWidgetState();
}
class _LatestCommitWidgetState extends State<LatestCommitWidget> {
GithubAPI githubAPI = GithubAPI();
String lastPatcherCommit = "Loading...";
String lastManagerCommit = "Loading...";
void latestCommit() async {
lastPatcherCommit =
await githubAPI.latestCommitTime("revanced", "revanced-patcher");
lastManagerCommit =
await githubAPI.latestCommitTime("revanced", "revanced-manager");
}
@override
void initState() {
setState(() {
latestCommit();
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Container(
@ -29,7 +54,7 @@ class LatestCommitWidget extends StatelessWidget {
),
),
Text(
"20 hours ago",
"$lastPatcherCommit ago",
style: robotoTextStyle,
)
],
@ -43,7 +68,7 @@ class LatestCommitWidget extends StatelessWidget {
),
),
Text(
"3 days ago",
"$lastManagerCommit ago",
style: robotoTextStyle,
)
],