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:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../data/api/api_client.dart';
|
||||
import '../data/api/auth_api.dart';
|
||||
import '../data/api/chat_api.dart';
|
||||
import '../data/api/notes_api.dart';
|
||||
import '../data/api/tasks_api.dart';
|
||||
import '../data/repositories/auth_repository.dart';
|
||||
import '../data/repositories/chat_repository.dart';
|
||||
import '../data/repositories/notes_repository.dart';
|
||||
import '../data/repositories/tasks_repository.dart';
|
||||
import 'settings_provider.dart';
|
||||
|
||||
final cookieJarProvider = Provider<PersistCookieJar>((ref) {
|
||||
final cookiesPath = ref.watch(cookiesPathProvider);
|
||||
return buildCookieJar(cookiesPath);
|
||||
});
|
||||
|
||||
final dioProvider = Provider<Dio>((ref) {
|
||||
final url = ref.watch(serverUrlProvider) ?? '';
|
||||
final jar = ref.watch(cookieJarProvider);
|
||||
return buildDio(url, jar);
|
||||
});
|
||||
|
||||
final authApiProvider = Provider<AuthApi>((ref) {
|
||||
return AuthApi(ref.watch(dioProvider));
|
||||
});
|
||||
|
||||
final notesApiProvider = Provider<NotesApi>((ref) {
|
||||
return NotesApi(ref.watch(dioProvider));
|
||||
});
|
||||
|
||||
final tasksApiProvider = Provider<TasksApi>((ref) {
|
||||
return TasksApi(ref.watch(dioProvider));
|
||||
});
|
||||
|
||||
final chatApiProvider = Provider<ChatApi>((ref) {
|
||||
return ChatApi(ref.watch(dioProvider));
|
||||
});
|
||||
|
||||
final authRepositoryProvider = Provider<AuthRepository>((ref) {
|
||||
return AuthRepository(ref.watch(authApiProvider));
|
||||
});
|
||||
|
||||
final notesRepositoryProvider = Provider<NotesRepository>((ref) {
|
||||
return NotesRepository(ref.watch(notesApiProvider));
|
||||
});
|
||||
|
||||
final tasksRepositoryProvider = Provider<TasksRepository>((ref) {
|
||||
return TasksRepository(ref.watch(tasksApiProvider));
|
||||
});
|
||||
|
||||
final chatRepositoryProvider = Provider<ChatRepository>((ref) {
|
||||
return ChatRepository(ref.watch(chatApiProvider));
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import 'api_client_provider.dart';
|
||||
|
||||
enum AuthStatus { unknown, authenticated, unauthenticated }
|
||||
|
||||
final authProvider = StateNotifierProvider<AuthNotifier, AuthStatus>((ref) {
|
||||
return AuthNotifier(ref);
|
||||
});
|
||||
|
||||
class AuthNotifier extends StateNotifier<AuthStatus> {
|
||||
final Ref _ref;
|
||||
|
||||
AuthNotifier(this._ref) : super(AuthStatus.unknown);
|
||||
|
||||
Future<void> verify() async {
|
||||
try {
|
||||
final repo = _ref.read(authRepositoryProvider);
|
||||
final ok = await repo.verify();
|
||||
state = ok ? AuthStatus.authenticated : AuthStatus.unauthenticated;
|
||||
} catch (_) {
|
||||
state = AuthStatus.unauthenticated;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> login(String username, String password) async {
|
||||
final repo = _ref.read(authRepositoryProvider);
|
||||
await repo.login(username, password);
|
||||
state = AuthStatus.authenticated;
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
try {
|
||||
final repo = _ref.read(authRepositoryProvider);
|
||||
await repo.logout();
|
||||
} finally {
|
||||
state = AuthStatus.unauthenticated;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../data/models/conversation.dart';
|
||||
import '../data/models/message.dart';
|
||||
import 'api_client_provider.dart';
|
||||
|
||||
final conversationsProvider =
|
||||
AsyncNotifierProvider<ConversationsNotifier, List<Conversation>>(
|
||||
ConversationsNotifier.new);
|
||||
|
||||
class ConversationsNotifier extends AsyncNotifier<List<Conversation>> {
|
||||
@override
|
||||
Future<List<Conversation>> build() async {
|
||||
return ref.watch(chatRepositoryProvider).getConversations();
|
||||
}
|
||||
|
||||
Future<Conversation> create(String title) async {
|
||||
final conv =
|
||||
await ref.read(chatRepositoryProvider).createConversation(title);
|
||||
state = AsyncData([conv, ...state.valueOrNull ?? []]);
|
||||
return conv;
|
||||
}
|
||||
|
||||
Future<void> delete(int id) async {
|
||||
await ref.read(chatRepositoryProvider).deleteConversation(id);
|
||||
state = AsyncData([
|
||||
for (final c in state.valueOrNull ?? [])
|
||||
if (c.id != id) c,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
final messagesProvider = AsyncNotifierProvider.family<MessagesNotifier,
|
||||
List<Message>, int>(MessagesNotifier.new);
|
||||
|
||||
class MessagesNotifier extends FamilyAsyncNotifier<List<Message>, int> {
|
||||
bool _isStreaming = false;
|
||||
bool get isStreaming => _isStreaming;
|
||||
|
||||
@override
|
||||
Future<List<Message>> build(int arg) async {
|
||||
return ref.watch(chatRepositoryProvider).getMessages(arg);
|
||||
}
|
||||
|
||||
Future<void> sendMessage(String content) async {
|
||||
final conversationId = arg;
|
||||
final repo = ref.read(chatRepositoryProvider);
|
||||
|
||||
// Optimistically add the user message.
|
||||
final userMsg = Message(
|
||||
conversationId: conversationId,
|
||||
role: MessageRole.user,
|
||||
content: content,
|
||||
);
|
||||
state = AsyncData([...state.valueOrNull ?? [], userMsg]);
|
||||
|
||||
// Add an empty assistant placeholder.
|
||||
final placeholder = Message(
|
||||
conversationId: conversationId,
|
||||
role: MessageRole.assistant,
|
||||
content: '',
|
||||
status: 'generating',
|
||||
);
|
||||
state = AsyncData([...state.requireValue, placeholder]);
|
||||
|
||||
_isStreaming = true;
|
||||
try {
|
||||
// Step 1: POST the message (fires background generation on server).
|
||||
await repo.sendMessage(conversationId, content);
|
||||
|
||||
// Step 2: Stream chunks and update the placeholder in place.
|
||||
await for (final chunk in repo.streamGeneration(conversationId)) {
|
||||
final msgs = state.requireValue;
|
||||
final last = msgs.last.copyWith(content: msgs.last.content + chunk);
|
||||
state = AsyncData([...msgs.sublist(0, msgs.length - 1), last]);
|
||||
}
|
||||
|
||||
// Mark the assistant message as complete.
|
||||
final msgs = state.requireValue;
|
||||
final last = msgs.last.copyWith(status: 'complete');
|
||||
state = AsyncData([...msgs.sublist(0, msgs.length - 1), last]);
|
||||
} catch (e) {
|
||||
// Remove the placeholder on error so the user can retry.
|
||||
state = AsyncData([
|
||||
for (final m in state.valueOrNull ?? [])
|
||||
if (m.status != 'generating') m,
|
||||
]);
|
||||
rethrow;
|
||||
} finally {
|
||||
_isStreaming = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final isStreamingProvider = Provider.family<bool, int>((ref, convId) {
|
||||
final notifier = ref.watch(messagesProvider(convId).notifier);
|
||||
return notifier.isStreaming;
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../data/models/note.dart';
|
||||
import 'api_client_provider.dart';
|
||||
|
||||
final notesProvider =
|
||||
AsyncNotifierProvider<NotesNotifier, List<Note>>(NotesNotifier.new);
|
||||
|
||||
class NotesNotifier extends AsyncNotifier<List<Note>> {
|
||||
@override
|
||||
Future<List<Note>> build() async {
|
||||
return ref.watch(notesRepositoryProvider).getAll();
|
||||
}
|
||||
|
||||
Future<Note> create(String title, String body) async {
|
||||
final note =
|
||||
await ref.read(notesRepositoryProvider).create(title, body);
|
||||
state = AsyncData([...state.valueOrNull ?? [], note]);
|
||||
return note;
|
||||
}
|
||||
|
||||
Future<Note> updateNote(int id, String title, String body) async {
|
||||
final updated =
|
||||
await ref.read(notesRepositoryProvider).update(id, title, body);
|
||||
state = AsyncData([
|
||||
for (final n in state.valueOrNull ?? [])
|
||||
if (n.id == id) updated else n,
|
||||
]);
|
||||
return updated;
|
||||
}
|
||||
|
||||
Future<void> delete(int id) async {
|
||||
await ref.read(notesRepositoryProvider).delete(id);
|
||||
state = AsyncData([
|
||||
for (final n in state.valueOrNull ?? [])
|
||||
if (n.id != id) n,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
final noteDetailProvider =
|
||||
FutureProvider.family<Note, int>((ref, id) async {
|
||||
return ref.watch(notesRepositoryProvider).getOne(id);
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
const _kServerUrl = 'server_url';
|
||||
|
||||
final sharedPreferencesProvider = Provider<SharedPreferences>((ref) {
|
||||
throw UnimplementedError('Override in ProviderScope');
|
||||
});
|
||||
|
||||
final cookiesPathProvider = Provider<String>((ref) {
|
||||
throw UnimplementedError('Override in ProviderScope');
|
||||
});
|
||||
|
||||
final serverUrlProvider = StateNotifierProvider<ServerUrlNotifier, String?>((ref) {
|
||||
final prefs = ref.watch(sharedPreferencesProvider);
|
||||
return ServerUrlNotifier(prefs);
|
||||
});
|
||||
|
||||
class ServerUrlNotifier extends StateNotifier<String?> {
|
||||
final SharedPreferences _prefs;
|
||||
|
||||
ServerUrlNotifier(this._prefs) : super(_prefs.getString(_kServerUrl));
|
||||
|
||||
Future<void> setUrl(String url) async {
|
||||
// Strip trailing slash
|
||||
final clean = url.endsWith('/') ? url.substring(0, url.length - 1) : url;
|
||||
await _prefs.setString(_kServerUrl, clean);
|
||||
state = clean;
|
||||
}
|
||||
|
||||
Future<void> clear() async {
|
||||
await _prefs.remove(_kServerUrl);
|
||||
state = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../data/models/task.dart';
|
||||
import 'api_client_provider.dart';
|
||||
|
||||
final tasksProvider =
|
||||
AsyncNotifierProvider<TasksNotifier, List<Task>>(TasksNotifier.new);
|
||||
|
||||
class TasksNotifier extends AsyncNotifier<List<Task>> {
|
||||
@override
|
||||
Future<List<Task>> build() async {
|
||||
return ref.watch(tasksRepositoryProvider).getAll();
|
||||
}
|
||||
|
||||
Future<Task> create({
|
||||
required String title,
|
||||
String? description,
|
||||
required TaskStatus status,
|
||||
required TaskPriority priority,
|
||||
DateTime? dueDate,
|
||||
}) async {
|
||||
final task = await ref.read(tasksRepositoryProvider).create(
|
||||
title: title,
|
||||
description: description,
|
||||
status: status,
|
||||
priority: priority,
|
||||
dueDate: dueDate,
|
||||
);
|
||||
state = AsyncData([...state.valueOrNull ?? [], task]);
|
||||
return task;
|
||||
}
|
||||
|
||||
Future<Task> updateTask(int id, Map<String, dynamic> fields) async {
|
||||
final updated = await ref.read(tasksRepositoryProvider).update(id, fields);
|
||||
state = AsyncData([
|
||||
for (final t in state.valueOrNull ?? [])
|
||||
if (t.id == id) updated else t,
|
||||
]);
|
||||
return updated;
|
||||
}
|
||||
|
||||
Future<void> delete(int id) async {
|
||||
await ref.read(tasksRepositoryProvider).delete(id);
|
||||
state = AsyncData([
|
||||
for (final t in state.valueOrNull ?? [])
|
||||
if (t.id != id) t,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user