Skip to content

Commit

Permalink
fix: Gallery loading and Saving Loading
Browse files Browse the repository at this point in the history
  • Loading branch information
MurilonND committed Aug 7, 2023
1 parent 633ddf4 commit 855af41
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 101 deletions.
31 changes: 14 additions & 17 deletions lib/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'src/features/connection/infrastructure/galaxy_cubit.dart';

HttpServer? server; // Declare a global variable to hold the server instance

void startImageServer(String imagePath, int lgScreens) async {
void startImageServer(String? imagePath, int lgScreens, Uint8List? imageFileBytes) async {
final imageServer = ImageServer();
final networkInfo = NetworkInfo();
String? wifiIPv4 = '';
Expand All @@ -24,26 +24,31 @@ void startImageServer(String imagePath, int lgScreens) async {
final address = wifiIPv4 ?? ''; // Bind to any IPv4 address on the machine
const port = 3000; // Port number to listen on

server = await shelf_io.serve((shelf.Request request) => imageServer.handleRequest(request, imagePath, lgScreens),
server = await shelf_io.serve((shelf.Request request) => imageServer.handleRequest(request, imagePath, lgScreens, imageFileBytes),
address, port);

print('Image server running on ${server?.address.host}:${server?.port}');
}

void rerunImageServer(String imagePath, int lgScreens) async {
void rerunImageServer(String? imagePath, int lgScreens, Uint8List? imageFileBytes) async {
if (server != null) {
await server!.close();
server = null; // Reset the server instance
startImageServer(imagePath, lgScreens);
startImageServer(imagePath, lgScreens, imageFileBytes);
} else {
startImageServer(imagePath, lgScreens);
startImageServer(imagePath, lgScreens, imageFileBytes);
}
}

class ImageServer {
Future<shelf.Response> handleRequest(shelf.Request request, String imagePath, int lgScreens) async {
Future<shelf.Response> handleRequest(shelf.Request request, String? imagePath, int lgScreens, Uint8List? imageFileBytes) async {
final logoBytes = await _getImageBytes('assets/logo/splash.png');
final imageBytes = await _getImageBytes(imagePath);
Uint8List? imageBytes;
if(imagePath != null){
imageBytes = await _getImageBytes(imagePath);
}else{
imageBytes = imageFileBytes!;
}
if (imageBytes != null && logoBytes != null) {
final base64Logo = base64Encode(logoBytes);
final base64Image = base64Encode(imageBytes);
Expand All @@ -53,8 +58,7 @@ class ImageServer {
<head>
<style>
body { margin: 0; padding: 0; height: 100vh; display: flex; justify-content: start; align-items: start; }
.logo-container3 { width: 50%; height: 50%; background-image: url('data:image/png;base64, $base64Logo'); background-size: 100% 100%; position: absolute; z-index: 1; }
.logo-container5 { width: 100%; height: 100%; background-image: url('data:image/png;base64, $base64Logo'); background-size: 100% 100%; position: absolute; z-index: 1; }
.logo-container { width: 50vh; height: 50vh; background-image: url('data:image/png;base64, $base64Logo'); background-size: 100% 100%; position: absolute; z-index: 1; }
.image-container { width: 100%; height: 100%; background-image: url('data:image/png;base64, $base64Image'); background-size: 300% 100%; }
.image-container.image3 { background-position: 0 0; }
.image-container.image1 { background-position: 50% 0; }
Expand Down Expand Up @@ -82,15 +86,8 @@ class ImageServer {

String _getLogoClass(String path){
if(path == '3' || path == '5'){
switch (path) {
case '3':
return 'logo-container3';
case '5':
return 'logo-container5';
default:
return '';
return 'logo-container';
}
}
return '';
}

Expand Down
25 changes: 13 additions & 12 deletions lib/src/features/connection/infrastructure/galaxy_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:dartssh2/dartssh2.dart';
import 'package:equatable/equatable.dart';
Expand Down Expand Up @@ -97,17 +98,17 @@ class GalaxyCubit extends Cubit<GalaxyState> {
}
}

// Future<void> shutdown() async {
// if (state.client == null || state.client!.isClosed) return;
//
// final shutdownCommand =
// 'bash <(curl -s https://raw.githubusercontent.com/LiquidGalaxyLAB/BIM-Liquid-Galaxy-Visualizer/main/bim_visualizer_node/libs/shutdown.sh) ${state.password}';
// final session = await state.client!.execute(shutdownCommand);
// await session.stdin.close();
// await session.done;
// }

Future<void> openCanvas() async {
Future<void> shutdown() async {
if (state.client == null || state.client!.isClosed) return;

final closeCanvasCommand = 'bash <(curl -S https://raw.githubusercontent.com/MurilonND/LiquidArt_AI/main/scripts/close.sh) ' +
state.password;
final close = await state.client!.execute(closeCanvasCommand);
await close.stdin.close();
await close.done;
}

Future<void> openCanvas(String? imagePath, Uint8List? imageFileBytes) async {
final networkInfo = NetworkInfo();
String? wifiIPv4 = '';

Expand All @@ -117,7 +118,7 @@ class GalaxyCubit extends Cubit<GalaxyState> {
wifiIPv4 = '';
}

rerunImageServer('assets/canvas3.jpg', state.lgScreens);
rerunImageServer(imagePath, state.lgScreens, imageFileBytes);

final closeCanvasCommand = 'bash <(curl -S https://raw.githubusercontent.com/MurilonND/LiquidArt_AI/main/scripts/close.sh) ' +
state.password;
Expand Down
23 changes: 15 additions & 8 deletions lib/src/features/connection/presentation/page/connection_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class ConnectionPage extends StatefulWidget {
}

class _ConnectionPageState extends State<ConnectionPage> {
final GalaxyCubit _galaxyCubit = GalaxyCubit();
late GalaxyCubit _galaxyCubit;

// @override
// void initState() {
// _galaxyCubit = context.read();
// super.initState();
// }
@override
void initState() {
_galaxyCubit = context.read<GalaxyCubit>();
super.initState();
}

//Text Strings
final String connectionPageTitle = "Connection";
Expand Down Expand Up @@ -158,10 +158,17 @@ class _ConnectionPageState extends State<ConnectionPage> {
// : null,
// ),
LiquidArtButton(
label: 'Open Canvas',
label: 'Open Demo Canvas',
onTap: state.client != null &&
!state.client!.isClosed
? () => _galaxyCubit.openCanvas()
? () => _galaxyCubit.openCanvas('assets/canvas3.jpg', null)
: null,
),
LiquidArtButton(
label: 'Close Canvas',
onTap: state.client != null &&
!state.client!.isClosed
? () => _galaxyCubit.shutdown()
: null,
),
],
Expand Down
34 changes: 23 additions & 11 deletions lib/src/features/drawer/presentation/pages/drawer_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class _DrawerPageState extends State<DrawerPage> {

String image = "";
bool isLoaded = false;
bool isSaving = false;
bool placeHolder = true;

var textController = TextEditingController();
Expand All @@ -58,15 +59,18 @@ class _DrawerPageState extends State<DrawerPage> {
if(img != null){
final directory = (await getApplicationDocumentsDirectory()).path;
final filename = "share.png";
final imgpath = await File("${directory}/$filename").create();
await imgpath.writeAsBytes(img);
final imgPath = await File("${directory}/$filename").create();
await imgPath.writeAsBytes(img);

Share.shareFiles([imgpath.path], text: "Generated by AI - LiquidArtAI");
Share.shareFiles([imgPath.path], text: "Generated by AI - LiquidArtAI");
}
});
}

downloadImg() async {
setState(() {
isSaving = true;
});
var res = await Permission.storage.request();
if(res.isGranted){
const folder = "LiquidArtAI";
Expand All @@ -75,14 +79,16 @@ class _DrawerPageState extends State<DrawerPage> {

final fileName = "${_imagePromptController?.text}.jpg";

print(imgPath);

if(await path.exists()){
await screenshotController.captureAndSave(imgPath.path,delay: const Duration(milliseconds: 100), fileName: fileName, pixelRatio: 1.0);
}else{
await imgPath.create();
await screenshotController.captureAndSave(imgPath.path,delay: const Duration(milliseconds: 100), fileName: fileName, pixelRatio: 1.0);
}

setState(() {
isSaving = false;
});
}
}

Expand Down Expand Up @@ -325,12 +331,18 @@ class _DrawerPageState extends State<DrawerPage> {
]
],
const SizedBox(height: 20),
LiquidArtButton(
label: 'Save Image',
onTap: image != "" ? () async {
await downloadImg();
} : null,
),
if(isSaving)...[
const Center(
child: CircularProgressIndicator(),
),
]else...[
LiquidArtButton(
label: 'Save Image',
onTap: image != "" ? () async {
await downloadImg();
} : null,
),
],
const SizedBox(
height: 15,
),
Expand Down
Loading

0 comments on commit 855af41

Please sign in to comment.