Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial github release version checker #4792

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions frontend/appflowy_flutter/lib/startup/deps_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import 'package:appflowy/workspace/application/settings/appearance/mobile_appear
import 'package:appflowy/workspace/application/settings/prelude.dart';
import 'package:appflowy/workspace/application/sidebar/rename_view/rename_view_bloc.dart';
import 'package:appflowy/workspace/application/tabs/tabs_bloc.dart';
import 'package:appflowy/workspace/application/update_checker/update_checker_bloc.dart';
import 'package:appflowy/workspace/application/user/prelude.dart';
import 'package:appflowy/workspace/application/view/prelude.dart';
import 'package:appflowy/workspace/application/workspace/prelude.dart';
Expand Down Expand Up @@ -191,6 +192,8 @@ void _resolveHomeDeps(GetIt getIt) {
getIt.registerSingleton<ReminderBloc>(ReminderBloc());

getIt.registerSingleton<RenameViewBloc>(RenameViewBloc(PopoverController()));

getIt.registerSingleton<VersionCheckerBloc>(VersionCheckerBloc());
}

void _resolveFolderDeps(GetIt getIt) {
Expand Down
5 changes: 3 additions & 2 deletions frontend/appflowy_flutter/lib/startup/startup.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:appflowy/env/cloud_env.dart';
import 'package:appflowy/workspace/application/settings/prelude.dart';
import 'package:appflowy_backend/appflowy_backend.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:package_info_plus/package_info_plus.dart';

Expand Down
5 changes: 5 additions & 0 deletions frontend/appflowy_flutter/lib/startup/tasks/app_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:appflowy/workspace/application/notifications/notification_servic
import 'package:appflowy/workspace/application/settings/appearance/appearance_cubit.dart';
import 'package:appflowy/workspace/application/settings/notifications/notification_settings_cubit.dart';
import 'package:appflowy/workspace/application/sidebar/rename_view/rename_view_bloc.dart';
import 'package:appflowy/workspace/application/update_checker/update_checker_bloc.dart';
import 'package:appflowy/workspace/application/view/view_ext.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
Expand Down Expand Up @@ -135,6 +136,10 @@ class _ApplicationWidgetState extends State<ApplicationWidget> {
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<VersionCheckerBloc>.value(
value: getIt<VersionCheckerBloc>()
..add(const VersionCheckerEvent.checkLatestRelease()),
),
BlocProvider<AppearanceSettingsCubit>(
create: (_) => AppearanceSettingsCubit(
widget.appearanceSetting,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// ignore_for_file: invalid_annotation_target

import 'dart:convert';

import 'package:appflowy_backend/log.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:http/http.dart' as http;

part 'github_service.freezed.dart';
part 'github_service.g.dart';

abstract class IGitHubService {
/// Checks for the latest release on GitHub.
///
/// Returns [GitHubReleaseInfo] if the request is successful, otherwise returns null.
Future<GitHubReleaseInfo?> checkLatestGitHubRelease();
}

@freezed
class GitHubReleaseInfo with _$GitHubReleaseInfo {
const GitHubReleaseInfo._();

const factory GitHubReleaseInfo({
@JsonKey(name: 'tag_name') required String tagName,
required String name,
@JsonKey(name: 'html_url') required String htmlUrl,
@JsonKey(name: 'created_at') required String createdAt,
@JsonKey(name: 'published_at') required String publishedAt,
}) = _GitHubReleaseInfo;

factory GitHubReleaseInfo.fromJson(Map<String, dynamic> json) =>
_$GitHubReleaseInfoFromJson(json);
}

class GitHubService implements IGitHubService {
GitHubService();

static const String _gitHubVersionHeader = 'X-GitHub-Api-Version';
static const String _gitHubVersion = '2022-11-28';

static const String _baseUrl = 'https://api.github.com';
static const String _repo = '/repos/AppFlowy-IO/AppFlowy';

static const String _latestReleaseEndpoint = '/releases/latest';

final http.Client _client = http.Client();
Xazin marked this conversation as resolved.
Show resolved Hide resolved
Uri _uri = Uri.parse(_baseUrl);

@override
Future<GitHubReleaseInfo?> checkLatestGitHubRelease() async {
_uri = _uri.replace(path: '$_repo$_latestReleaseEndpoint');
try {
final response = await _client.get(
_uri,
headers: {
_gitHubVersionHeader: _gitHubVersion,
},
);

if (response.statusCode == 200) {
final Map<String, dynamic> body =
jsonDecode(response.body) as Map<String, dynamic>;
return GitHubReleaseInfo.fromJson(body);
} else if (response.statusCode == 403) {
Xazin marked this conversation as resolved.
Show resolved Hide resolved
// For unauthenticated usage, the rate limit is 60 requests per hour.
//
// https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28
Log.warn(
'checkLatestGitHubRelease failed: GitHub API rate limit exceeded',
);
}
} on http.ClientException catch (e, s) {
Log.error(
'checkLatestGitHubRelease failed: ${e.message}\n$s',
);
} on TypeError catch (e) {
Log.error(
'checkLatestGitHubRelease failed mapping GitHubReleaseInfo\n${e.stackTrace}',
);
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'package:flutter/foundation.dart';

import 'package:appflowy/user/application/github/github_service.dart';
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:package_info_plus/package_info_plus.dart';

part 'update_checker_bloc.freezed.dart';

class VersionCheckerBloc
extends Bloc<VersionCheckerEvent, VersionCheckerState> {
VersionCheckerBloc() : super(const VersionCheckerState.initial()) {
_gitHubService = GitHubService();

on<VersionCheckerEvent>((event, emit) async {
await event.when(
checkLatestRelease: () async {
final releaseInfo = await _gitHubService.checkLatestGitHubRelease();
final currentInfo = await PackageInfo.fromPlatform();

if (releaseInfo == null) {
// Fail silently
return emit(const VersionCheckerState.upToDate());
} else {
final currentVersion = currentInfo.version;
if (currentVersion != releaseInfo.tagName) {
emit(
VersionCheckerState.updateAvailable(
version: releaseInfo.tagName,
),
);
} else {
emit(const VersionCheckerState.upToDate());
}
}
},
);
});
}

late final IGitHubService _gitHubService;
}

@freezed
class VersionCheckerEvent with _$VersionCheckerEvent {
const factory VersionCheckerEvent.checkLatestRelease() = _CheckLatestRelease;
}

@freezed
class VersionCheckerState with _$VersionCheckerState {
const factory VersionCheckerState.initial() = _Initial;
const factory VersionCheckerState.fetchingVersions() = _FetchingVersions;
const factory VersionCheckerState.upToDate() = _UpToDate;
const factory VersionCheckerState.updateAvailable({required String version}) =
_UpdateAvailable;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/startup/startup.dart';
import 'package:appflowy/startup/tasks/rust_sdk.dart';
import 'package:appflowy/workspace/application/update_checker/update_checker_bloc.dart';
import 'package:appflowy/workspace/presentation/home/toast.dart';
import 'package:appflowy/workspace/presentation/widgets/pop_up_action.dart';
import 'package:appflowy_popover/appflowy_popover.dart';
Expand All @@ -10,8 +15,7 @@ import 'package:flowy_infra/size.dart';
import 'package:flowy_infra_ui/style_widget/button.dart';
import 'package:flowy_infra_ui/style_widget/text.dart';
import 'package:flowy_infra_ui/widget/spacing.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:styled_widget/styled_widget.dart';
import 'package:url_launcher/url_launcher.dart';
Expand Down Expand Up @@ -55,16 +59,21 @@ class _BubbleActionListState extends State<BubbleActionList> {

@override
Widget build(BuildContext context) {
final List<PopoverAction> actions = [];
actions.addAll(
BubbleAction.values.map((action) => BubbleActionWrapper(action)),
);
actions.add(FlowyVersionDescription());
final List<PopoverAction> actions = [
...BubbleAction.values.map((action) => BubbleActionWrapper(action)),
FlowyVersionDescription(),
FlowyUpdateStatus(),
];

return PopoverActionList<PopoverAction>(
direction: PopoverDirection.topWithRightAligned,
actions: actions,
offset: const Offset(0, -8),
constraints: const BoxConstraints(
minWidth: 170,
maxWidth: 225,
maxHeight: 300,
),
buildChild: (controller) {
return FlowyTextButton(
'?',
Expand Down Expand Up @@ -199,12 +208,48 @@ class FlowyVersionDescription extends CustomActionCell {
}
}

class FlowyUpdateStatus extends CustomActionCell {
@override
Widget buildWithContext(BuildContext context) {
return BlocProvider.value(
value: getIt<VersionCheckerBloc>(),
child: BlocBuilder<VersionCheckerBloc, VersionCheckerState>(
Xazin marked this conversation as resolved.
Show resolved Hide resolved
builder: (context, state) {
return SizedBox(
height: 23,
child: Row(
children: [
FlowyText(
state.maybeWhen(
orElse: () =>
LocaleKeys.questionBubble_updateStatus_checking.tr(),
upToDate: () =>
LocaleKeys.questionBubble_updateStatus_upToDate.tr(),
updateAvailable: (v) => LocaleKeys
.questionBubble_updateStatus_updateAvailable
.tr(args: [v]),
),
maxLines: 2,
color: Theme.of(context).hintColor,
),
],
).padding(
horizontal: ActionListSizes.itemHPadding,
),
);
},
),
);
}
}

enum BubbleAction { whatsNews, help, debug, shortcuts, markdown, github }

class BubbleActionWrapper extends ActionCell {
BubbleActionWrapper(this.inner);

final BubbleAction inner;

@override
Widget? leftIcon(Color iconColor) => inner.emoji;

Expand Down
5 changes: 5 additions & 0 deletions frontend/resources/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@
"success": "Copied debug info to clipboard!",
"fail": "Unable to copy debug info to clipboard"
},
"updateStatus": {
"upToDate": "AppFlowy is up to date 🎉",
"updateAvailable": "Version {} is available",
"checking": "Checking version..."
},
"feedback": "Feedback"
},
"menuAppHeader": {
Expand Down
Loading