use ContentService (not legacy endpoint) for didvanvoice list

This commit is contained in:
Mohamad Mahdi Jebeli 2026-06-22 14:21:19 +03:30
parent 2253c71338
commit 8317dc74af
1 changed files with 39 additions and 25 deletions

View File

@ -2,6 +2,7 @@ import 'package:didvan/models/notification_message.dart';
import 'package:didvan/models/didvan_voice_model.dart';
import 'package:didvan/models/didvan_plus_model.dart';
import 'package:didvan/services/app_initalizer.dart';
import 'package:didvan/services/content/content_service.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:home_widget/home_widget.dart';
@ -326,35 +327,48 @@ class HomeWidgetRepository {
case 'didvanvoice':
case 'media':
// DidvanVoiceList expects List<DidvanVoiceModel>. The notification
// doesn't carry the full list, so we fetch it on the fly the same
// way the legacy int-id branch does.
// The list page expects List<DidvanVoiceModel>. New content is
// served by content-service (UUID); we fetch it by category id
// the same way main_page_state._getDidvanVoice does, then turn
// each row into a DidvanVoiceModel.
const didvanVoiceCategoryId =
'a7f3c9e2-5b18-4d6a-9c7e-3f8b1d5e2a91';
try {
final service = RequestService(RequestHelper.didvanVoice);
await service.httpGet();
if (service.statusCode == 200) {
final rawData = service.data('result');
List<DidvanVoiceModel> voices = [];
if (rawData is List && rawData.isNotEmpty) {
voices = rawData
.map((e) => DidvanVoiceModel.fromJson(
Map<String, dynamic>.from(e as Map)))
.toList();
} else if (rawData is Map) {
voices = [
DidvanVoiceModel.fromJson(
Map<String, dynamic>.from(rawData)),
];
final res = await ContentService.instance.getContents(
page: 1,
limit: 20,
categoryId: didvanVoiceCategoryId,
sortBy: const [MapEntry('createdAt', 'DESC')],
);
if (!res.isSuccess || res.data == null) {
route = Routes.home;
break;
}
// List endpoint doesn't return body — fetch detail for each
// item to get the audio URL.
final fetches = res.data!.data.map((c) async {
final detail = await ContentService.instance.getContent(c.id);
if (detail.isSuccess && detail.data != null) return detail.data!;
return c;
}).toList();
final fullItems = await Future.wait(fetches);
final voices = fullItems
.map((c) => DidvanVoiceModel(
id: c.id.hashCode,
title: c.title,
file: c.audioUrl ?? '',
image: c.coverImage ?? '',
publishedAt: (c.publishedAt ?? c.createdAt)
?.toIso8601String() ??
DateTime.now().toIso8601String(),
))
.toList();
if (voices.isNotEmpty) {
route = Routes.didvanVoiceList;
args = voices;
} else {
route = Routes.home;
}
} else {
route = Routes.home;
}
} catch (e) {
if (kDebugMode) {
print("Error fetching didvan voices for notification: $e");