aves_mio/lib/remote/remote_test_page.dart
Fabio Micheluz 452c378178
Some checks are pending
Quality check / Flutter analysis (push) Waiting to run
Quality check / CodeQL analysis (java-kotlin) (push) Waiting to run
f1
2026-02-28 13:42:46 +01:00

129 lines
4.2 KiB
Dart

// lib/remote/remote_test_page.dart
import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';
class RemoteTestPage extends StatefulWidget {
final Database db;
final String baseUrl; // es. https://prova.patachina.it
const RemoteTestPage({super.key, required this.db, required this.baseUrl});
@override
State<RemoteTestPage> createState() => _RemoteTestPageState();
}
class _RemoteTestPageState extends State<RemoteTestPage> {
late Future<List<_RemoteRow>> _future;
@override
void initState() {
super.initState();
_future = _load();
}
Future<List<_RemoteRow>> _load() async {
// prendi le prime 200 entry remote
final rows = await widget.db.rawQuery(
"SELECT id, title, remotePath, remoteThumb2 FROM entry WHERE origin=1 ORDER BY id DESC LIMIT 200",
);
return rows.map((r) => _RemoteRow(
id: r['id'] as int,
title: (r['title'] as String?) ?? '',
remotePath: r['remotePath'] as String?,
remoteThumb2: r['remoteThumb2'] as String?,
)).toList();
}
String _url(String? rel) {
if (rel == null || rel.isEmpty) return '';
var base = widget.baseUrl;
if (!base.endsWith('/')) base = '$base/';
final cleaned = rel.startsWith('/') ? rel.substring(1) : rel;
return '$base$cleaned';
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('[DEBUG] Remote Test')),
body: FutureBuilder<List<_RemoteRow>>(
future: _future,
builder: (context, snap) {
if (snap.connectionState != ConnectionState.done) {
return const Center(child: CircularProgressIndicator());
}
if (snap.hasError) {
return Center(child: Text('Errore: ${snap.error}'));
}
final items = snap.data ?? const <_RemoteRow>[];
if (items.isEmpty) {
return const Center(child: Text('Nessuna entry remota (origin=1)'));
}
return GridView.builder(
padding: const EdgeInsets.all(8),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3, mainAxisSpacing: 4, crossAxisSpacing: 4),
itemCount: items.length,
itemBuilder: (context, i) {
final it = items[i];
final thumbUrl = _url(it.remoteThumb2 ?? it.remotePath);
final fullUrl = _url(it.remotePath);
return GestureDetector(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (_) => _RemoteFullPage(title: it.title, url: fullUrl),
));
},
child: Hero(
tag: 'remote_${it.id}',
child: DecoratedBox(
decoration: BoxDecoration(border: Border.all(color: Colors.black12)),
child: thumbUrl.isEmpty
? const ColoredBox(color: Colors.black12)
: Image.network(
thumbUrl,
fit: BoxFit.cover,
errorBuilder: (_, __, ___) => const Center(child: Icon(Icons.broken_image)),
),
),
),
);
},
);
},
),
);
}
}
class _RemoteRow {
final int id;
final String title;
final String? remotePath;
final String? remoteThumb2;
_RemoteRow({required this.id, required this.title, this.remotePath, this.remoteThumb2});
}
class _RemoteFullPage extends StatelessWidget {
final String title;
final String url;
const _RemoteFullPage({super.key, required this.title, required this.url});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title.isEmpty ? 'Remote' : title)),
body: Center(
child: url.isEmpty
? const Text('URL non valido')
: InteractiveViewer(
maxScale: 5,
child: Image.network(
url,
fit: BoxFit.contain,
errorBuilder: (_, __, ___) => const Icon(Icons.broken_image, size: 64),
),
),
),
);
}
}