Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ coverage/
# Misc
*.log
.flutter-plugins-dependencies
dart-sdk/
flutter/
*.tar.xz
1 change: 1 addition & 0 deletions packages/locorda/example/minimal/lib/consts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const appBaseUrl = 'https://locorda.dev/example/minimal';
77 changes: 37 additions & 40 deletions packages/locorda/example/minimal/lib/init_locorda.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions packages/locorda/example/minimal/lib/locorda_config.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 25 additions & 40 deletions packages/locorda/example/minimal/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,6 @@ void main() async {
runApp(const MinimalTaskApp());
}

// #docregion locorda-setup
/// Initialize Locorda with worker architecture.
Future<Locorda> setupLocorda() async {
return initLocorda(
// Local Dir for testing/debugging (not for production!)
remotes: [
await DirMainIntegration.create(
displayName: 'Local Directory (Testing)',
),
],

// InMemoryStorage requires empty main handler
storage: InMemoryStorageMainHandler(),

// Configure Task resource with CRDT mapping
config: LocordaConfig(
resources: [
ResourceConfig(
type: Task,
crdtMapping: Uri.parse(
'https://locorda.dev/example/minimal/mappings/task-v1.ttl'),
indices: [FullIndex()], // Simple: fetch all tasks
),
],
),
);
}
// #enddocregion locorda-setup

class MinimalTaskApp extends StatefulWidget {
const MinimalTaskApp({super.key});

Expand All @@ -63,7 +34,22 @@ class _MinimalTaskAppState extends State<MinimalTaskApp> {

Future<void> _initialize() async {
try {
final locorda = await setupLocorda();
// #docregion locorda-setup
/// Initialize Locorda with worker architecture.

final locorda = await initLocorda(
// Local Dir for testing/debugging (not for production!)
remotes: [
await DirMainIntegration.create(
displayName: 'Local Directory (Testing)'),
],

// InMemoryStorage for simplicity - data won't persist across app restarts
storage: InMemoryStorageMainHandler(),
);

// #enddocregion locorda-setup

final taskRepo = await TaskRepository.create(locorda.syncEngine);

setState(() {
Expand All @@ -79,17 +65,13 @@ class _MinimalTaskAppState extends State<MinimalTaskApp> {
Widget build(BuildContext context) {
if (_errorMessage != null) {
return MaterialApp(
home: Scaffold(
body: Center(child: Text(_errorMessage!)),
),
home: Scaffold(body: Center(child: Text(_errorMessage!))),
);
}

if (_taskRepo == null) {
return const MaterialApp(
home: Scaffold(
body: Center(child: CircularProgressIndicator()),
),
home: Scaffold(body: Center(child: CircularProgressIndicator())),
);
}

Expand Down Expand Up @@ -166,10 +148,12 @@ class _TaskListScreenState extends State<TaskListScreen> {
onPressed: () {
final title = _controller.text.trim();
if (title.isNotEmpty) {
widget.repository.save(Task(
id: 'task_${DateTime.now().millisecondsSinceEpoch}',
title: title,
));
widget.repository.save(
Task(
id: 'task_${DateTime.now().millisecondsSinceEpoch}',
title: title,
),
);
}
_controller.clear();
Navigator.pop(context);
Expand All @@ -187,4 +171,5 @@ class _TaskListScreenState extends State<TaskListScreen> {
super.dispose();
}
}

// #enddocregion ui
7 changes: 5 additions & 2 deletions packages/locorda/example/minimal/lib/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import 'package:locorda_annotations/locorda_annotations.dart';
import 'package:locorda_rdf_mapper_annotations/annotations.dart';
import 'package:locorda_rdf_core/core.dart';
import 'package:locorda_rdf_terms_schema/schema.dart';
import 'package:minimal_task_sync/consts.dart' show appBaseUrl;

// #docregion task-model
/// A simple task with CRDT sync.
@LcrdRootResource(
IriTerm('https://locorda.dev/example/minimal/Task'),
IriTerm('$appBaseUrl/vocabulary/task#Task'),
'$appBaseUrl/mappings/task-v1.ttl',
)
class Task {
/// Unique ID for this task
Expand All @@ -22,7 +24,7 @@ class Task {
final String title;

/// Completion status - LWW (schema.org has no boolean completion property)
@RdfProperty(IriTerm('https://locorda.dev/example/minimal#completed'))
@RdfProperty(IriTerm('$appBaseUrl/vocabulary/task#completed'))
@CrdtLwwRegister()
final bool completed;

Expand All @@ -45,4 +47,5 @@ class Task {
createdAt: createdAt,
);
}

// #enddocregion task-model
10 changes: 7 additions & 3 deletions packages/locorda/example/minimal/lib/task.rdf_mapper.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions packages/locorda/example/minimal/lib/task_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TaskRepository {
/// Create and initialize repository with hydration.
static Future<TaskRepository> create(ObjectSyncEngine syncEngine) async {
final repo = TaskRepository._(syncEngine);

// Setup hydration: remote changes → local storage
repo._hydrationSubscription = await syncEngine.hydrateWithCallbacks<Task>(
getCurrentCursor: () async => null, // Simple: no cursor persistence
Expand All @@ -30,18 +30,20 @@ class TaskRepository {
repo._tasks.remove(id);
repo._notifyListeners();
},
onCursorUpdate: (cursor) async {}, // Skip cursor persistence for minimal example
onCursorUpdate:
(cursor) async {}, // Skip cursor persistence for minimal example
);

return repo;
}

/// Watch all tasks reactively
Stream<List<Task>> watchAll() => _controller.stream;

/// Get all tasks (snapshot)
List<Task> getAll() => _tasks.values.toList()
..sort((a, b) => b.createdAt.compareTo(a.createdAt));
List<Task> getAll() =>
_tasks.values.toList()
..sort((a, b) => b.createdAt.compareTo(a.createdAt));

/// Save task (create or update) - triggers sync
Future<void> save(Task task) async {
Expand All @@ -64,4 +66,5 @@ class TaskRepository {
_controller.close();
}
}

// #enddocregion repository
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const appBaseUrl = 'https://locorda.dev/example/personal_notes_app';
Loading