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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
class Conversation {
|
||||
final int id;
|
||||
final String title;
|
||||
final DateTime createdAt;
|
||||
final DateTime updatedAt;
|
||||
|
||||
const Conversation({
|
||||
required this.id,
|
||||
required this.title,
|
||||
required this.createdAt,
|
||||
required this.updatedAt,
|
||||
});
|
||||
|
||||
factory Conversation.fromJson(Map<String, dynamic> json) => Conversation(
|
||||
id: json['id'] as int,
|
||||
title: json['title'] as String,
|
||||
createdAt: DateTime.parse(json['created_at'] as String),
|
||||
updatedAt: DateTime.parse(json['updated_at'] as String),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
enum MessageRole { user, assistant }
|
||||
|
||||
class Message {
|
||||
final int? id;
|
||||
final int conversationId;
|
||||
final MessageRole role;
|
||||
final String content;
|
||||
final String status; // "complete" | "generating"
|
||||
final DateTime? createdAt;
|
||||
|
||||
const Message({
|
||||
this.id,
|
||||
required this.conversationId,
|
||||
required this.role,
|
||||
required this.content,
|
||||
this.status = 'complete',
|
||||
this.createdAt,
|
||||
});
|
||||
|
||||
factory Message.fromJson(Map<String, dynamic> json) => Message(
|
||||
id: json['id'] as int?,
|
||||
conversationId: json['conversation_id'] as int,
|
||||
role: json['role'] == 'user' ? MessageRole.user : MessageRole.assistant,
|
||||
content: json['content'] as String,
|
||||
status: json['status'] as String? ?? 'complete',
|
||||
createdAt: json['created_at'] != null
|
||||
? DateTime.parse(json['created_at'] as String)
|
||||
: null,
|
||||
);
|
||||
|
||||
Message copyWith({String? content, String? status}) => Message(
|
||||
id: id,
|
||||
conversationId: conversationId,
|
||||
role: role,
|
||||
content: content ?? this.content,
|
||||
status: status ?? this.status,
|
||||
createdAt: createdAt,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
class Note {
|
||||
final int id;
|
||||
final String title;
|
||||
final String body;
|
||||
final DateTime createdAt;
|
||||
final DateTime updatedAt;
|
||||
|
||||
const Note({
|
||||
required this.id,
|
||||
required this.title,
|
||||
required this.body,
|
||||
required this.createdAt,
|
||||
required this.updatedAt,
|
||||
});
|
||||
|
||||
factory Note.fromJson(Map<String, dynamic> json) => Note(
|
||||
id: json['id'] as int,
|
||||
title: json['title'] as String? ?? '',
|
||||
body: json['body'] as String? ?? '',
|
||||
createdAt: DateTime.parse(json['created_at'] as String),
|
||||
updatedAt: DateTime.parse(json['updated_at'] as String),
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'title': title,
|
||||
'body': body,
|
||||
};
|
||||
|
||||
Note copyWith({String? title, String? body}) => Note(
|
||||
id: id,
|
||||
title: title ?? this.title,
|
||||
body: body ?? this.body,
|
||||
createdAt: createdAt,
|
||||
updatedAt: updatedAt,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
enum TaskStatus { todo, inProgress, done }
|
||||
|
||||
enum TaskPriority { none, low, medium, high }
|
||||
|
||||
extension TaskStatusExtension on TaskStatus {
|
||||
String get value => switch (this) {
|
||||
TaskStatus.todo => 'todo',
|
||||
TaskStatus.inProgress => 'in_progress',
|
||||
TaskStatus.done => 'done',
|
||||
};
|
||||
|
||||
String get label => switch (this) {
|
||||
TaskStatus.todo => 'To Do',
|
||||
TaskStatus.inProgress => 'In Progress',
|
||||
TaskStatus.done => 'Done',
|
||||
};
|
||||
|
||||
static TaskStatus fromString(String? s) => switch (s) {
|
||||
'in_progress' => TaskStatus.inProgress,
|
||||
'done' => TaskStatus.done,
|
||||
_ => TaskStatus.todo,
|
||||
};
|
||||
}
|
||||
|
||||
extension TaskPriorityExtension on TaskPriority {
|
||||
String get value => switch (this) {
|
||||
TaskPriority.none => 'none',
|
||||
TaskPriority.low => 'low',
|
||||
TaskPriority.medium => 'medium',
|
||||
TaskPriority.high => 'high',
|
||||
};
|
||||
|
||||
String get label => switch (this) {
|
||||
TaskPriority.none => 'None',
|
||||
TaskPriority.low => 'Low',
|
||||
TaskPriority.medium => 'Medium',
|
||||
TaskPriority.high => 'High',
|
||||
};
|
||||
|
||||
static TaskPriority fromString(String? s) => switch (s) {
|
||||
'high' => TaskPriority.high,
|
||||
'medium' => TaskPriority.medium,
|
||||
'low' => TaskPriority.low,
|
||||
_ => TaskPriority.none,
|
||||
};
|
||||
}
|
||||
|
||||
class Task {
|
||||
final int id;
|
||||
final String title;
|
||||
final String? description;
|
||||
final TaskStatus status;
|
||||
final TaskPriority priority;
|
||||
final DateTime? dueDate;
|
||||
final DateTime createdAt;
|
||||
final DateTime updatedAt;
|
||||
|
||||
const Task({
|
||||
required this.id,
|
||||
required this.title,
|
||||
this.description,
|
||||
required this.status,
|
||||
required this.priority,
|
||||
this.dueDate,
|
||||
required this.createdAt,
|
||||
required this.updatedAt,
|
||||
});
|
||||
|
||||
factory Task.fromJson(Map<String, dynamic> json) => Task(
|
||||
id: json['id'] as int,
|
||||
title: json['title'] as String? ?? '',
|
||||
description: json['body'] as String?,
|
||||
status: TaskStatusExtension.fromString(json['status'] as String?),
|
||||
priority: TaskPriorityExtension.fromString(json['priority'] as String?),
|
||||
dueDate: json['due_date'] != null
|
||||
? DateTime.parse(json['due_date'] as String)
|
||||
: null,
|
||||
createdAt: DateTime.parse(json['created_at'] as String),
|
||||
updatedAt: DateTime.parse(json['updated_at'] as String),
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'title': title,
|
||||
'body': description,
|
||||
'status': status.value,
|
||||
'priority': priority.value,
|
||||
'due_date': dueDate?.toIso8601String(),
|
||||
};
|
||||
|
||||
Task copyWith({
|
||||
String? title,
|
||||
String? description,
|
||||
TaskStatus? status,
|
||||
TaskPriority? priority,
|
||||
DateTime? dueDate,
|
||||
}) =>
|
||||
Task(
|
||||
id: id,
|
||||
title: title ?? this.title,
|
||||
description: description ?? this.description,
|
||||
status: status ?? this.status,
|
||||
priority: priority ?? this.priority,
|
||||
dueDate: dueDate ?? this.dueDate,
|
||||
createdAt: createdAt,
|
||||
updatedAt: updatedAt,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import '../api/auth_api.dart';
|
||||
|
||||
class AuthRepository {
|
||||
final AuthApi _api;
|
||||
const AuthRepository(this._api);
|
||||
|
||||
Future<void> login(String username, String password) =>
|
||||
_api.login(username, password);
|
||||
|
||||
Future<void> logout() => _api.logout();
|
||||
|
||||
Future<bool> verify() => _api.verify();
|
||||
|
||||
Future<Map<String, dynamic>> getStatus() => _api.getStatus();
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import '../api/chat_api.dart';
|
||||
import '../models/conversation.dart';
|
||||
import '../models/message.dart';
|
||||
|
||||
class ChatRepository {
|
||||
final ChatApi _api;
|
||||
const ChatRepository(this._api);
|
||||
|
||||
Future<List<Conversation>> getConversations() => _api.getConversations();
|
||||
Future<Conversation> createConversation(String title) =>
|
||||
_api.createConversation(title);
|
||||
Future<void> deleteConversation(int id) => _api.deleteConversation(id);
|
||||
Future<List<Message>> getMessages(int conversationId) =>
|
||||
_api.getMessages(conversationId);
|
||||
Future<void> sendMessage(int conversationId, String content) =>
|
||||
_api.sendMessage(conversationId, content);
|
||||
Stream<String> streamGeneration(int conversationId) =>
|
||||
_api.streamGeneration(conversationId);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import '../api/notes_api.dart';
|
||||
import '../models/note.dart';
|
||||
|
||||
class NotesRepository {
|
||||
final NotesApi _api;
|
||||
const NotesRepository(this._api);
|
||||
|
||||
Future<List<Note>> getAll() => _api.getAll();
|
||||
Future<Note> getOne(int id) => _api.getOne(id);
|
||||
Future<Note> create(String title, String body) =>
|
||||
_api.create(title, body);
|
||||
Future<Note> update(int id, String title, String body) =>
|
||||
_api.update(id, title, body);
|
||||
Future<void> delete(int id) => _api.delete(id);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import '../api/tasks_api.dart';
|
||||
import '../models/task.dart';
|
||||
|
||||
class TasksRepository {
|
||||
final TasksApi _api;
|
||||
const TasksRepository(this._api);
|
||||
|
||||
Future<List<Task>> getAll() => _api.getAll();
|
||||
Future<Task> getOne(int id) => _api.getOne(id);
|
||||
|
||||
Future<Task> create({
|
||||
required String title,
|
||||
String? description,
|
||||
required TaskStatus status,
|
||||
required TaskPriority priority,
|
||||
DateTime? dueDate,
|
||||
}) =>
|
||||
_api.create(
|
||||
title: title,
|
||||
description: description,
|
||||
status: status,
|
||||
priority: priority,
|
||||
dueDate: dueDate,
|
||||
);
|
||||
|
||||
Future<Task> update(int id, Map<String, dynamic> fields) =>
|
||||
_api.update(id, fields);
|
||||
|
||||
Future<void> delete(int id) => _api.delete(id);
|
||||
}
|
||||
Reference in New Issue
Block a user