Skip to content

Commit

Permalink
[CORE] Add network settings
Browse files Browse the repository at this point in the history
  • Loading branch information
monolidth committed Mar 12, 2021
1 parent c47bd7c commit b1ee76f
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 22 deletions.
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<application
android:name="io.flutter.app.FlutterApplication"
android:icon="@mipmap/ic_launcher"
android:usesCleartextTraffic="true"
android:label="Receipt Manager">
<activity
android:name=".MainActivity"
Expand Down
17 changes: 17 additions & 0 deletions ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
<key>NSExceptionDomains</key>
<dict>
<key>example.com</key> <!--Include your domain at this line -->
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
<false/>
</dict>
</plist>
Expand Down
1 change: 1 addition & 0 deletions lib/generated/intl/messages_de_DE.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class MessageLookup extends MessageLookupByLibrary {
"handshakeException" : MessageLookupByLibrary.simpleMessage("Zertifikat ungültig"),
"healthCategory" : MessageLookupByLibrary.simpleMessage("Gesundheit"),
"highContrast" : MessageLookupByLibrary.simpleMessage("Hoher Kontrast"),
"https" : MessageLookupByLibrary.simpleMessage("Aktiviere HTTPS"),
"income" : MessageLookupByLibrary.simpleMessage("Einnahme"),
"insertServerApiToken" : MessageLookupByLibrary.simpleMessage("Insert server api token"),
"insertYourApiToken" : MessageLookupByLibrary.simpleMessage("Insert your api token"),
Expand Down
1 change: 1 addition & 0 deletions lib/generated/intl/messages_en_US.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class MessageLookup extends MessageLookupByLibrary {
"handshakeException" : MessageLookupByLibrary.simpleMessage("Bad certificate. Please regenerate the certificate"),
"healthCategory" : MessageLookupByLibrary.simpleMessage("Health"),
"highContrast" : MessageLookupByLibrary.simpleMessage("High contrast"),
"https" : MessageLookupByLibrary.simpleMessage("Aktiviere HTTPS"),
"income" : MessageLookupByLibrary.simpleMessage("Income"),
"insertServerApiToken" : MessageLookupByLibrary.simpleMessage("Insert server API token"),
"insertYourApiToken" : MessageLookupByLibrary.simpleMessage("Insert your api token"),
Expand Down
10 changes: 10 additions & 0 deletions lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/l10n/intl_de_DE.arb
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,6 @@
"detectReceiptServer": "Erkenne Receipt Server",
"noReceiptServer": "Kein Receipt Server wurde gefunden",
"noServicesFound": "No services found",
"type" : "Typ"
"type" : "Typ",
"https" : "Aktiviere HTTPS"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_en_GB.arb
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,6 @@
"detectReceiptServer": "Detect Receipt Server",
"noReceiptServer": "No receipt server found.",
"noServicesFound": "No services found",
"type" : "Type"
"type" : "Type",
"https" : "Enable HTTPS"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_en_US.arb
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,6 @@
"detectReceiptServer": "Detect Receipt Server",
"noReceiptServer": "No receipt server found.",
"noServicesFound": "No services found",
"type" : "Type"
"type" : "Type",
"https" : "Aktiviere HTTPS"
}
38 changes: 20 additions & 18 deletions lib/network/network_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import '../main.dart';
/// After, a new receipt object is crafted and send to the [ReceiptForm].
/// The receipt form uses this object to fill the corresponding text fields.
class NetworkClient {
final _protocol = "https://";
final _https = "https://";
final _http = "http://";
final _uploadPath = "/api/upload";
final _trainingPath = "/api/training";
final _token = "?access_token=";
Expand All @@ -59,19 +60,20 @@ class NetworkClient {

NetworkClient._internal();

init() {
/// override the agent to provide support for self signed
/// certificates.
///
/// Since the server runs local and the certificate is protected
/// with a password, the security risk is small.
HttpOverrides.global = new SelfSignedHttpAgent();
}

String getAPIUrl(final ip, final token, final legacyParser, final gaussian,
final grayscale, final rotate) {
if (token == null || token == "")
return _protocol +
final grayscale, final rotate, final https) {

if (https)
/// override the agent to provide support for self signed
/// certificates.
///
/// Since the server runs local and the certificate is protected
/// with a password, the security risk is small.
HttpOverrides.global = new SelfSignedHttpAgent();

String protocol = https ? _https : _http;
if (token == null || token == "") {
return protocol +
ip +
":" +
_port +
Expand All @@ -84,7 +86,9 @@ class NetworkClient {
getValue(gaussian) +
"&rotate=" +
getValue(rotate);
return _protocol +
}

return protocol +
ip +
":" +
_port +
Expand Down Expand Up @@ -119,7 +123,6 @@ class NetworkClient {

sendTrainingData(String ip, String token, String company, String date,
String total, BuildContext context) async {
init();

log("Submit training data.");
Map<String, String> headers = {"Content-type": "application/json"};
Expand All @@ -142,7 +145,6 @@ class NetworkClient {
/// Send image via post request to the server and capture the response.
sendImage(File imageFile, NetworkClientHolder holder, BuildContext context,
[GlobalKey<ScaffoldState> key]) async {
init();

log("Try to upload new image.");
if (holder.ip == null || holder.ip.isEmpty) {
Expand All @@ -163,7 +165,7 @@ class NetworkClient {

var length = await imageFile.length();
var uri = Uri.parse(getAPIUrl(holder.ip, holder.token, holder.legacyParser,
holder.gaussian, holder.grayscale, holder.rotate));
holder.gaussian, holder.grayscale, holder.rotate, holder.https));

log(uri.toString());

Expand Down Expand Up @@ -325,7 +327,7 @@ class NetworkClient {
}

getTrainingUrl(String ip, String token) {
return _protocol + ip + ":" + _port + _trainingPath + _token + token;
return _https + ip + ":" + _port + _trainingPath + _token + token;
}
}

Expand Down
5 changes: 5 additions & 0 deletions lib/network/network_client_holder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ class NetworkClientHolder {
/// Show receipt article list
bool showItemList;

/// send via https
bool https;

static final NetworkClientHolder _networkClientHolder =
NetworkClientHolder._internal();

NetworkClientHolder._internal();
factory NetworkClientHolder() {
return _networkClientHolder;
}

void readOptions(SharedPreferences sharedPrefs) {
ip = sharedPrefs.get("ipv4");
token = sharedPrefs.get("api_token");
Expand All @@ -68,5 +72,6 @@ class NetworkClientHolder {
legacyParser = sharedPrefs.get("legacyParser");
rotate = sharedPrefs.get("rotate");
showItemList = sharedPrefs.get("showItemList");
https = sharedPrefs.get("https");
}
}
16 changes: 16 additions & 0 deletions lib/ui/settings/settings_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class _SettingsWidgetState extends State<SettingsWidget> {
bool _rotate = false;
bool _sendTrainingData = false;
bool _showItemList = false;
bool _https = true;

final SharedPreferences _prefs;

Expand Down Expand Up @@ -71,6 +72,10 @@ class _SettingsWidgetState extends State<SettingsWidget> {
_showItemList = _prefs.getBool("showItemList") == null
? _showItemList
: _prefs.getBool("showItemList");

_https = _prefs.getBool("https") == null
? _https
: _prefs.getBool("https");
}

@override
Expand Down Expand Up @@ -228,6 +233,17 @@ class _SettingsWidgetState extends State<SettingsWidget> {
});
},
),
SettingsTile.switchTile(
title: S.of(context).https,
leading: Icon(Icons.lock),
switchValue: _https,
onToggle: (bool value) {
setState(() {
_https = value;
_prefs.setBool("https", value);
});
},
),
],
),
SettingsSection(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: '1.1.1' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.1.1+125
version: 1.1.1+130

environment:
sdk: ">=2.7.0 <3.0.0"
Expand Down

0 comments on commit b1ee76f

Please sign in to comment.