import 'dart:convert'; import 'dart:io'; import 'package:cpd_app/language_utils.dart'; import 'package:flutter/services.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:http/http.dart' as http; import 'package:http/http.dart'; import 'package:http_parser/http_parser.dart'; import 'package:flutter/material.dart'; import 'package:connectivity/connectivity.dart'; import 'dart:async'; class HttpUtils { var client = http.Client(); Future checkLang(Uint8List imageBytes, String imageName) async { var postUri = Uri.parse("http://130.61.27.201/upload"); http.MultipartRequest request = http.MultipartRequest("POST", postUri); http.MultipartFile multipartFile = http.MultipartFile.fromBytes( 'file', imageBytes, filename: 'lorem.png', contentType: MediaType('image', 'png'), ); request.files.add(multipartFile); http.StreamedResponse response = await client.send(request); http.Response finalResponse = await http.Response.fromStream(response); var lang = ""; if (finalResponse.statusCode == 200) { Map jsonData = jsonDecode(finalResponse.body); lang = jsonData['language']; } else { throw ('Error: ${finalResponse.statusCode}'); } return lang; } Future extractTextKnownSource( Uint8List imageBytes, String sourceLang) async { var postUri = Uri.parse("http://130.61.27.201/extractWithKnownSource"); http.MultipartRequest request = http.MultipartRequest("POST", postUri); http.MultipartFile multipartFile = http.MultipartFile.fromBytes( 'file', imageBytes, filename: 'lorem.png', contentType: MediaType('image', 'png'), ); MultipartFile source = MultipartFile.fromString( 'sourceLang', sourceLang, ); request.files.add(multipartFile); request.files.add(source); http.StreamedResponse response = await client.send(request); http.Response finalResponse = await http.Response.fromStream(response); var lang = ""; if (finalResponse.statusCode == 200) { lang = finalResponse.body.toString(); } else { throw ('Error: ${finalResponse.statusCode}'); } return lang; } Future extractTextUnknownSource(Uint8List imageBytes) async { var postUri = Uri.parse("http://130.61.27.201/extractWithoutKnownSource"); http.MultipartRequest request = http.MultipartRequest("POST", postUri); http.MultipartFile multipartFile = http.MultipartFile.fromBytes( 'file', imageBytes, filename: 'lorem.png', contentType: MediaType('image', 'png'), ); request.files.add(multipartFile); http.StreamedResponse response = await client.send(request); http.Response finalResponse = await http.Response.fromStream(response); var lang = ""; if (finalResponse.statusCode == 200) { lang = finalResponse.body.toString(); } else { throw ('Error: ${finalResponse.statusCode}'); } return lang; } Future translateKnownSource( Uint8List imageBytes, String sourceLang, String targetLang) async { var url = Uri.parse("http://130.61.27.201/translKnownSource"); http.MultipartRequest request = http.MultipartRequest("POST", url); http.MultipartFile multipartFile = http.MultipartFile.fromBytes( 'file', imageBytes, filename: 'lorem.png', contentType: MediaType('image', 'png'), ); MultipartFile source = MultipartFile.fromString( 'sourceLang', LanguageUtils.translatorLanguages[sourceLang]!, ); MultipartFile target = MultipartFile.fromString( 'targetLang', LanguageUtils.translatorLanguages[targetLang]!, ); request.files.add(multipartFile); request.files.add(source); request.files.add(target); http.StreamedResponse response = await client.send(request); http.Response finalResponse = await http.Response.fromStream(response); Uint8List lang = Uint8List(0); if (finalResponse.statusCode == 200) { lang = Uint8List.fromList(finalResponse.bodyBytes); } else { throw ('Error: ${finalResponse.statusCode}'); } return lang; } void showProgressBar(BuildContext context) { showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return const Dialog( backgroundColor: Color.fromARGB(0, 44, 44, 44), elevation: 0, child: Center( child: CircularProgressIndicator(), ), ); }, ); } void hideProgressBar(BuildContext context) { Navigator.pop(context); } Future internetConnectivityCheck() async { if (Platform.isAndroid) { var connectivityResult = await Connectivity().checkConnectivity(); return connectivityResult == ConnectivityResult.wifi || connectivityResult == ConnectivityResult.mobile; } else { final response = await http.get(Uri.parse('http://130.61.27.201')); return response.statusCode == 200; } } void triggerNoInetToast() { Fluttertoast.showToast( msg: "No internet connection", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 1, backgroundColor: const Color.fromARGB(255, 161, 120, 17), textColor: Colors.white, fontSize: 16.0, ); } void triggerConnectedToast() { Fluttertoast.showToast( msg: "Connected", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 1, backgroundColor: const Color.fromARGB(255, 161, 120, 17), textColor: Colors.white, fontSize: 16.0, ); } }