import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../models/annauncement_model.dart';
import 'annaucement_service.dart';

class AnnouncementController extends GetxController {
  final AnnouncementService _announcementService = AnnouncementService();
  var announcements = <AnnouncementModel>[].obs;
  var isLoading = true.obs;
  var errorMessage = ''.obs;

  Future<void> fetchAnnouncements() async {
    isLoading.value = true;
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String? token = prefs.getString('token');
      final List<AnnouncementModel>? fetchedAnnouncements =
      await _announcementService.fetchAnnouncements(token!);
      announcements.assignAll(fetchedAnnouncements as Iterable<AnnouncementModel>);
    } catch (e) {
      errorMessage.value = e.toString();
    } finally {
      isLoading.value = false;
    }
  }
}
