Skip to content

Commit

Permalink
bloc
Browse files Browse the repository at this point in the history
  • Loading branch information
jacopocarlini committed Mar 2, 2024
1 parent 4004ed0 commit 478b74c
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 438 deletions.
101 changes: 88 additions & 13 deletions lib/bloc/versions/app_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import 'dart:convert';

import 'package:chaleno/chaleno.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:http/http.dart' as http;
import 'package:statuspage/bloc/versions/app_state.dart';
import 'package:statuspage/constant.dart';

class AppCubit extends Cubit<AppState> {
AppCubit({
Expand All @@ -14,32 +19,102 @@ class AppCubit extends Cubit<AppState> {
prodVersion: prodVersion ?? {},
));

void addRepo(repoName, version) {
state.repoVersion
.update(repoName, (value) => version, ifAbsent: () => version);
void addAll() {
addTeamProject(projectsCore);
addTeamProject(projectsNodo5);
addTeamProject(projectsVAS);
addTeamProject(projectsTouchPoint);
}

void addTeamProject(teamProject) {
for (var namespace in teamProject.values) {
for (var project in namespace) {
addRepo(project);
addDev(project);
addUat(project);
addProd(project);
}
}
}

Future<void> addRepo(project) async {
var parser = await Chaleno()
.load('https://github.com/pagopa/${project['repository']}');
List<Result>? results = parser?.getElementsByClassName(
'css-truncate css-truncate-target text-bold mr-2');
var version = results?.first.text ?? 'ERROR';

state.repoVersion.update(project['product'], (value) => version,
ifAbsent: () => version);
return emit(state.copyWith(repoVersion: state.repoVersion));
}

void addDev(repoName, version) {
state.devVersion
.update(repoName, (value) => version, ifAbsent: () => version);
Future<void> addDev(project) async {
var url = apim_d + basePath + project['product'];
String version = await fetchVersion(project, url);

state.devVersion.update(project['product'], (value) => version,
ifAbsent: () => version);
return emit(state.copyWith(devVersion: state.devVersion));
}

void addUat(repoName, version) {
state.uatVersion
.update(repoName, (value) => version, ifAbsent: () => version);
Future<void> addUat(project) async {
var url = apim_u + basePath + project['product'];
String version = await fetchVersion(project, url);

state.uatVersion.update(project['product'], (value) => version,
ifAbsent: () => version);
return emit(state.copyWith(uatVersion: state.uatVersion));
}

void addProd(repoName, version) {
state.prodVersion
.update(repoName, (value) => version, ifAbsent: () => version);
Future<void> addProd(project) async {
var url = apim_p + basePath + project['product'];
String version = await fetchVersion(project, url);

state.prodVersion.update(project['product'], (value) => version,
ifAbsent: () => version);
return emit(state.copyWith(prodVersion: state.prodVersion));
}

void empty() {
return emit(state.copyWith(
emit(state.copyWith(
repoVersion: {}, devVersion: {}, uatVersion: {}, prodVersion: {}));
addAll();
}

Future<String> fetchVersion(project, url) {
if (project["type"] == "frontend") {
return fetchVersionFE(url);
} else {
return fetchVersionBE(url);
}
}

Future<String> fetchVersionBE(url) async {
var version = '';
var response = await http.get(Uri.parse(url));
if (response.statusCode == 200 && response.bodyBytes.isNotEmpty) {
version = jsonDecode(response.body)['version'] ?? 'No Info Version';
} else {
if (response.bodyBytes.isEmpty) {
version = 'Empty Body';
} else {
version = 'ERROR';
}
}
return version;
}

Future<String> fetchVersionFE(url) async {
var response = await http.get(Uri.parse(url));
if (response.statusCode == 200 && response.bodyBytes.isNotEmpty) {
return jsonDecode(response.body)['version'] ?? 'No Info Version';
} else {
if (response.bodyBytes.isEmpty) {
return 'Empty Body';
} else {
return 'ERROR';
}
}
}
}
151 changes: 28 additions & 123 deletions lib/components/info_cell.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import 'dart:convert';

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/versions/app_cubit.dart';
import 'package:statuspage/bloc/versions/app_state.dart';
import 'package:statuspage/constant.dart';
import 'package:statuspage/utils.dart';

class InfoCell extends StatelessWidget {
Expand All @@ -21,55 +17,32 @@ class InfoCell extends StatelessWidget {

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: getVersion(context, env),
builder: (BuildContext context, AsyncSnapshot snapshot) {
late List<Widget> children;
if (snapshot.hasData) {
if (snapshot.data.contains('.')) {
children = buildOk(snapshot.data);
saveInState(context, snapshot.data);
} else if (snapshot.data.contains('Empty Body')) {
children = buildWarning(snapshot.data);
saveInState(context, snapshot.data);
} else {
children = buildError(snapshot.data);
saveInState(context, "ERROR");
}
} else if (snapshot.hasError) {
children = buildError(snapshot.error);
saveInState(context, "ERROR");
} else {
children = buildLoading();
saveInState(context, "LOADING");
}
return Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
...children,
// buildReleaseButton(env)
],
),
),
);
});
}

void saveInState(BuildContext context, String value) {
if (context.mounted) {
if (env == "DEV") {
context.read<AppCubit>().addDev(project['product'], value);
}
if (env == "UAT") {
context.read<AppCubit>().addUat(project['product'], value);
}
if (env == "PROD") {
context.read<AppCubit>().addProd(project['product'], value);
return BlocBuilder<AppCubit, AppState>(
builder: (BuildContext context, state) {
late List<Widget> children;
String? version = state.devVersion[project['product']];
if (version != null) {
if (version != 'ERROR') {
children = buildOk(version, state);
} else {
children = buildError(version);
}
} else {
children = buildLoading();
}
}
return Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
...children,
// buildReleaseButton(env)
],
),
),
);
});
}

List<Widget> buildLoading() {
Expand Down Expand Up @@ -118,7 +91,7 @@ class InfoCell extends StatelessWidget {
];
}

List<Widget> buildOk(String version) {
List<Widget> buildOk(String version, state) {
return <Widget>[
const Icon(
Icons.check_circle_outline,
Expand All @@ -130,12 +103,7 @@ class InfoCell extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
BlocSelector<AppCubit, AppState, Map<String, String>>(
selector: (state) {
return state.repoVersion;
}, builder: (context, Map<String, String> repoVersion) {
return buildIcon(repoVersion[project['product']] ?? '', version);
}),
buildIcon(state.repoVersion[project['product']] ?? '', version),
Flexible(
fit: FlexFit.loose,
child: Tooltip(
Expand All @@ -153,6 +121,7 @@ class InfoCell extends StatelessWidget {
}

Widget buildIcon(String repoVersion, String version) {
repoVersion = repoVersion.replaceFirst('Release ', '');
if (compareTo(repoVersion, version) == -1) {
return const Tooltip(
message: 'versione maggiore rispetto al repository',
Expand Down Expand Up @@ -185,68 +154,4 @@ class InfoCell extends StatelessWidget {
}
return Container();
}

Future<String> getVersion(context, String env) {
String url = project['product'];
if (project["type"] == "frontend") {
return getInfo(env, url);
} else {
return getInfoFE(env, url);
}
}

Future<String> getInfo(String env, String product) async {
late String url;
if (env == 'DEV') {
url = apim_d + basePath + product;
}
if (env == 'UAT') {
url = apim_u + basePath + product;
}
if (env == 'PROD') {
url = apim_p + basePath + product;
}

var key = "${project['product']}-info-$env";

// return remember(key, () async {
var response = await http.get(Uri.parse(url));

if (response.statusCode == 200 && response.bodyBytes.isNotEmpty) {
return jsonDecode(response.body)['version'] ?? 'No Info Version';
} else {
if (response.bodyBytes.isEmpty) {
return 'Empty Body';
} else {
return response.statusCode.toString();
}
}
// });
}

Future<String> getInfoFE(String env, String product) async {
late String url;
if (env == 'DEV') {
url = apim_d + basePath + product;
}
if (env == 'UAT') {
url = apim_u + basePath + product;
}
if (env == 'PROD') {
url = apim_p + basePath + product;
}
var key = "${project['product']}-info-$env";
// return remember(key, () async {
var response = await http.get(Uri.parse(url));
if (response.statusCode == 200 && response.bodyBytes.isNotEmpty) {
return jsonDecode(response.body)['version'] ?? 'No Info Version';
} else {
if (response.bodyBytes.isEmpty) {
return 'Empty Body';
} else {
return response.statusCode.toString();
}
}
// });
}
}
Loading

0 comments on commit 478b74c

Please sign in to comment.