Skip to content

Commit

Permalink
Merge pull request #22 from MurilonND/develop
Browse files Browse the repository at this point in the history
fix: offset image & server function is a separed function
  • Loading branch information
MurilonND authored Aug 3, 2023
2 parents f86c6ba + 2cb5a49 commit ffa855f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 49 deletions.
Binary file added assets/canvas3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 0 additions & 32 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:liquid_art_ai/server.dart';
import 'package:network_info_plus/network_info_plus.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:liquid_art_ai/src/features/connection/infrastructure/galaxy_cubit.dart';
import 'package:liquid_art_ai/src/features/home/presentation/page/home_page.dart';
import 'package:liquid_art_ai/src/utils/user_configurations.dart';

Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await UserConfigurations.init();

startImageServer();

runApp(const MyApp());
}

Expand Down Expand Up @@ -69,27 +61,3 @@ class _SplashScreenState extends State<SplashScreen> {
);
}
}

void startImageServer() async {
final imageServer = ImageServer();
final networkInfo = NetworkInfo();
String? wifiIPv4 = '';

try {
wifiIPv4 = await networkInfo.getWifiIP();
} on PlatformException catch (e) {
print('Failed to get Wifi IPv4 error: $e');
}

final address = wifiIPv4 ?? ''; // Bind to any IPv4 address on the machine
const port = 3000; // Port number to listen on



shelf_io
.serve((shelf.Request request) => imageServer.handleRequest(request),
address, port)
.then((server) {
print('Image server running on ${server.address.host}:${server.port}');
});
}
57 changes: 43 additions & 14 deletions lib/server.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
import 'dart:io';
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:network_info_plus/network_info_plus.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as shelf_io;

import 'src/features/connection/infrastructure/galaxy_cubit.dart';

class ImageServer {
final GalaxyCubit _galaxyCubit = GalaxyCubit();
HttpServer? server; // Declare a global variable to hold the server instance

void startImageServer(String imagePath, int lgScreens) async {
final imageServer = ImageServer();
final networkInfo = NetworkInfo();
String? wifiIPv4 = '';

try {
wifiIPv4 = await networkInfo.getWifiIP();
} on PlatformException catch (e) {
print('Failed to get Wifi IPv4 error: $e');
}

final address = wifiIPv4 ?? ''; // Bind to any IPv4 address on the machine
const port = 3000; // Port number to listen on

Future<shelf.Response> handleRequest(shelf.Request request) async {
print('========================================');
print(_galaxyCubit.state.lgScreens);
print('========================================');
server = await shelf_io.serve((shelf.Request request) => imageServer.handleRequest(request, imagePath, lgScreens),
address, port);

final imageBytes = await _getImageBytes('assets/logo/cat.png');
print('Image server running on ${server?.address.host}:${server?.port}');
}

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

class ImageServer {
Future<shelf.Response> handleRequest(shelf.Request request, String imagePath, int lgScreens) async {
final imageBytes = await _getImageBytes(imagePath);
if (imageBytes != null) {
final base64Image = base64Encode(imageBytes);
final htmlResponse = '''
Expand All @@ -22,15 +51,15 @@ class ImageServer {
<head>
<style>
body { margin: 0; padding: 0; height: 100vh; display: flex; justify-content: center; align-items: center; }
.image-container { width: 100vw; height: 100%; background-image: url('data:image/png;base64, $base64Image'); background-size: 400% 100%; }
.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: 33.33% 0; }
.image-container.image2 { background-position: 66.66% 0; }
.image-container.image1 { background-position: 50% 0; }
.image-container.image2 { background-position: 100% 0; }
.image-container.5 { background-position: 0 0; }
.image-container.3 { background-position: 20% 0; }
.image-container.1 { background-position: 40% 0; }
.image-container.2 { background-position: 60% 0; }
.image-container.4 { background-position: 80% 0; }
.image-container.3 { background-position: 25% 0; }
.image-container.1 { background-position: 50% 0; }
.image-container.2 { background-position: 75% 0; }
.image-container.4 { background-position: 100% 0; }
</style>
</head>
<body>
Expand Down
5 changes: 2 additions & 3 deletions lib/src/features/connection/infrastructure/galaxy_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:dartssh2/dartssh2.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:liquid_art_ai/server.dart';
import 'package:network_info_plus/network_info_plus.dart';

part 'galaxy_state.dart';
Expand Down Expand Up @@ -111,9 +112,7 @@ class GalaxyCubit extends Cubit<GalaxyState> {
wifiIPv4 = '';
}

print('============================================================');
print(state.lgScreens);
print('============================================================');
rerunImageServer('assets/canvas3.jpg', state.lgScreens);

final closeCanvasCommand = 'bash <(curl -S https://raw.githubusercontent.com/MurilonND/LiquidArt_AI/main/scripts/close.sh) ' +
state.password;
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ flutter:
- assets/logo/Logo.png
- assets/logo/splash.png
- assets/logo/cat.png
- assets/canvas3.jpg


# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware
Expand Down

0 comments on commit ffa855f

Please sign in to comment.