Can now download dynamically the background

This commit is contained in:
ALEZ-DEV 2024-05-18 18:34:24 +02:00
parent 2c02ef39c6
commit 63b2aff20a
10 changed files with 220 additions and 33 deletions

View File

@ -0,0 +1,55 @@
import 'dart:convert';
import 'dart:io';
import 'package:babylonia_terminal_launcher/models/config.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import './../providers/providers.dart';
class Background {
String path;
Background._({required this.path});
static Future<Background> get(SettingsProvider provider) async {
final json = await _fetchMainMenu();
final info = json['pcTopPicture'];
final backgroundId = info['topPictureId'];
final videoLink = info['backgroundVideo'];
final path = "${Config.instance.path}/background.mp4";
if (provider.backgroundId == null ||
provider.backgroundId != backgroundId) {
provider.backgroundId = backgroundId;
await _updateVideo(videoLink, path);
}
return Background._(path: path);
}
static Future<Map<String, dynamic>> _fetchMainMenu() async {
// ignore: prefer_interpolation_to_compose_strings, prefer_adjacent_string_concatenation
final response = await http.get(
Uri.parse('https://media-cdn-zspms.' +
'k' +
'u' +
'r' +
'o' +
'game.net/pnswebsite/website2.0/json/G167/MainMenu.json'),
);
return jsonDecode(response.body);
}
static _updateVideo(String link, String path) async {
final response = await http.get(Uri.parse(link));
final file = File(path);
if (await file.exists()) {
// just to be sure to overwrite the file
await file.delete();
await file.create();
}
await file.writeAsBytes(response.bodyBytes);
}
}

View File

@ -0,0 +1,26 @@
import 'package:babylonia_terminal_launcher/messages/config.pb.dart';
class Config {
String path;
static late Config instance;
Config._({required this.path});
static bool _isLoadingConfig = false;
static Future update() async {
if (!_isLoadingConfig) {
_isLoadingConfig = true;
ConfigInput().sendSignalToRust();
final stream = ConfigOutput.rustSignalStream;
await for (final rustSignal in stream) {
instance = Config._(
path: rustSignal.message.configPath,
);
break;
}
_isLoadingConfig = false;
}
}
}

View File

@ -48,6 +48,20 @@ class Settings {
);
}
int? _backgroundId;
String backgroundIdKey = 'background_id';
int? get backgroundId {
_backgroundId ??= prefs.getInt(backgroundIdKey);
return _backgroundId;
}
set backgroundId(int? value) {
_backgroundId = value;
prefs.setInt(backgroundIdKey, value!);
}
static final List<BackgroundType> backgoundList = [
BackgroundType.contain,
BackgroundType.fill,

View File

@ -1,12 +1,14 @@
import 'package:flutter/material.dart';
import './../models/settings.dart';
import './../models/config.dart';
class SettingsProvider with ChangeNotifier {
late final Settings _settings;
Future init() async {
_settings = await Settings.create();
await Config.update();
}
set setSelectedBackgroundType(BackgroundType selectedBackground) {
@ -17,4 +19,12 @@ class SettingsProvider with ChangeNotifier {
get getSelectedBackgroundType {
return _settings.selectedBackgroundType;
}
int? get backgroundId {
return _settings.backgroundId;
}
set backgroundId(int? value) {
_settings.backgroundId = value;
}
}

View File

@ -1,9 +1,10 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import './../widgets/background.dart';
import '../widgets/background_widget.dart';
import './../providers/providers.dart';
import './../models/settings.dart';
import './../models/background.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@ -12,32 +13,7 @@ class HomeScreen extends StatelessWidget {
Widget build(BuildContext context) {
return Stack(
children: [
if (Provider.of<SettingsProvider>(context).getSelectedBackgroundType !=
BackgroundType.disable)
const Background()
else
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 300,
child: Image.asset('assets/images/Lee6.png'),
),
const Center(
child: Padding(
padding: EdgeInsets.all(8.0),
child: Text(
'Babylonia Terminal',
style: TextStyle(
fontSize: 34,
),
),
),
)
],
),
),
const ShowBackground(),
SizedBox(
child: Padding(
padding: const EdgeInsets.all(50.0),
@ -83,3 +59,79 @@ class HomeScreen extends StatelessWidget {
);
}
}
class ShowBackground extends StatefulWidget {
const ShowBackground({super.key});
@override
State<ShowBackground> createState() => _ShowBackgroundState();
}
class _ShowBackgroundState extends State<ShowBackground> {
bool isLoading = false;
bool hadLoaded = false;
late final Background _background;
@override
void didChangeDependencies() async {
if (!hadLoaded) {
isLoading = true;
_background =
await Background.get(Provider.of<SettingsProvider>(context));
setState(() {
isLoading = false;
hadLoaded = true;
});
}
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
if (Provider.of<SettingsProvider>(context).getSelectedBackgroundType !=
BackgroundType.disable) {
if (isLoading) {
return const DefaultBackground();
} else {
return BackgroundWidget(
background: _background,
);
}
} else {
return const DefaultBackground();
}
}
}
class DefaultBackground extends StatelessWidget {
const DefaultBackground({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 300,
child: Image.asset('assets/images/Lee6.png'),
),
const Center(
child: Padding(
padding: EdgeInsets.all(8.0),
child: Text(
'Babylonia Terminal',
style: TextStyle(
fontSize: 34,
),
),
),
)
],
),
);
}
}

View File

@ -5,17 +5,20 @@ import 'package:provider/provider.dart';
import './../providers/settings_provider.dart';
import './../models/settings.dart';
import './../models/background.dart';
class Background extends StatefulWidget {
const Background({super.key});
class BackgroundWidget extends StatefulWidget {
const BackgroundWidget({super.key, required this.background});
final Background background;
@override
State<Background> createState() => _BackgroundState();
State<BackgroundWidget> createState() => _BackgroundWidgetState();
}
class _BackgroundState extends State<Background> {
class _BackgroundWidgetState extends State<BackgroundWidget> {
late final player = Player(
configuration: PlayerConfiguration(),
configuration: const PlayerConfiguration(),
);
late final controller = VideoController(player);
@ -24,7 +27,7 @@ class _BackgroundState extends State<Background> {
super.initState();
player.open(
Media(
'/home/alez/Downloads/1l5uvj9eqpjjcazt4p-1715219648481.mp4',
widget.background.path,
),
);
player.setPlaylistMode(PlaylistMode.loop);

View File

@ -0,0 +1,10 @@
syntax = "proto3";
package config;
// [RINF:DART-SIGNAL]
message ConfigInput {}
// [RINF:RUST-SIGNAL]
message ConfigOutput {
string configPath = 1;
}

View File

@ -0,0 +1,14 @@
use babylonia_terminal_sdk::game_state::GameState;
use crate::messages::config::{ConfigInput, ConfigOutput};
pub async fn get_config() {
let mut receiver = ConfigInput::get_dart_signal_receiver();
while let Some(_) = receiver.recv().await {
let config = GameState::get_config().await;
ConfigOutput {
config_path: config.config_dir.to_str().unwrap().to_string(),
}
.send_signal_to_dart();
}
}

View File

@ -6,6 +6,7 @@
// if you're not targeting the web.
use tokio_with_wasm::tokio;
mod config;
mod game_state;
mod messages;
@ -22,4 +23,5 @@ async fn main() {
//tokio::spawn(sample_functions::stream_fractal());
//tokio::spawn(sample_functions::run_debug_tests());
tokio::spawn(game_state::get_game_state());
tokio::spawn(config::get_config());
}

View File

@ -47,6 +47,7 @@ dependencies:
rinf: ^6.9.0
protobuf: ^3.1.0
shared_preferences: ^2.2.3
http: ^1.2.1
dependency_overrides:
media_kit: