Skip to content

Implement Optional downloadModel Utility in RN and Flutter for Consistency #97

@rshemet

Description

@rshemet

⚠️ This may appear complex but in reality this issue is quite simple. We tried to ensure it's thoroughly documented for ease of contribution ⚠️

Problem Overview

There is a discrepancy in model initialization between Flutter and React Native (RN): Flutter's CactusContext.init automatically downloads models if modelUrl is provided, while RN requires manual handling of local paths. To unify, we need to remove auto-download from Flutter's init, making both platforms require a local path for consistency.

This keeps init lean and avoids forcing dependencies in RN (since RN does not ship with file management modules).

Note: This is part 2 of a two-part unification effort (remove auto-download from Flutter). See part 1 for removing auto-download from Flutter: #96

Task

Flutter (flutter/lib/utils/download.dart):

  • Create a new file and implement Future<String> downloadModel(String url, {String? filename, Function(double, String)? onProgress}).
  • Use Dart's built-in http package: Check if file exists in app docs dir; download if not; report progress via callback (e.g., percentage and status like "Downloading...").
  • Return the local path on success.

React Native (src/utils/download.ts):

  • Create a new file and implement the same function signature using react-native-fs (treat as optional peer dependency and throw clear error like "Install react-native-fs to use downloadModel" if not found).

  • Check if file exists; download to DocumentDirectoryPath; support progress callback.

  • In src/index.ts, export it with a try-catch for missing rn-fs (e.g., export { downloadModel } from './utils/download';).

  • For both: Handle basics like custom filenames, retries on failure (simple 3-try logic), and caching (skip if file exists and is valid/non-zero size).

  • Update docs and examples in both platforms to show downloadModel usage before init (e.g., const path = await downloadModel(url); await init({ model: path });).

Contributor Checklist

  • Install required deps if needed (Flutter: none; RN: yarn add react-native-fs for testing).
  • Test on device/simulator: Download a small test file; verify progress callback works; check error handling (e.g., bad URL, no network).
  • Keep in mind: No forced deps in RN core: utility should fail gracefully if rn-fs missing. Support large files (progress essential).
  • Align signatures exactly between platforms (same params, return local path string).
  • Update README.md with examples; ensure no changes to core init functions here (that's part 1).
  • Run linter/tests; in PR, bonus points for demo with screenshots/logs of download working :) .

PR Checklist

  • Functions return local path; progress callback fires correctly (e.g., 0-1.0 scale).
  • RN version doesn't break if rn-fs not installed (throws helpful error).
  • Examples show end-to-end: download + init.
  • Good for beginners with file/network handling; cross-links to part 1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions