Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jacopocarlini committed Dec 7, 2023
1 parent 491cbfe commit d8a08a4
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 55 deletions.
42 changes: 42 additions & 0 deletions lib/bloc/app_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:flutter_bloc/flutter_bloc.dart';

import 'app_state.dart';

class AppCubit extends Cubit<AppState> {
AppCubit({
Map<String, String>? repoVersion,
Map<String, String>? devVersion,
Map<String, String>? uatVersion,
Map<String, String>? prodVersion,
}) : super(AppState(
repoVersion: repoVersion ?? {},
devVersion: devVersion ?? {},
uatVersion: uatVersion ?? {},
prodVersion: prodVersion ?? {},
));

void addRepo(repoName, version) {
state.repoVersion.update(repoName, (value) => version, ifAbsent: () => version);
return emit(state.copyWith(repoVersion: state.repoVersion));
}


void addDev(repoName, version) {
state.devVersion.update(repoName, (value) => version, ifAbsent: () => version);
return emit(state.copyWith(devVersion: state.devVersion));
}


void addUat(repoName, version) {
state.uatVersion.update(repoName, (value) => version, ifAbsent: () => version);
return emit(state.copyWith(uatVersion: state.uatVersion));
}

void addProd(repoName, version) {
state.prodVersion.update(repoName, (value) => version, ifAbsent: () => version);
return emit(state.copyWith(prodVersion: state.prodVersion));
}



}
27 changes: 27 additions & 0 deletions lib/bloc/app_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class AppState {
Map<String, String> repoVersion;
Map<String, String> devVersion;
Map<String, String> uatVersion;
Map<String, String> prodVersion;

AppState({
required this.repoVersion,
required this.devVersion,
required this.uatVersion,
required this.prodVersion,
});

AppState copyWith({
Map<String, String>? repoVersion,
Map<String, String>? devVersion,
Map<String, String>? uatVersion,
Map<String, String>? prodVersion,
}) {
return AppState(
repoVersion: repoVersion ?? this.repoVersion,
devVersion: devVersion ?? this.devVersion,
uatVersion: uatVersion ?? this.uatVersion,
prodVersion: prodVersion ?? this.prodVersion,
);
}
}
22 changes: 10 additions & 12 deletions lib/components/be_cell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import 'dart:convert';

import 'package:cache_manager/cache_manager.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:http/http.dart' as http;
import 'package:statuspage/bloc/app_cubit.dart';
import 'package:statuspage/bloc/app_state.dart';
import 'package:statuspage/constant.dart';
import 'package:statuspage/utils.dart';

Expand Down Expand Up @@ -88,14 +91,12 @@ class BeCell extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
FutureBuilder<Widget>(
future: buildIcon(version),
builder: (context, snapshot) {
if (snapshot.hasData) {
return snapshot.data!;
}
return Container();

BlocSelector<AppCubit, AppState, Map<String,String>>(
selector: (state){
return state.repoVersion;
},
builder: (context, Map<String,String> repoVersion) {
return buildIcon(repoVersion[project['product']] ?? '', version);
}),
Flexible(
fit: FlexFit.loose,
Expand All @@ -113,10 +114,7 @@ class BeCell extends StatelessWidget {
];
}

Future<Widget> buildIcon(String version) async {
var key = "${project['product']}-release";

String repoVersion = await ReadCache.getString(key: key);
Widget buildIcon(String repoVersion, String version) {

if (compareTo(repoVersion, version) == -1) {
return const Tooltip(
Expand Down
98 changes: 63 additions & 35 deletions lib/components/name_cell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import 'dart:convert';
import 'dart:html';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:http/http.dart' as http;
import 'package:statuspage/bloc/app_cubit.dart';
import 'package:statuspage/utils.dart';

class NameCell extends StatelessWidget {
Expand All @@ -20,36 +22,43 @@ class NameCell extends StatelessWidget {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Tooltip(
buildName(),
buildRepoVersion(context),
buildExtraLabel(context),
],
);
}

FutureBuilder<String> buildRepoVersion(context) {
return FutureBuilder(
future: fetchRelease(context, project['repository']),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Tooltip(
message: snapshot.data!,
child: Text(
snapshot.data!,
overflow: TextOverflow.ellipsis,
),
);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
// By default, show a loading spinner.
return const SizedBox(
height: 20, width: 20, child: CircularProgressIndicator());
},
);
}

Tooltip buildName() {
return Tooltip(
message: '$name',
child: Text(
'$name',
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
overflow: TextOverflow.ellipsis,
),), FutureBuilder(
future: fetchRelease(project['repository']),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Tooltip(
message: '${snapshot.data!}',
child: Text(
'${snapshot.data!}',
overflow: TextOverflow.ellipsis,
),);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
// By default, show a loading spinner.
return const SizedBox(height: 20, width: 20, child: CircularProgressIndicator());
},
),
MediaQuery.of(context).size.width > 450 ?
Row(children: [
buildLabelEnv(project),
buildLabelPipe(project),
]):Container(),

],
),
);
}

Expand All @@ -59,14 +68,17 @@ class NameCell extends StatelessWidget {
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
color: project['actions'] == 'GHA' ? Colors.black54 : Colors.blue[100],
color:
project['actions'] == 'GHA' ? Colors.black54 : Colors.blue[100],
),
child: Padding(
padding: const EdgeInsets.all(6.0),
child: Text(
project['actions'] ?? 'TODO',
style: TextStyle(
color: project['actions'] == 'GHA' ? Colors.white : Colors.blue[500],
color: project['actions'] == 'GHA'
? Colors.white
: Colors.blue[500],
fontSize: 12,
fontWeight: FontWeight.bold),
),
Expand All @@ -79,30 +91,34 @@ class NameCell extends StatelessWidget {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
color: project['env'] == 'AKS' ? Colors.deepPurple[100] : Colors.blue[100],
color:
project['env'] == 'AKS' ? Colors.deepPurple[100] : Colors.blue[100],
),
child: Padding(
padding: const EdgeInsets.all(6.0),
child: Text(
project['env'] ?? 'TODO',
style: TextStyle(
color: project['env'] == 'AKS' ? Colors.deepPurple[500] : Colors.blue[500],
color: project['env'] == 'AKS'
? Colors.deepPurple[500]
: Colors.blue[500],
fontSize: 12,
fontWeight: FontWeight.bold),
),
),
);
}

Future<String> fetchRelease(repository) async {
Future<String> fetchRelease(BuildContext context, repository) async {
final Storage storage = window.localStorage;

var key = "${project['product']}-release";
return remember(key, () async {
var response;
var version = await remember(key, () async {
http.Response response;
if (storage['gh_token'] != null) {
response = await http.get(
Uri.parse('https://api.github.com/repos/pagopa/$repository/releases/latest'),
Uri.parse(
'https://api.github.com/repos/pagopa/$repository/releases/latest'),
headers: <String, String>{
'X-GitHub-Api-Version': '2022-11-28',
'Authorization': 'Bearer ${storage['gh_token']}',
Expand All @@ -113,8 +129,20 @@ class NameCell extends StatelessWidget {
response = await http.get(Uri.parse(
'https://api.github.com/repos/pagopa/$repository/releases/latest')); // there are limitation 60 requests per hour
}

return jsonDecode(response.body)['tag_name'] ?? 'No Release';
});
if (context.mounted) {
context.read<AppCubit>().addRepo(project['product'], version);
}
return version;
}

buildExtraLabel(context) {
return MediaQuery.of(context).size.width > 450
? Row(children: [
buildLabelEnv(project),
buildLabelPipe(project),
])
: Container();
}
}
13 changes: 11 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:statuspage/bloc/app_cubit.dart';
import 'package:statuspage/bloc/settings_cubit.dart';
import 'package:statuspage/pages/home_page.dart';
import 'package:statuspage/pages/init_page.dart';
Expand All @@ -14,8 +15,16 @@ class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => SettingsCubit(),
return MultiBlocProvider(
providers: [
BlocProvider(
create: (BuildContext context) => SettingsCubit(),
),
BlocProvider(
create: (BuildContext context) => AppCubit(),
),

],
child: BlocBuilder<SettingsCubit, bool>(
builder: (context, darkTheme) => buildMaterialApp(darkTheme),
),);
Expand Down
11 changes: 5 additions & 6 deletions lib/widgets/row_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class RowItem extends StatefulWidget {
}

class RowItemState extends State<RowItem> {

@override
void initState() {
super.initState();
Expand All @@ -37,9 +36,9 @@ class RowItemState extends State<RowItem> {
flex: 1,
fit: FlexFit.tight,
child: NameCell(name: widget.name, project: widget.project)),
buildCell("DEV", widget.project),
buildCell("UAT", widget.project),
buildCell("PROD", widget.project),
Flexible(flex: 1, child: buildCell("DEV", widget.project)),
Flexible(flex: 1, child: buildCell("UAT", widget.project)),
Flexible(flex: 1, child: buildCell("PROD", widget.project)),
MediaQuery.of(context).size.width > 1200
? Flexible(flex: 1, child: LinkCell(project: widget.project))
: Container()
Expand All @@ -50,9 +49,9 @@ class RowItemState extends State<RowItem> {

Widget buildCell(env, product) {
if (product["type"] == "frontend") {
return Flexible(flex: 1, child: FeCell(env: env, project: product));
return FeCell(env: env, project: product);
} else {
return Flexible(flex: 1, child: BeCell(env: env, project: product));
return BeCell(env: env, project: product);
}
}
}

0 comments on commit d8a08a4

Please sign in to comment.