Skip to content

Commit

Permalink
Flatten zikr body text after calling from db directly
Browse files Browse the repository at this point in the history
  • Loading branch information
7Eltantawy committed Jul 4, 2024
1 parent c2948ef commit 3cbc763
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions alazkar/lib/src/core/helpers/azkar_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:alazkar/src/core/helpers/db_helper.dart';
import 'package:alazkar/src/core/models/zikr.dart';
import 'package:alazkar/src/core/models/zikr_extension.dart';
import 'package:alazkar/src/core/models/zikr_title.dart';
import 'package:sqflite/sqflite.dart';

Expand Down Expand Up @@ -62,9 +63,11 @@ class AzkarDBHelper {
final List<Map<String, dynamic>> maps =
await db.rawQuery('SELECT * FROM contents ORDER BY `order` ASC');

return List.generate(maps.length, (i) {
return Zikr.fromMap(maps[i]);
});
return flattenZikrBodyText(
List.generate(maps.length, (i) {
return Zikr.fromMap(maps[i]);
}),
);
}

Future<List<Zikr>> getContentsByName(String name) async {
Expand All @@ -75,9 +78,11 @@ class AzkarDBHelper {
['%$name%'],
);

return List.generate(maps.length, (i) {
return Zikr.fromMap(maps[i]);
});
return flattenZikrBodyText(
List.generate(maps.length, (i) {
return Zikr.fromMap(maps[i]);
}),
);
}

Future<List<Zikr>> getContentByTitleId(int id) async {
Expand All @@ -88,9 +93,19 @@ class AzkarDBHelper {
[id],
);

return List.generate(maps.length, (i) {
return Zikr.fromMap(maps[i]);
});
return flattenZikrBodyText(
List.generate(maps.length, (i) {
return Zikr.fromMap(maps[i]);
}),
);
}

Future<List<Zikr>> flattenZikrBodyText(List<Zikr> contents) {
return Future.wait(
contents
.map((e) async => e.copyWith(body: await e.toPlainText()))
.toList(),
);
}

Future<Zikr> getContentById(int id) async {
Expand All @@ -101,9 +116,12 @@ class AzkarDBHelper {
[id],
);

return List.generate(maps.length, (i) {
return Zikr.fromMap(maps[i]);
}).first;
return (await flattenZikrBodyText(
List.generate(maps.length, (i) {
return Zikr.fromMap(maps[i]);
}),
))
.first;
}

/// Close database
Expand Down

0 comments on commit 3cbc763

Please sign in to comment.