Skip to content

Commit d8a779a

Browse files
committed
clipboard share improved, command multiplication
1 parent 36100f7 commit d8a779a

File tree

11 files changed

+285
-49
lines changed

11 files changed

+285
-49
lines changed

android/app/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@
3636

3737
<uses-permission android:name="android.permission.VIBRATE"/>
3838
<uses-permission android:name="android.permission.RECORD_AUDIO" />
39+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3940

4041
</manifest>

lib/Pages/input_dialog.dart

+83
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,89 @@ Future<int> inputMinuteHours(
378378
)??-1;
379379
}
380380

381+
Future<int> inputNumber(
382+
BuildContext context,
383+
String contentText,
384+
IconData icon,
385+
{String? title,int startValue=1,}) async {
386+
int num=1;
387+
int num2=0;
388+
return await showDialog<int>(
389+
context: context,
390+
builder: (context) {
391+
return AlertDialog(
392+
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18)),
393+
394+
title: title==null?null:Text(
395+
title,
396+
style: GoogleFonts.lato(fontSize: 20,),
397+
),
398+
content: Column(
399+
mainAxisSize: MainAxisSize.min,
400+
children: [
401+
Flexible(
402+
fit: FlexFit.loose,
403+
child: SingleChildScrollView(
404+
child: Text(
405+
contentText,
406+
style: GoogleFonts.lato(fontSize: 18,fontWeight: FontWeight.w300),
407+
),
408+
),
409+
),
410+
SizedBox(height: adjustSizeVertically(context, 20),),
411+
412+
Text("Value:",style: GoogleFonts.lato(fontSize: 24,fontWeight: FontWeight.w300),),
413+
const SizedBox(height: 10,),
414+
SpinBox(
415+
min: 1,
416+
max: 20,
417+
value: startValue.toDouble(),
418+
step: 1,
419+
acceleration: 3,
420+
showButtons: true,
421+
decoration: InputDecoration(border: OutlineInputBorder(
422+
borderRadius: BorderRadius.circular(18))),
423+
textStyle: GoogleFonts.lato(fontSize: 22,fontWeight: FontWeight.bold),
424+
onChanged: (value) => num=value.toInt(),
425+
),
426+
427+
],
428+
),
429+
actions: <Widget>[
430+
ElevatedButton(
431+
style: ElevatedButton.styleFrom(
432+
elevation: 2,
433+
primary: Colors.deepOrangeAccent,
434+
onPrimary: Colors.red[800],
435+
shape: RoundedRectangleBorder(
436+
borderRadius: BorderRadius.circular(8.0),
437+
),
438+
),
439+
child: Text('CANCEL',
440+
style: GoogleFonts.lato(fontSize: 16, color: Colors.white)),
441+
onPressed: () {
442+
return Navigator.pop(context,-1);
443+
},
444+
),
445+
ElevatedButton(
446+
style: ElevatedButton.styleFrom(
447+
elevation: 2,
448+
primary: Colors.lightGreen,
449+
onPrimary: Colors.green[800],
450+
shape: RoundedRectangleBorder(
451+
borderRadius: BorderRadius.circular(8.0),
452+
),
453+
),
454+
child: Text('CONFIRM',
455+
style: GoogleFonts.lato(fontSize: 16, color: Colors.white)),
456+
onPressed: () {
457+
return Navigator.pop(context,num+num2*60 );
458+
})
459+
]);
460+
},
461+
)??-1;
462+
}
463+
381464

382465

383466
Future<List<String>> inputTabAndEnter(

lib/Screens/Home.dart

+45-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import 'dart:async';
12
import 'dart:convert';
23
import 'dart:developer';
34
import 'dart:typed_data';
45

6+
import 'package:clipboard_monitor/clipboard_monitor.dart';
57
import 'package:flutter/cupertino.dart';
68
import 'package:flutter/material.dart';
79
import 'package:flutter/services.dart';
810
import 'package:google_fonts/google_fonts.dart';
11+
import 'package:intl/intl.dart';
912
import 'package:mr_power_manager_client/Screens/pc_manager.dart';
1013
import 'package:mr_power_manager_client/Screens/shimmer.dart';
1114
import 'package:mr_power_manager_client/Utils/StoreKeyValue.dart';
@@ -20,14 +23,23 @@ import '../Utils/SnackbarGenerator.dart';
2023
import '../Widgets/process_box.dart';
2124

2225
class Home extends StatefulWidget {
26+
27+
static void clipboardListener(context,str) async {
28+
print('SHARE_CLIPBOARD_listner');
29+
if (str != '') {
30+
Home.pcManagerState?.sendMessage("SHARE_CLIPBOARD@@@$str");
31+
SnackBarGenerator.makeSnackBar(context, 'Clipboard shared!');
32+
}
33+
}
34+
2335
Home({Key? key}) : super(key: key);
2436

2537
static PcManagerState? pcManagerState;
2638
late final MyStompClient myStompClient;
2739
List<Widget> pcListWidget = [];
2840
List<Widget> shimmerPcListWidget = [];
2941
static String token = '';
30-
static bool stopListenOnMessage=false;
42+
// static bool stopListenOnMessage=false;
3143
TextEditingController inputBoxText = TextEditingController();
3244
List<String> pcNames = [];
3345
Map<String, bool> pcStatus = {};
@@ -44,6 +56,16 @@ class HomeState extends State<Home> {
4456
bool tapped=false;
4557
String message='';
4658

59+
// Timer clipboardTriggerTime= Timer.periodic(
60+
// const Duration(seconds: 5),
61+
// (timer) {
62+
// Clipboard.getData('text/plain').then((clipboardContent) {
63+
// log('Clipboard content ${clipboardContent?.text}');
64+
//
65+
// });
66+
// },
67+
// );
68+
4769
@override
4870
Widget build(BuildContext context) {
4971
widget.shimmerPcListWidget = widget.isLoading
@@ -292,7 +314,7 @@ class HomeState extends State<Home> {
292314
void initState() {
293315
super.initState();
294316

295-
Future.delayed(const Duration(milliseconds: 50), () async {
317+
Future.delayed(const Duration(milliseconds: 0), () async {
296318
var token = await StoreKeyValue.readStringData('token');
297319
setState(() {
298320
Home.token = token;
@@ -320,9 +342,10 @@ class HomeState extends State<Home> {
320342
});
321343

322344
widget.myStompClient.subscribeMessage(stompFrame, (map) {
323-
if(Home.stopListenOnMessage){
324-
return;
325-
}
345+
// if(Home.stopListenOnMessage){
346+
// return;
347+
// }
348+
log(map['message'].split('@@@')[0]);
326349
if(map['message'].toString().contains('SHARE_CLIPBOARD')){
327350
Clipboard.setData(ClipboardData(text: map['message'].split('@@@')[1].toString()));
328351
return;
@@ -414,6 +437,20 @@ class HomeState extends State<Home> {
414437
Home.pcManagerState?.widget.windows=windowsProcessBox;
415438
});
416439
}
440+
441+
else if (map['message'].split('@@@')[0] == "CLIPBOARD_IMAGE") {
442+
log('SHARE_CLIPBOARD_IMAGE');
443+
Uint8List? image;
444+
try{
445+
image=base64Decode(message);
446+
}catch(e){
447+
print('err');
448+
return;
449+
}
450+
StoreKeyValue.writeFile(image, 'SharedImage ${
451+
DateFormat('yyyy-MM-dd HH-mm-ss').format(DateTime.now())}.jpg');
452+
SnackBarGenerator.makeSnackBar(context, 'Pc screenshot saved to gallery!',color: Colors.amber.shade600);
453+
}
417454
}
418455
});
419456

@@ -498,19 +535,20 @@ class HomeState extends State<Home> {
498535
}
499536

500537
poolingImOnline() async {
501-
Future.delayed(const Duration(seconds: 16), () {
538+
Future.delayed(const Duration(seconds: 5), () {
502539
sendOnline();
503540
/*
504541
requestData(context, HttpType.get, '/login',
505542
{'token': Home.token, 'imTheClient': 'true'});
506543
*/
507-
print('Im online sent!');
544+
// print('Im online sent!');
508545
poolingImOnline();
509546
});
510547
}
511548

512549

513550
sendOnline(){
551+
// log('mandato l-online al server!');
514552
widget.myStompClient.stompClient.send(
515553
destination: "/app/setOnline/${PcManager.token}", body: 'true');
516554
}

lib/Screens/pc_manager.dart

+22-16
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class PcManager extends StatefulWidget {
9494
redLight = true,
9595
saveBattery = true,
9696
hotspot = true,
97-
isLock = false;
97+
isClipboardShared = false;
9898
double opacityTop = 1, opacityBottom = 0;
9999
DateTime lastStatusEditedByClient = DateTime.fromMicrosecondsSinceEpoch(0);
100100
List<String> windowsTitle = [];
@@ -163,7 +163,7 @@ class PcManagerState extends State<PcManager>
163163
false) {
164164
PcManager.myStompClient?.unsubscribe[widget.pcName] = false;
165165
} else {
166-
var callback = (map) {
166+
void callback (map) {
167167
Home.pcManagerState?.setState(() {
168168
var widget = Home.pcManagerState?.widget ?? this.widget;
169169
if ((DateTime.now().difference(widget.lastStatusEditedByClient))
@@ -197,7 +197,7 @@ class PcManagerState extends State<PcManager>
197197
widget.saveBattery =
198198
map['saveBattery'].toString().toLowerCase() == 'true';
199199
widget.hotspot = map['hotspot'].toString().toLowerCase() == 'true';
200-
widget.isLock = map['locked'].toString().toLowerCase() == 'true';
200+
widget.isClipboardShared = map['clipboardShared'].toString().toLowerCase() == 'true';
201201

202202
var w1m = PcManager.wattageValues1m;
203203

@@ -232,7 +232,7 @@ class PcManagerState extends State<PcManager>
232232
w1m.todayWattHour =
233233
w1m.watts.map((e) => e).reduce((a, b) => a + b) / 3600;
234234
});
235-
};
235+
}
236236
PcManager.myStompClient?.subscribeStatusCallbacks[widget.pcName] =
237237
callback;
238238
PcManager.myStompClient?.subscribeStatus(widget.pcName, callback);
@@ -260,7 +260,7 @@ class PcManagerState extends State<PcManager>
260260
Container(),
261261
];
262262

263-
var FABsizes=[66,66,66,78,66,66,66,66,66,66,66,66,66];
263+
var FABsizes=[70,66,66,78,66,66,66,66,66,66,66,66,66];
264264

265265
var FABcolors = [
266266
[Colors.blue[900], Colors.blue, Colors.white],
@@ -1371,7 +1371,7 @@ class PcManagerState extends State<PcManager>
13711371
* se il pc è online mando il comando tramite socket, altrimenti lo mando alla api per memorizzarlo
13721372
*/
13731373
void sendCommand(DateTime? scheduledDate, String command,
1374-
{int value = -1, bool snackbar = true}) async {
1374+
{int value = -1, bool snackbar = false}) async {
13751375
var newToken = keepOnlyAlphaNum(PcManager.token);
13761376
var newPcName = keepOnlyAlphaNum(widget.pcName);
13771377
var formattedDate = '';
@@ -1421,7 +1421,7 @@ class PcManagerState extends State<PcManager>
14211421
}
14221422

14231423
sendBase64(String data, String header) {
1424-
Home.stopListenOnMessage = true;
1424+
// Home.stopListenOnMessage = true;
14251425
setState(() {});
14261426
var BYTES_LIMIT = 14000;
14271427
var max = data.length ~/ BYTES_LIMIT;
@@ -1437,13 +1437,19 @@ class PcManagerState extends State<PcManager>
14371437
data = data.substring(BYTES_LIMIT);
14381438
}
14391439
PcManager.myStompClient?.stompClient.send(
1440-
destination: "/app/sendMessage/${PcManager.token}/${widget.pcName}",
1440+
destination: "/app/sendMessage/to/server/${PcManager.token}/${widget.pcName}",
14411441
body: msg);
14421442

1443-
Home.stopListenOnMessage = false;
1443+
// Home.stopListenOnMessage = false;
14441444
}
14451445
}
14461446

1447+
sendMessage(String msg) {
1448+
PcManager.myStompClient?.stompClient.send(
1449+
destination: "/app/sendMessage/to/server/${PcManager.token}/${widget.pcName}",
1450+
body: msg);
1451+
}
1452+
14471453
controlScreen() {
14481454
return Column(
14491455
children: [
@@ -1551,16 +1557,16 @@ class PcManagerState extends State<PcManager>
15511557
CommandShape(
15521558
widget.pcName,
15531559
Colors.pinkAccent[100] ?? Colors.pinkAccent,
1554-
'Keyboard',
1555-
Icons.keyboard,
1560+
'Send my clipboard',
1561+
Icons.paste,
15561562
true,
1557-
Commands.KEYBOARD),
1563+
Commands.SEND_CLIPBOARD),
15581564
CommandShape(
15591565
widget.pcName,
15601566
Colors.pinkAccent[300] ?? Colors.pinkAccent,
1561-
'Share clip.',
1562-
Icons.content_paste,
1563-
true,
1567+
'Toggle get pc screenshots',
1568+
Icons.copy,
1569+
widget.isClipboardShared,
15641570
Commands.SHARE_CLIPBOARD),
15651571
],
15661572
),
@@ -2021,7 +2027,7 @@ class PcManagerState extends State<PcManager>
20212027
}
20222028

20232029
void pollingActiveWindows() async {
2024-
Future.delayed(const Duration(milliseconds: 2000), () {
2030+
Future.delayed(const Duration(milliseconds: 3000), () {
20252031
if (_currentIndex==1) {
20262032
sendCommand(null, 'TASK_MANAGER', snackbar: false);
20272033
}

lib/Styles/commands.dart

+1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ enum Commands {
3131
WATTAGE,
3232
PASSWORD,
3333
SHARE_CLIPBOARD,
34+
SEND_CLIPBOARD,
3435
KEYBOARD,
3536
}

lib/Utils/StoreKeyValue.dart

+28
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import 'dart:io';
2+
import 'dart:typed_data';
3+
4+
import 'package:gallery_saver/gallery_saver.dart';
5+
import 'package:path_provider/path_provider.dart';
6+
import 'package:permission_handler/permission_handler.dart';
17
import 'package:shared_preferences/shared_preferences.dart';
28

39
class StoreKeyValue {
@@ -85,4 +91,26 @@ class StoreKeyValue {
8591
static Future<void> removeData(String key) async {
8692
(await _prefs).remove(key);
8793
}
94+
95+
96+
97+
static Future<void> writeFile(Uint8List data, String name) async {
98+
// storage permission ask
99+
var status = await Permission.storage.status;
100+
if (!status.isGranted) {
101+
await Permission.storage.request();
102+
}
103+
104+
Directory? tempDir = await getExternalStorageDirectory();
105+
String tempPath = tempDir?.path??'';
106+
var filePath = tempPath + '/$name';
107+
print(filePath);
108+
109+
// the data
110+
var bytes = ByteData.view(data.buffer);
111+
final buffer = bytes.buffer;
112+
// save the data in the path
113+
await (await File(filePath).create(recursive: true)).writeAsBytes(buffer.asUint8List(data.offsetInBytes, data.lengthInBytes),mode: FileMode.write);
114+
GallerySaver.saveImage(filePath);
115+
}
88116
}

lib/Utils/api_request.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class MyStompClient {
7373
subscribeMessage(StompFrame frame, Function(Map) onCallback) {
7474
print('subscribeMessage...');
7575
stompClient.subscribe(
76-
destination: '/both/${keepOnlyAlphaNum(token)}/message',
76+
destination: '/client/${keepOnlyAlphaNum(token)}/message',
7777
callback: (StompFrame frame) {
7878
if (frame.body != null) {
7979
var jsonData = json.decode(frame.body ?? '');

0 commit comments

Comments
 (0)