Skip to content

Commit

Permalink
remove hive completely
Browse files Browse the repository at this point in the history
  • Loading branch information
souramoo committed Jul 15, 2024
1 parent 4c66c97 commit d10dbbf
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 84 deletions.
119 changes: 54 additions & 65 deletions lib/data/web_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:flutter/foundation.dart';
//import 'package:flutter_redux/flutter_redux.dart';

// Package imports:
import 'package:hive_flutter/hive_flutter.dart';
import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:invoiceninja_flutter/utils/platforms.dart';
Expand All @@ -32,17 +31,6 @@ class WebClient {
return connectionStatus.hasConnection;
}

Future<void> _cacheData(String key, dynamic data) async {
final box = await Hive.openBox('cachedResponses');
await box.put(key, data);
}

Future<dynamic> _getCachedData(String key) async {
final box = await Hive.openBox('cachedResponses');
final data = box.get(key);
return data;
}

Future<dynamic> get(
String url,
String? token, {
Expand All @@ -67,6 +55,8 @@ class WebClient {
print('GET: $url');

final isOnline = await _isOnline();
print("isOnline: $isOnline");

if (isOnline) {
final client = http.Client();
final http.Response response = await client.get(
Expand All @@ -83,17 +73,9 @@ class WebClient {

final dynamic jsonResponse = json.decode(response.body);

// Cache the response
await _cacheData(url, jsonResponse);

return jsonResponse;
} else {
final cachedData = await _getCachedData(url);
if (cachedData != null) {
print('Returning cached data');
return cachedData;
}
throw 'No internet connection available';
return Future.error("No internet connection available.");
}
}

Expand Down Expand Up @@ -121,6 +103,7 @@ class WebClient {

final isOnline = await _isOnline();
print("isOnline: $isOnline");

if (isOnline) {
if (multipartFiles != null) {
response = await _uploadFiles(url, token, multipartFiles, data: data);
Expand Down Expand Up @@ -157,17 +140,9 @@ class WebClient {

final dynamic jsonResponse = json.decode(response.body);

// Cache the response
await _cacheData(url + data, jsonResponse);

return jsonResponse;
} else {
final cachedData = await _getCachedData(url + data);
if (cachedData != null) {
print('Returning cached data');
return cachedData;
}
throw 'No internet connection available';
return Future.error("No internet connection available.");
}
}

Expand All @@ -191,27 +166,34 @@ class WebClient {
}
http.Response response;

if (multipartFile != null) {
response = await _uploadFiles(url, token, [multipartFile],
data: data, method: 'PUT');
} else {
final client = http.Client();
response = await client.put(
Uri.parse(url),
body: data,
headers: _getHeaders(
url,
token,
password: password,
idToken: idToken,
),
);
client.close();
}
final isOnline = await _isOnline();
print("isOnline: $isOnline");

if (isOnline) {
if (multipartFile != null) {
response = await _uploadFiles(url, token, [multipartFile],
data: data, method: 'PUT');
} else {
final client = http.Client();
response = await client.put(
Uri.parse(url),
body: data,
headers: _getHeaders(
url,
token,
password: password,
idToken: idToken,
),
);
client.close();
}

_checkResponse(url, response);
_checkResponse(url, response);

return json.decode(response.body);
return json.decode(response.body);
} else {
return Future.error("No internet connection available.");
}
}

Future<dynamic> delete(
Expand All @@ -229,22 +211,29 @@ class WebClient {

print('Delete: $url');

final client = http.Client();
final http.Response response = await client.delete(
Uri.parse(url),
headers: _getHeaders(
url,
token,
password: password,
idToken: idToken,
),
body: data,
);
client.close();

_checkResponse(url, response);

return json.decode(response.body);
final isOnline = await _isOnline();
print("isOnline: $isOnline");

if (isOnline) {
final client = http.Client();
final http.Response response = await client.delete(
Uri.parse(url),
headers: _getHeaders(
url,
token,
password: password,
idToken: idToken,
),
body: data,
);
client.close();

_checkResponse(url, response);

return json.decode(response.body);
} else {
return Future.error("No internet connection available.");
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import 'package:redux_logging/redux_logging.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:invoiceninja_flutter/utils/platforms.dart';
import 'package:hive_flutter/hive_flutter.dart';
// import 'package:bitsdojo_window/bitsdojo_window.dart';

// Project imports:
Expand Down Expand Up @@ -101,7 +100,6 @@ emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
''';

void main({bool isTesting = false}) async {
await Hive.initFlutter();
final ConnectionStatusSingleton connectionStatus =
ConnectionStatusSingleton.getInstance();
connectionStatus.initialize();
Expand Down
16 changes: 0 additions & 16 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -609,22 +609,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.1"
hive:
dependency: transitive
description:
name: hive
sha256: "8dcf6db979d7933da8217edcec84e9df1bdb4e4edc7fc77dbd5aa74356d6d941"
url: "https://pub.dev"
source: hosted
version: "2.2.3"
hive_flutter:
dependency: "direct main"
description:
name: hive_flutter
sha256: dca1da446b1d808a51689fb5d0c6c9510c0a2ba01e22805d492c73b68e33eecc
url: "https://pub.dev"
source: hosted
version: "1.1.0"
html:
dependency: transitive
description:
Expand Down
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ dependencies:
device_info_plus: ^9.1.0
ios_open_subscriptions_settings: ^0.0.4
connectivity_plus: ^6.0.3
hive_flutter: ^1.1.0

dependency_overrides:
intl: any
Expand Down

0 comments on commit d10dbbf

Please sign in to comment.