QuickSSH/lib/screens/home_screen.dart

188 lines
4.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:QuickSSH/screens/edit_client.dart';
import '../widgets/server_status_tile.dart';
import 'add_client.dart';
import 'package:QuickSSH/classes/ServerCommand.dart';
import 'package:QuickSSH/services/storage_service.dart';
import 'package:QuickSSH/screens/settings.dart';
import 'package:flutter/services.dart';
import 'package:QuickSSH/main.dart';
import 'package:QuickSSH/widgets/ad_banner.dart';
import 'package:QuickSSH/screens/help.dart';
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
List<ServerCommand> allCommands = [];
@override
void initState() {
super.initState();
_loadData();
WidgetsBinding.instance.addPostFrameCallback((_) async {
bool allowed = await settingsController.checkSecurity();
if (!allowed) {
SystemNavigator.pop();
}
});
}
void _loadData() async {
allCommands = await SecureStorageService.loadCommands();
setState(() {});
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context).colorScheme;
return Scaffold(
appBar: AppBar(
backgroundColor: theme.surface,
leading: Builder(
builder: (context) {
return IconButton(
icon: Icon(Icons.menu, color: theme.onSurface),
onPressed: () {
Scaffold.of(context).openDrawer();
},
);
},
),
),
drawer: Drawer(
backgroundColor: theme.primaryContainer,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: ListView(
children: [
InkWell(
borderRadius: BorderRadius.circular(16),
onTap: _openSettings,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Settings",
style: TextStyle(
color: theme.onSurface,
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
),
),
InkWell(
borderRadius: BorderRadius.circular(16),
onTap: _openHelp,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Help",
style: TextStyle(
color: theme.onSurface,
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
),
),
],
),
),
),
body: ListView.builder(
itemCount: allCommands.length,
itemBuilder: (context, index) {
// You can keep this as 'context' or rename it
return ServerStatusTile(
command: allCommands[index],
onEdit: () => _editCommand(allCommands[index], index),
);
},
),
floatingActionButton: Builder(
builder: (context) {
return FloatingActionButton(
backgroundColor: theme.primary,
shape: const CircleBorder(),
child: const Icon(
Icons.add,
color: Color.fromARGB(255, 0, 0, 0),
size: 32,
weight: 50,
),
onPressed: () {
_addCommand();
},
);
},
),
bottomNavigationBar: SafeArea(
child: MyBannerAd(isEnabled: settingsController.adsEnabled),
),
backgroundColor: theme.surface,
);
}
void _addCommand() async {
final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => const AddClient()),
);
if (result != null) {
setState(() {
allCommands.add(result);
});
SecureStorageService.saveCommands(allCommands);
}
}
_editCommand(ServerCommand cmdToEdit, int index) async {
final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EditClient(existingCommand: cmdToEdit),
),
);
if (result != null) {
if (result == "DELETE") {
setState(() {
allCommands.removeAt(index);
});
SecureStorageService.saveCommands(allCommands);
return;
}
setState(() {
allCommands[index] = result;
});
SecureStorageService.saveCommands(allCommands);
}
}
_openSettings() {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Settings()),
).then((_) {
setState(() {});
});
}
_openHelp() {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HelpScreen()),
).then((_) {
setState(() {});
});
}
}