SimpleRclone Progress Report 2

I’ve been a busy bee - unfortunately not so much with SimpleRclone as with work and life 😅. I did make some progress on some important basics though!

State management

I mentioned in the last progress report that I wanted to throw out flutter_it (pushing AI in docs = yuck) and replace it with something simpler. Since then I’ve figured out how I can use klizma as a far simpler option and vendored it into the codebase.

Klizma is not a whole big state management system, it’s just a service locator. It provides an object that you can use as a global variable, which I called sl for service locator. You can then register constructors on this global variable, eg. sl.provide<RcloneRepository>(() => RcloneRepository()). You can then get an RcloneRepository object by calling sl.get<RcloneRepository>() - either a singleton or a fresh one. The object is constructed lazily, whenever get is called.

So that part replaces get_it pretty well, but it’s notably missing an equivalent to registerAsync(), there is only a synchronous constructor method. Instead of implementing it myself, I just hacked my way around it by giving my services that need asynchronous initialisation a ready property and turning them into ChangeNotifiers that can be listened to with a ListenableBuilder, like this simplified example:

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    final permissionService = sl.get<PermissionService>();
    final rcloneService = sl.get<RcloneService>();

    return ListenableBuilder(
      listenable: Listenable.merge([permissionService, rcloneService]),
      builder: (context, _) {
        if (!rcloneService.ready) {
          return CircularProgressIndicator();
        } else if (!permissionService.allGranted) {
          return MaterialApp(home: PermissionView());
        } else {
          return MaterialApp(home: TaskView());
        }
      },
    );
  }
}

Here we effectively first wait for RcloneService to be ready, then we use PermissionService.allGranted to check for permissions and show the PermissionView if they have not been granted yet. If both are done, we can show the main view.

I also generally just use ChangeNotifiers with ListenableBuilder in place of the functionality from watch_it - they made it quite convenient with WatchingWidget but hey, successfully removed some dependencies and abstractions!

And to replace command_it, I just decided to go with simple async methods and… you guessed, it, ChangeNotifier with ListenableBuilder to respond to completion of the async method.

Anyway, no idea if this is ideal or if I’ll keep it this way, perhaps flutter professionals are screaming at their screen, but it works for me for now.

New rclone API models

Besides state management, another pain point I mentioned last time was the compromise between API data structures and the application data structures that I’d really like to use.

Because the compromise frankly sucked, I got started with re-doing all the data structures, starting with the huge amount of JSON returned by rclone’s config/providers command. I made it a pure datastructure clone of the JSON this time and that helped the quite a bit by reducing thinking and transfrormation. The application data structures will happen later, when I have written more of the application and figured out what I really need.

One cool thing I learned about there is pattern-based destructuring, which I decided to use heavily. Here is the simplest example:

class ExampleApiModel {
  final String help;
  final String value;

  ExampleApiModel({required this.help, required this.value});

  factory ExampleApiModel.fromJson(Map json) {
    if (json case {"Help": String help, "Value": String value}) {
      return ExampleApiModel(help: help, value: value);
    } else {
      throw const FormatException("ExampleApiModel: Unexpected JSON format");
    }
  }
}

The important part here is the if (json case {...}) statement. Here we can use case, followed by a pattern that mirrors the expected JSON format. The pattern itself includes the variable names that the values are assigned to if the pattern matches. If it doesn’t match, we go to else and throw an exception. For models that contain additional models (not just basic types like this one), the object constructor call then contains the call to the other model’s constructor.

Optional keys were a bit tricky because patterns can’t have optional keys, so I worked around it by inserting the optional keys upfront with value null and then using eg. String? in the pattern.

What’s next

Next, I will properly implement the TaskView that is teased in the first example. Perhaps I’ll do the UI first and then add an SQlite database later, not sure about the order yet - generally I like to go top-down in terms of abstraction level so that I don’t implement lower level things that the higher level doesn’t really need or would ideally use differently.

See you next time!