Skip to content

Commit 01bc0db

Browse files
authored
Merge pull request #18 from iss2022-BCR/sprint2
Sprint1 v1.3
2 parents 5f88768 + 4bb2db4 commit 01bc0db

29 files changed

+847
-83
lines changed
Binary file not shown.

Sprint1/Sprint1_Project/run_WasteService.bat

Lines changed: 0 additions & 10 deletions
This file was deleted.

Sprint1/Sprint1_Project/sprint1_smart_device/android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ android {
4444

4545
defaultConfig {
4646
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
47-
applicationId "com.example.sprint1_smart_device"
47+
applicationId "it.unibo.sprint1_smart_device"
4848
// You can update the following values to match your application needs.
4949
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
5050
minSdkVersion flutter.minSdkVersion

Sprint1/Sprint1_Project/sprint1_smart_device/android/app/src/debug/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.example.sprint1_smart_device">
2+
package="it.unibo.sprint1_smart_device">
33
<!-- The INTERNET permission is required for development. Specifically,
44
the Flutter tool needs it to communicate with the running application
55
to allow setting breakpoints, to provide hot reload, etc.

Sprint1/Sprint1_Project/sprint1_smart_device/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.example.sprint1_smart_device">
2+
package="it.unibo.sprint1_smart_device">
33
<application
44
android:label="SmartDeviceSimulator Sprint1"
55
android:name="${applicationName}"

Sprint1/Sprint1_Project/sprint1_smart_device/android/app/src/main/kotlin/com/example/sprint1_smart_device/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.sprint1_smart_device
1+
package it.unibo.sprint1_smart_device
22

33
import io.flutter.embedding.android.FlutterActivity
44

Loading

Sprint1/Sprint1_Project/sprint1_smart_device/lib/main.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
22

33
import 'view/view_home.dart';
44

5+
import '../model/constants.dart' as Constants;
6+
57
void main() {
68
runApp(const MyApp());
79
}
@@ -12,7 +14,7 @@ class MyApp extends StatelessWidget {
1214
@override
1315
Widget build(BuildContext context) {
1416
return MaterialApp(
15-
title: 'SmartDevice Simulator',
17+
title: Constants.appTitle,
1618
theme: ThemeData(
1719
primarySwatch: Colors.blue,
1820
),

Sprint1/Sprint1_Project/sprint1_smart_device/lib/model/constants.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
const String appTitle = "SmartDevice Simulator";
2+
const String VERSION_NUMBER = "1.3";
13
const String titleHome = "Smart Device Simulator";
24
const String titleRequest = "Store Request";
5+
const String titleSettings = "Settings";
6+
const String titleAbout = "About";
37

48
const String defaultIP = "127.0.0.1";
59
const int defaultPort = 11800;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import 'package:shared_preferences/shared_preferences.dart';
2+
import 'package:sprint1_smart_device/model/appl_message.dart';
3+
4+
class Settings {
5+
static const bool DEFAULT_SEND_TYPES_REQUEST = true;
6+
static const String DEFAULT_MESSAGE_ID = "storerequest";
7+
static const ApplMessageType DEFAULT_MESSAGE_TYPE = ApplMessageType.request;
8+
static const String DEFAULT_MESSAGE_SENDER = "smartdevice";
9+
static const String DEFAULT_MESSAGE_RECEIVER = "wasteservice";
10+
11+
bool sendTypesRequest = DEFAULT_SEND_TYPES_REQUEST;
12+
String messageID = DEFAULT_MESSAGE_ID;
13+
ApplMessageType messageType = DEFAULT_MESSAGE_TYPE;
14+
String messageSender = DEFAULT_MESSAGE_SENDER;
15+
String messageReceiver = DEFAULT_MESSAGE_RECEIVER;
16+
17+
void loadSettings() async {
18+
final prefs = await SharedPreferences.getInstance();
19+
20+
sendTypesRequest =
21+
prefs.getBool("sendTypesRequest") ?? DEFAULT_SEND_TYPES_REQUEST;
22+
messageID = prefs.getString("messageID") ?? DEFAULT_MESSAGE_ID;
23+
String mType = prefs.getString("messageType") ?? DEFAULT_MESSAGE_TYPE.name;
24+
try {
25+
messageType = ApplMessageType.values.byName(mType);
26+
} catch (e) {
27+
messageType = DEFAULT_MESSAGE_TYPE;
28+
}
29+
messageSender = prefs.getString("messageSender") ?? DEFAULT_MESSAGE_SENDER;
30+
messageReceiver =
31+
prefs.getString("messageReceiver") ?? DEFAULT_MESSAGE_RECEIVER;
32+
}
33+
34+
void saveSettings() async {
35+
final prefs = await SharedPreferences.getInstance();
36+
37+
prefs.setBool("sendTypesRequest", sendTypesRequest);
38+
prefs.setString("messageID", messageID);
39+
prefs.setString("messageType", messageType.name);
40+
prefs.setString("messageSender", messageSender);
41+
prefs.setString("messageReceiver", messageReceiver);
42+
}
43+
44+
void resetSettings() async {
45+
final prefs = await SharedPreferences.getInstance();
46+
47+
prefs.setBool("sendTypesRequest", DEFAULT_SEND_TYPES_REQUEST);
48+
prefs.setString("messageID", DEFAULT_MESSAGE_ID);
49+
prefs.setString("messageType", DEFAULT_MESSAGE_TYPE.name);
50+
prefs.setString("messageSender", DEFAULT_MESSAGE_SENDER);
51+
prefs.setString("messageReceiver", DEFAULT_MESSAGE_RECEIVER);
52+
}
53+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'package:flutter/material.dart';
2+
3+
import '../model/constants.dart' as Constants;
4+
5+
class ViewAbout extends StatelessWidget {
6+
@override
7+
Widget build(BuildContext context) {
8+
return WillPopScope(
9+
onWillPop: () async {
10+
return true;
11+
},
12+
child: Scaffold(
13+
appBar: AppBar(
14+
title: const Text(Constants.titleAbout),
15+
centerTitle: true,
16+
automaticallyImplyLeading: true,
17+
leading: IconButton(
18+
icon: const Icon(Icons.arrow_back_ios),
19+
onPressed: () {
20+
if (Navigator.canPop(context)) {
21+
Navigator.pop(context);
22+
}
23+
},
24+
),
25+
),
26+
body: Center(
27+
child: ListView(
28+
shrinkWrap: true,
29+
padding: const EdgeInsets.all(20.0),
30+
children: const [
31+
ListTile(title: Text('App developed by BCR Team')),
32+
],
33+
),
34+
),
35+
),
36+
);
37+
}
38+
}

Sprint1/Sprint1_Project/sprint1_smart_device/lib/view/view_home.dart

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import 'package:flutter/material.dart';
44
import 'package:flutter/services.dart';
55
import 'package:sprint1_smart_device/model/networking/tcp_client_connection.dart';
66

7+
import '../model/appl_message.dart';
78
import '../model/networking/client_connection.dart';
9+
import '../model/settings.dart';
10+
import '../widgets/side_menu_widget.dart';
811
import 'view_request.dart';
912

1013
import '../model/constants.dart' as Constants;
@@ -17,6 +20,8 @@ class ViewHome extends StatefulWidget {
1720
}
1821

1922
class _ViewHomeState extends State<ViewHome> {
23+
final Settings _settings = Settings();
24+
2025
final _formKeyConnection = GlobalKey<FormState>();
2126

2227
final TextEditingController _textControllerAddress = TextEditingController();
@@ -54,17 +59,22 @@ class _ViewHomeState extends State<ViewHome> {
5459
_tcpClientConnection = TcpClientConnection();
5560
_tcpClientConnection
5661
.connect(ip, port, timeout: const Duration(seconds: 5))
57-
.then((value) {
58-
setState(() {
59-
_isLoading = false;
60-
});
61-
Navigator.push(
62+
.then(
63+
(value) {
64+
setState(() {
65+
_isLoading = false;
66+
});
67+
Navigator.push(
6268
context,
6369
MaterialPageRoute(
64-
builder: (context) => ViewRequest(
65-
connection: _tcpClientConnection,
66-
notifyParent: _showAlertDialog)));
67-
}).onError((error, stackTrace) {
70+
builder: (context) => ViewRequest(
71+
settings: _settings,
72+
connection: _tcpClientConnection,
73+
notifyParent: _showAlertDialog),
74+
),
75+
);
76+
},
77+
).onError((error, stackTrace) {
6878
setState(() {
6979
_isLoading = false;
7080
});
@@ -81,9 +91,29 @@ class _ViewHomeState extends State<ViewHome> {
8191
});
8292
}
8393

94+
void _saveSettings(bool sendTypesReq, String msgID, ApplMessageType msgType,
95+
String msgSender, String msgReceiver) {
96+
setState(() {
97+
_settings.sendTypesRequest = sendTypesReq;
98+
_settings.messageID = msgID;
99+
_settings.messageType = msgType;
100+
_settings.messageSender = msgSender;
101+
_settings.messageReceiver = msgReceiver;
102+
_settings.saveSettings();
103+
});
104+
}
105+
106+
@override
107+
void initState() {
108+
super.initState();
109+
110+
_settings.loadSettings();
111+
}
112+
84113
@override
85114
Widget build(BuildContext context) {
86115
return Scaffold(
116+
drawer: NavDrawer(settings: _settings, saveSettings: _saveSettings),
87117
appBar: AppBar(
88118
title: const Text(Constants.titleHome),
89119
centerTitle: true,

Sprint1/Sprint1_Project/sprint1_smart_device/lib/view/view_request.dart

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ import 'package:intl/intl.dart';
99
import 'package:sprint1_smart_device/model/appl_message.dart';
1010
import 'package:sprint1_smart_device/model/networking/client_connection.dart';
1111
import 'package:sprint1_smart_device/model/waste_service/types_request.dart';
12+
import '../model/settings.dart';
1213
import '../model/waste_service/store_request.dart';
1314

1415
import '../model/constants.dart' as Constants;
1516

1617
class ViewRequest extends StatefulWidget {
17-
ViewRequest({Key? key, required this.connection, required this.notifyParent})
18+
ViewRequest(
19+
{Key? key,
20+
required this.settings,
21+
required this.connection,
22+
required this.notifyParent})
1823
: super(key: key);
1924

25+
final Settings settings;
2026
ClientConnection connection;
2127
final Function(String, String) notifyParent;
2228

@@ -103,6 +109,30 @@ class _ViewRequestState extends State<ViewRequest> {
103109
}
104110
}
105111

112+
Future<void> _sendMessage() async {
113+
setState(() {
114+
_waitingReply = true;
115+
_reply = "";
116+
});
117+
118+
ApplMessage applMsg = ApplMessage(
119+
widget.settings.messageID,
120+
widget.settings.messageType,
121+
widget.settings.messageSender,
122+
widget.settings.messageReceiver,
123+
"${widget.settings.messageID}($_currentWasteType, ${_textControllerWeight.text})",
124+
3);
125+
String msg = applMsg.toString();
126+
127+
_logMessage("Message: $msg");
128+
129+
widget.connection.sendMessage(msg);
130+
131+
/*if (timeout != null) {
132+
_startTimer();
133+
}*/
134+
}
135+
106136
List<String> _parseTypes(String types, String sep) {
107137
return types.split(sep);
108138
}
@@ -191,7 +221,11 @@ class _ViewRequestState extends State<ViewRequest> {
191221
if (_timer == null) {
192222
return;
193223
}
194-
_timer!.isActive ? _timer!.cancel() : null;
224+
if (_timer!.isActive) {
225+
_timer!.cancel();
226+
} else {
227+
return;
228+
}
195229
setState(() {
196230
_waitingReply = false;
197231
});
@@ -212,6 +246,13 @@ class _ViewRequestState extends State<ViewRequest> {
212246
super.didChangeDependencies();
213247
}
214248

249+
@override
250+
void dispose() {
251+
super.dispose();
252+
253+
_stopTimer();
254+
}
255+
215256
@override
216257
void initState() {
217258
super.initState();
@@ -225,7 +266,6 @@ class _ViewRequestState extends State<ViewRequest> {
225266
Widget build(BuildContext context) {
226267
return WillPopScope(
227268
onWillPop: () async {
228-
_stopTimer();
229269
widget.notifyParent(
230270
"Disconnected", "You disconnecred from the server.");
231271
widget.connection.close();
@@ -240,7 +280,6 @@ class _ViewRequestState extends State<ViewRequest> {
240280
leading: IconButton(
241281
icon: const Icon(Icons.arrow_back_ios),
242282
onPressed: () {
243-
//_stopTimer();
244283
widget.notifyParent(
245284
"Disconnected", "You disconnecred from the server.");
246285
widget.connection.close();
@@ -393,9 +432,12 @@ class _ViewRequestState extends State<ViewRequest> {
393432
borderRadius: BorderRadius.circular(8.0),
394433
color: _reply.toLowerCase().contains("accepted")
395434
? Colors.green
396-
: _reply
397-
.toLowerCase()
398-
.contains(r'rejected|error')
435+
: (_reply
436+
.toLowerCase()
437+
.contains("rejected") ||
438+
_reply
439+
.toLowerCase()
440+
.contains("rejected"))
399441
? Colors.red
400442
: Colors.grey,
401443
),
@@ -427,7 +469,7 @@ class _ViewRequestState extends State<ViewRequest> {
427469
}
428470
});
429471

430-
_sendStoreRequest();
472+
_debug ? _sendMessage() : _sendStoreRequest();
431473
}
432474
},
433475
style: ElevatedButton.styleFrom(

0 commit comments

Comments
 (0)