Initial commit: Fabled Android app
Flutter Android client for FabledAssistant with: - Session-cookie auth via persistent cookie jar (Dio + cookie_jar) - OAuth/SSO login via in-app WebView (flutter_inappwebview) - Notes: list, detail (markdown render), create/edit - Tasks: list with status tabs, create/edit with priority - Chat: SSE streaming bubbles, conversation management - Quick Capture FAB for rapid note/task creation - Settings screen (change server URL, logout) - Android home screen widget → opens chat - Riverpod state management, go_router navigation Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import 'package:cookie_jar/cookie_jar.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
|
||||
|
||||
import '../../core/exceptions.dart';
|
||||
|
||||
PersistCookieJar buildCookieJar(String cookiesPath) =>
|
||||
PersistCookieJar(storage: FileStorage('$cookiesPath/.cookies/'));
|
||||
|
||||
Dio buildDio(String baseUrl, PersistCookieJar cookieJar) {
|
||||
|
||||
final dio = Dio(BaseOptions(
|
||||
baseUrl: baseUrl,
|
||||
connectTimeout: const Duration(seconds: 10),
|
||||
receiveTimeout: const Duration(seconds: 30),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
));
|
||||
|
||||
dio.interceptors.add(CookieManager(cookieJar));
|
||||
dio.interceptors.add(_ErrorInterceptor());
|
||||
|
||||
return dio;
|
||||
}
|
||||
|
||||
class _ErrorInterceptor extends Interceptor {
|
||||
@override
|
||||
void onError(DioException err, ErrorInterceptorHandler handler) {
|
||||
if (err.response?.statusCode == 401) {
|
||||
handler.reject(DioException(
|
||||
requestOptions: err.requestOptions,
|
||||
error: const AuthException('Session expired. Please log in again.'),
|
||||
type: err.type,
|
||||
response: err.response,
|
||||
));
|
||||
return;
|
||||
}
|
||||
if (err.type == DioExceptionType.connectionTimeout ||
|
||||
err.type == DioExceptionType.connectionError) {
|
||||
handler.reject(DioException(
|
||||
requestOptions: err.requestOptions,
|
||||
error: const NetworkException('Cannot reach server. Check your connection.'),
|
||||
type: err.type,
|
||||
response: err.response,
|
||||
));
|
||||
return;
|
||||
}
|
||||
handler.next(err);
|
||||
}
|
||||
}
|
||||
|
||||
AppException dioToApp(DioException e) {
|
||||
if (e.error is AppException) return e.error as AppException;
|
||||
final status = e.response?.statusCode;
|
||||
if (status == 401) return const AuthException('Not authenticated.');
|
||||
if (status == 404) return const NotFoundException('Resource not found.');
|
||||
return NetworkException(e.message ?? 'Unknown network error.');
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../core/exceptions.dart';
|
||||
import 'api_client.dart';
|
||||
|
||||
class AuthApi {
|
||||
final Dio _dio;
|
||||
const AuthApi(this._dio);
|
||||
|
||||
Future<void> login(String username, String password) async {
|
||||
try {
|
||||
await _dio.post('/api/auth/login', data: {
|
||||
'username': username,
|
||||
'password': password,
|
||||
});
|
||||
} on DioException catch (e) {
|
||||
if (e.response?.statusCode == 401) {
|
||||
throw const AuthException('Invalid username or password.');
|
||||
}
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
try {
|
||||
await _dio.post('/api/auth/logout');
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> verify() async {
|
||||
try {
|
||||
await _dio.get('/api/auth/me');
|
||||
return true;
|
||||
} on DioException catch (e) {
|
||||
if (e.response?.statusCode == 401) return false;
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> getStatus() async {
|
||||
try {
|
||||
final response = await _dio.get('/api/auth/status');
|
||||
return response.data as Map<String, dynamic>;
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../models/conversation.dart';
|
||||
import '../models/message.dart';
|
||||
import 'api_client.dart';
|
||||
|
||||
class ChatApi {
|
||||
final Dio _dio;
|
||||
const ChatApi(this._dio);
|
||||
|
||||
Future<List<Conversation>> getConversations() async {
|
||||
try {
|
||||
final response = await _dio.get('/api/chat/conversations');
|
||||
final data = response.data as Map<String, dynamic>;
|
||||
final list = data['conversations'] as List<dynamic>;
|
||||
return list
|
||||
.map((e) => Conversation.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Conversation> createConversation(String title) async {
|
||||
try {
|
||||
final response = await _dio.post('/api/chat/conversations', data: {
|
||||
'title': title,
|
||||
});
|
||||
return Conversation.fromJson(response.data as Map<String, dynamic>);
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteConversation(int id) async {
|
||||
try {
|
||||
await _dio.delete('/api/chat/conversations/$id');
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
// Messages are embedded in the conversation detail response.
|
||||
Future<List<Message>> getMessages(int conversationId) async {
|
||||
try {
|
||||
final response =
|
||||
await _dio.get('/api/chat/conversations/$conversationId');
|
||||
final conv = response.data as Map<String, dynamic>;
|
||||
final list = conv['messages'] as List<dynamic>? ?? [];
|
||||
return list
|
||||
.map((e) => Message.fromJson(e as Map<String, dynamic>))
|
||||
.where((m) => m.status != 'generating') // skip in-flight placeholders
|
||||
.toList();
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
// Step 1: POST the user message — server starts background generation.
|
||||
Future<void> sendMessage(int conversationId, String content) async {
|
||||
try {
|
||||
await _dio.post(
|
||||
'/api/chat/conversations/$conversationId/messages',
|
||||
data: {'content': content},
|
||||
);
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2: GET the SSE stream and yield text chunks.
|
||||
Stream<String> streamGeneration(int conversationId) async* {
|
||||
try {
|
||||
final response = await _dio.get(
|
||||
'/api/chat/conversations/$conversationId/generation/stream',
|
||||
options: Options(
|
||||
responseType: ResponseType.stream,
|
||||
headers: {'Accept': 'text/event-stream'},
|
||||
),
|
||||
);
|
||||
|
||||
final stream = (response.data as ResponseBody).stream;
|
||||
final buf = StringBuffer();
|
||||
String currentEvent = '';
|
||||
|
||||
await for (final chunk in stream) {
|
||||
buf.write(utf8.decode(chunk));
|
||||
final raw = buf.toString();
|
||||
final lines = raw.split('\n');
|
||||
|
||||
// Keep the last (potentially incomplete) line in the buffer.
|
||||
buf.clear();
|
||||
buf.write(lines.last);
|
||||
|
||||
for (final line in lines.sublist(0, lines.length - 1)) {
|
||||
if (line.startsWith('event: ')) {
|
||||
currentEvent = line.substring(7).trim();
|
||||
} else if (line.startsWith('data: ')) {
|
||||
final jsonStr = line.substring(6).trim();
|
||||
if (currentEvent == 'chunk') {
|
||||
try {
|
||||
final data = json.decode(jsonStr) as Map<String, dynamic>;
|
||||
final text = data['text'] as String? ?? '';
|
||||
if (text.isNotEmpty) yield text;
|
||||
} catch (_) {}
|
||||
} else if (currentEvent == 'done' || currentEvent == 'error') {
|
||||
return;
|
||||
}
|
||||
} else if (line.isEmpty) {
|
||||
currentEvent = ''; // blank line = SSE event separator
|
||||
}
|
||||
}
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../models/note.dart';
|
||||
import 'api_client.dart';
|
||||
|
||||
class NotesApi {
|
||||
final Dio _dio;
|
||||
const NotesApi(this._dio);
|
||||
|
||||
Future<List<Note>> getAll() async {
|
||||
try {
|
||||
final response = await _dio.get('/api/notes');
|
||||
final data = response.data as Map<String, dynamic>;
|
||||
final list = data['notes'] as List<dynamic>;
|
||||
return list.map((e) => Note.fromJson(e as Map<String, dynamic>)).toList();
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Note> getOne(int id) async {
|
||||
try {
|
||||
final response = await _dio.get('/api/notes/$id');
|
||||
return Note.fromJson(response.data as Map<String, dynamic>);
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Note> create(String title, String body) async {
|
||||
try {
|
||||
final response = await _dio.post('/api/notes', data: {
|
||||
'title': title,
|
||||
'body': body,
|
||||
});
|
||||
return Note.fromJson(response.data as Map<String, dynamic>);
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Note> update(int id, String title, String body) async {
|
||||
try {
|
||||
final response = await _dio.put('/api/notes/$id', data: {
|
||||
'title': title,
|
||||
'body': body,
|
||||
});
|
||||
return Note.fromJson(response.data as Map<String, dynamic>);
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> delete(int id) async {
|
||||
try {
|
||||
await _dio.delete('/api/notes/$id');
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../models/task.dart';
|
||||
import 'api_client.dart';
|
||||
|
||||
class TasksApi {
|
||||
final Dio _dio;
|
||||
const TasksApi(this._dio);
|
||||
|
||||
Future<List<Task>> getAll() async {
|
||||
try {
|
||||
final response = await _dio.get('/api/tasks');
|
||||
final data = response.data as Map<String, dynamic>;
|
||||
final list = data['tasks'] as List<dynamic>;
|
||||
return list.map((e) => Task.fromJson(e as Map<String, dynamic>)).toList();
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Task> getOne(int id) async {
|
||||
try {
|
||||
final response = await _dio.get('/api/tasks/$id');
|
||||
return Task.fromJson(response.data as Map<String, dynamic>);
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Task> create({
|
||||
required String title,
|
||||
String? description,
|
||||
required TaskStatus status,
|
||||
required TaskPriority priority,
|
||||
DateTime? dueDate,
|
||||
}) async {
|
||||
try {
|
||||
final response = await _dio.post('/api/tasks', data: {
|
||||
'title': title,
|
||||
'body': description,
|
||||
'status': status.value,
|
||||
'priority': priority.value,
|
||||
'due_date': dueDate?.toIso8601String(),
|
||||
});
|
||||
return Task.fromJson(response.data as Map<String, dynamic>);
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Task> update(int id, Map<String, dynamic> fields) async {
|
||||
try {
|
||||
final response = await _dio.put('/api/tasks/$id', data: fields);
|
||||
return Task.fromJson(response.data as Map<String, dynamic>);
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> delete(int id) async {
|
||||
try {
|
||||
await _dio.delete('/api/tasks/$id');
|
||||
} on DioException catch (e) {
|
||||
throw dioToApp(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user