Skip to content
This repository was archived by the owner on May 27, 2025. It is now read-only.

Commit b3027b2

Browse files
committed
check local model before downloading
1 parent 709c82a commit b3027b2

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

flutter-app/lib/datasets_list.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,16 @@ class DatasetActions extends StatelessWidget {
289289
.orderBy("generated_at", descending: true)
290290
.getDocuments();
291291

292+
// reference to the latest model
292293
final modelInfo = snapshot.documents.first;
293294

294295
final filesToDownload = {
295296
modelInfo["model"]: "model.tflite",
296297
modelInfo["label"]: "dict.txt",
297298
};
298299

300+
final int generatedAt = modelInfo["generated_at"];
301+
299302
// create a datasets dir in app's data folder
300303
final Directory appDocDir = await getTemporaryDirectory();
301304
final Directory modelDir =
@@ -311,6 +314,20 @@ class DatasetActions extends StatelessWidget {
311314
if (!manifestJsonFile.existsSync()) {
312315
manifestJsonFile.writeAsString(jsonEncode(MANIFEST_JSON_CONTENTS));
313316
}
317+
// stores the timestamp at which the latest model was generated
318+
final File generatedAtFile = File('${modelDir.path}/generated_at');
319+
if (!generatedAtFile.existsSync()) {
320+
generatedAtFile.writeAsStringSync(modelInfo["generated_at"].toString());
321+
} else {
322+
// if the timestamp file exists, compare the timestamps to decide if the
323+
// model should be downloaded again.
324+
final storedTimestamp = int.parse(generatedAtFile.readAsStringSync());
325+
if (storedTimestamp >= generatedAt) {
326+
// newer (or same) model is stored, no need to download it again.
327+
print("[DatasetsList] Using cached model");
328+
return Future.value();
329+
}
330+
}
314331

315332
// TODO: This will be replaced by the ML Kit Model Publishing API when it becomes available.
316333
final downloadFutures = filesToDownload.keys.map((filename) async {

0 commit comments

Comments
 (0)