This commit is contained in:
2026-04-28 22:04:24 +08:00
parent 80ee99e564
commit 71c940ab46
156 changed files with 5700 additions and 304 deletions

View File

@@ -23,8 +23,8 @@ class _HomeScreenState extends State<HomeScreen> {
selectedIndex: _index,
onDestinationSelected: (i) => setState(() => _index = i),
destinations: const [
NavigationDestination(icon: Icon(Icons.sms_outlined), selectedIcon: Icon(Icons.sms), label: 'Messages'),
NavigationDestination(icon: Icon(Icons.settings_outlined), selectedIcon: Icon(Icons.settings), label: 'Settings'),
NavigationDestination(icon: Icon(Icons.sms_outlined), selectedIcon: Icon(Icons.sms), label: '短信'),
NavigationDestination(icon: Icon(Icons.settings_outlined), selectedIcon: Icon(Icons.settings), label: '设置'),
],
),
);

View File

@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import '../main.dart';
import 'home_screen.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({super.key});
@@ -10,30 +11,30 @@ class LoginScreen extends StatefulWidget {
class _LoginScreenState extends State<LoginScreen> {
final _serverCtrl = TextEditingController(text: 'http://192.168.1.100:3000');
final _userCtrl = TextEditingController();
final _passCtrl = TextEditingController();
final _tokenCtrl = TextEditingController();
bool _loading = false;
String? _error;
Future<void> _submit(bool register) async {
Future<void> _connect() async {
setState(() { _loading = true; _error = null; });
final url = _serverCtrl.text.trim();
final user = _userCtrl.text.trim();
final pass = _passCtrl.text.trim();
final token = _tokenCtrl.text.trim();
try {
apiService.configure(url, '');
final token = register
? await apiService.register(user, pass)
: await apiService.login(user, pass);
final valid = await apiService.verifyToken(token);
if (!valid) {
setState(() { _error = '令牌无效'; _loading = false; });
return;
}
apiService.configure(url, token);
await settings.setServerUrl(url);
await settings.setToken(token);
await apiService.registerDevice('Android');
if (mounted) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => const HomeScreen()),
MaterialPageRoute(builder: (_) => HomeScreen()),
);
}
} catch (e) {
@@ -61,55 +62,36 @@ class _LoginScreenState extends State<LoginScreen> {
TextField(
controller: _serverCtrl,
decoration: const InputDecoration(
labelText: 'Server URL',
hintText: 'http://your-server:3000',
labelText: '服务器地址',
hintText: 'http://服务器IP:3000',
prefixIcon: Icon(Icons.dns_outlined),
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
TextField(
controller: _userCtrl,
decoration: const InputDecoration(
labelText: 'Username',
prefixIcon: Icon(Icons.person_outline),
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
TextField(
controller: _passCtrl,
controller: _tokenCtrl,
obscureText: true,
decoration: const InputDecoration(
labelText: 'Password',
prefixIcon: Icon(Icons.lock_outline),
labelText: '访问令牌',
prefixIcon: Icon(Icons.key),
border: OutlineInputBorder(),
),
onSubmitted: (_) => _submit(false),
onSubmitted: (_) => _connect(),
),
if (_error != null) ...[
const SizedBox(height: 12),
Text(_error!, style: TextStyle(color: colors.error)),
],
const SizedBox(height: 24),
Row(
children: [
Expanded(
child: OutlinedButton(
onPressed: _loading ? null : () => _submit(true),
child: const Text('Register'),
),
),
const SizedBox(width: 16),
Expanded(
child: FilledButton(
onPressed: _loading ? null : () => _submit(false),
child: _loading
? const SizedBox(height: 20, width: 20, child: CircularProgressIndicator(strokeWidth: 2))
: const Text('Sign In'),
),
),
],
SizedBox(
width: double.infinity,
child: FilledButton(
onPressed: _loading ? null : _connect,
child: _loading
? const SizedBox(height: 20, width: 20, child: CircularProgressIndicator(strokeWidth: 2))
: const Text('连接'),
),
),
],
),

View File

@@ -1,16 +1,22 @@
import 'package:flutter/material.dart';
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
import '../main.dart';
import 'login_screen.dart';
class SettingsScreen extends StatelessWidget {
class SettingsScreen extends StatefulWidget {
const SettingsScreen({super.key});
@override
State<SettingsScreen> createState() => _SettingsScreenState();
}
class _SettingsScreenState extends State<SettingsScreen> {
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
return Scaffold(
appBar: AppBar(title: const Text('Settings')),
appBar: AppBar(title: const Text('设置')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
@@ -22,11 +28,11 @@ class SettingsScreen extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Server', style: Theme.of(context).textTheme.titleMedium),
Text('服务器', style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 8),
Text('URL: ${settings.serverUrl ?? "Not set"}',
Text('地址: ${settings.serverUrl ?? "未设置"}',
style: Theme.of(context).textTheme.bodyMedium?.copyWith(color: colors.onSurfaceVariant)),
Text('Token: ${settings.token != null ? "Configured" : "Missing"}',
Text('令牌: ${settings.token != null ? "已配置" : "未设置"}',
style: Theme.of(context).textTheme.bodyMedium?.copyWith(color: colors.onSurfaceVariant)),
],
),
@@ -39,13 +45,24 @@ class SettingsScreen extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Upload Mode', style: Theme.of(context).textTheme.titleMedium),
Text('上传模式', style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 8),
SwitchListTile(
title: const Text('Auto Upload'),
subtitle: const Text('Automatically upload new incoming SMS'),
title: const Text('自动上传'),
subtitle: Text(
settings.autoUpload
? '后台服务运行中,自动上传新短信'
: '自动上传新收到的短信',
style: TextStyle(color: settings.autoUpload ? colors.primary : null),
),
value: settings.autoUpload,
onChanged: (v) => settings.setAutoUpload(v),
onChanged: (v) {
settings.setAutoUpload(v);
if (!v) {
FlutterForegroundTask.stopService();
}
setState(() {});
},
contentPadding: EdgeInsets.zero,
),
],
@@ -57,14 +74,16 @@ class SettingsScreen extends StatelessWidget {
width: double.infinity,
child: OutlinedButton.icon(
onPressed: () {
FlutterForegroundTask.stopService();
settings.setToken('');
settings.setAutoUpload(false);
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (_) => const LoginScreen()),
(_) => false,
);
},
icon: const Icon(Icons.logout),
label: const Text('Logout'),
label: const Text('断开连接'),
style: OutlinedButton.styleFrom(foregroundColor: colors.error),
),
),

View File

@@ -1,8 +1,10 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
import '../models/sms_message.dart';
import '../main.dart';
import '../services/sms_foreground_handler.dart';
class SmsListScreen extends StatefulWidget {
const SmsListScreen({super.key});
@@ -24,7 +26,6 @@ class _SmsListScreenState extends State<SmsListScreen> {
@override
void initState() {
super.initState();
_uploadedKeys = settings.getUploadedKeys();
_requestPermission();
}
@@ -32,16 +33,35 @@ class _SmsListScreenState extends State<SmsListScreen> {
final status = await Permission.sms.request();
if (mounted) {
setState(() => _hasSmsPermission = status.isGranted);
if (status.isGranted) _loadSms();
if (status.isGranted) {
_loadSms();
if (settings.autoUpload) _startAutoUpload();
}
}
}
Future<void> _loadSms() async {
setState(() => _loading = true);
try {
_messages = await smsReaderService.queryAllSms();
final local = await smsReaderService.queryAllSms();
Set<String> serverKeys = {};
try {
final serverMsgs = await apiService.fetchServerSms();
serverKeys = serverMsgs.map((m) {
final phone = m['phone_number'] as String? ?? '';
final content = m['content'] as String? ?? '';
final date = m['sms_date'] as String? ?? '';
return '$phone|$content|$date';
}).toSet();
} catch (_) {}
setState(() {
_messages = local;
_uploadedKeys = serverKeys;
});
} catch (e) { /* ignore */ }
setState(() => _loading = false);
if (mounted) setState(() => _loading = false);
}
bool _isUploaded(SmsMessage m) => _uploadedKeys.contains(_dedupKey(m));
@@ -57,19 +77,20 @@ class _SmsListScreenState extends State<SmsListScreen> {
if (toUpload.isEmpty) return;
try {
await apiService.uploadSms(toUpload);
final newKeys = toUpload.map(_dedupKey).toList();
settings.addUploadedKeys(newKeys);
_uploadedKeys = settings.getUploadedKeys();
_selected.clear();
final newKeys = toUpload.map(_dedupKey).toSet();
setState(() {
_uploadedKeys = _uploadedKeys.union(newKeys);
_selected.clear();
});
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Uploaded ${toUpload.length} messages')),
SnackBar(content: Text('已上传 ${toUpload.length} 条短信')),
);
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Upload failed: $e')),
SnackBar(content: Text('上传失败: $e')),
);
}
}
@@ -80,50 +101,68 @@ class _SmsListScreenState extends State<SmsListScreen> {
if (toUpload.isEmpty) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('No new messages to upload')),
const SnackBar(content: Text('没有新短信需要上传')),
);
}
return;
}
try {
final count = await apiService.uploadSms(toUpload);
final newKeys = toUpload.map(_dedupKey).toList();
settings.addUploadedKeys(newKeys);
_uploadedKeys = settings.getUploadedKeys();
await apiService.uploadSms(toUpload);
final newKeys = toUpload.map(_dedupKey).toSet();
setState(() {
_uploadedKeys = _uploadedKeys.union(newKeys);
});
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Uploaded $count messages')),
SnackBar(content: Text('已上传 ${toUpload.length} 条短信')),
);
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Upload failed: $e')),
SnackBar(content: Text('上传失败: $e')),
);
}
}
}
void _toggleAutoUpload() {
Future<void> _toggleAutoUpload() async {
final newVal = !settings.autoUpload;
settings.setAutoUpload(newVal);
if (newVal) {
// Request notification permission for foreground service
await Permission.notification.request();
_startForegroundService();
_startAutoUpload();
} else {
_stopForegroundService();
_smsSub?.cancel();
_smsSub = null;
}
setState(() {});
}
void _startForegroundService() {
FlutterForegroundTask.startService(
notificationTitle: 'SMS Monitor',
notificationText: '正在监听新短信...',
callback: startCallback,
);
}
void _stopForegroundService() {
FlutterForegroundTask.stopService();
}
void _startAutoUpload() {
_smsSub = smsReaderService.listenToIncoming().listen((sms) async {
try {
await apiService.uploadSms([sms]);
settings.addUploadedKey(_dedupKey(sms));
_uploadedKeys = settings.getUploadedKeys();
_messages.insert(0, sms);
if (mounted) setState(() {});
setState(() {
_uploadedKeys.add(_dedupKey(sms));
_messages.insert(0, sms);
});
} catch (_) {}
});
}
@@ -140,10 +179,10 @@ class _SmsListScreenState extends State<SmsListScreen> {
return Scaffold(
appBar: AppBar(
title: const Text('Messages'),
title: const Text('短信'),
actions: [
if (_selected.isNotEmpty)
IconButton(icon: const Icon(Icons.cloud_upload), onPressed: _uploadSelected, tooltip: 'Upload selected'),
IconButton(icon: const Icon(Icons.cloud_upload), onPressed: _uploadSelected, tooltip: '上传选中'),
IconButton(icon: const Icon(Icons.refresh), onPressed: _hasSmsPermission ? _loadSms : _requestPermission),
],
),
@@ -152,16 +191,16 @@ class _SmsListScreenState extends State<SmsListScreen> {
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text('SMS permission required'),
const Text('需要短信权限'),
const SizedBox(height: 12),
FilledButton(onPressed: _requestPermission, child: const Text('Grant')),
FilledButton(onPressed: _requestPermission, child: const Text('授权')),
],
),
)
: _loading
? const Center(child: CircularProgressIndicator())
: _messages.isEmpty
? const Center(child: Text('No messages'))
? const Center(child: Text('暂无短信'))
: RefreshIndicator(
onRefresh: _loadSms,
child: ListView.builder(
@@ -219,7 +258,7 @@ class _SmsListScreenState extends State<SmsListScreen> {
FloatingActionButton(
heroTag: 'upload',
onPressed: _uploadAll,
tooltip: 'Upload all',
tooltip: '全部上传',
child: const Icon(Icons.cloud_upload),
),
],