import 'dart:async'; import 'dart:math'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:hive_flutter/hive_flutter.dart'; import 'package:konectar_events/contacts_module/model_class/k2_api_model/kol_event_k2.dart'; import 'package:konectar_events/model/affiliationsmodel.dart'; import 'package:konectar_events/model/allsessionnotesmodel.dart'; import 'package:konectar_events/model/events_details.dart'; import 'package:konectar_events/model/events_list_resp_2.dart'; import 'package:konectar_events/model/events_speakers_k1.dart'; import 'package:konectar_events/model/eventsdetailmodel.dart'; import 'package:konectar_events/model/eventsmodel.dart'; import 'package:konectar_events/model/eventsoverview.dart'; import 'package:konectar_events/model/eventspeakers.dart'; import 'package:konectar_events/model/keywords_model.dart'; import 'package:konectar_events/model/my_events_list_model.dart'; import 'package:konectar_events/model/neweventsmodel.dart'; import 'package:konectar_events/model/scope_model.dart'; import 'package:konectar_events/model/sessionnotesmodel.dart'; import 'package:konectar_events/model/specialtymodel.dart'; import 'package:konectar_events/model/topics_cloud_model.dart'; import 'package:konectar_events/utils/apicall.dart'; import 'package:konectar_events/utils/dateformater.dart'; import 'package:konectar_events/widgets/word_cloud.dart'; class EventsProvider extends ChangeNotifier { List keywordList = []; List therapeuticList = []; List eventList = []; List neweventList = []; List searchList = []; List myEvents = []; List offlineEvents = []; List eventdetailList = []; List srcheventdetailList = []; List selectedTherapeutic = []; List addedSessionNotes = []; List speakersList = []; List myEventids = []; bool showCheckbox = false; bool offlineExists = false; int eventSessionCount = 0; String message = ""; int speakercount = 0; late Box box; // late Box myeventsbox; late Box sessionBox; bool isAllSelected = true; bool isFavSeleted = true; bool isSearch = false; bool isSearchSpeakers = false; List kFlutterHashtags = []; List topics = []; List specialtyList = []; EventsOverviewData overviewData = EventsOverviewData( arrEvents: [], topSpeakers: [], eventSponsers: [], eventTopics: []); Affiliations affiliations = Affiliations(affiliationCount: [], affiliationNames: []); List allSessionNotes = []; late StreamSubscription> connectivitySubscription; bool isLoadingInsights = true; Future onSelectAll(int page) async { isFavSeleted = false; isAllSelected = !isAllSelected; eventList = await getEvents(page); notifyListeners(); } Future onSelectMy() async { isFavSeleted = !isFavSeleted; eventList = []; eventList = await getMyEvents(1); notifyListeners(); } bool checkIfUserInterested(String eventid) { bool user = false; if (eventList.isNotEmpty) { int index = eventList.indexWhere( (element) => element.eventId == eventid, ); print("FOR EVENT ID :$eventid : ${eventList[index].eventUserInterest}"); user = eventList[index].eventUserInterest ?? false; } return user; } List OnSearch(String searchtxt) { print("Searching......."); // // searchtxt = searchtxt.toLowerCase(); // isSearch = true; // if (isFavSeleted) { // searchList = eventList // .where((element) => element.name1!.toLowerCase().contains(searchtxt)) // // (element.city != "" && // // element.city!.toLowerCase().contains(searchtxt)) || // // (element.region != "" && // // element.region!.toLowerCase().contains(searchtxt)) || // // (element.country != "" && // // element.country!.toLowerCase().contains(searchtxt))) // .toList(); // } else { // eventList = // await ApiCall().getSearchedEventsFromK2(1, "event_name", searchtxt); // // searchList = eventList // // .where((element) => element.name1!.toLowerCase().contains(searchtxt)) // // // element.city!.toLowerCase().contains(searchtxt) || // // // element.region!.toLowerCase().contains(searchtxt) || // // // element.country!.toLowerCase().contains(searchtxt)) // // .toList(); // } List searchList = eventList ..where((element) => element.name1!.toLowerCase().contains(searchtxt)); return searchList; // print("###$searchList@@@"); // notifyListeners(); } bool ifOfflineExists(String eventid) { if (offlineEvents.isNotEmpty) { offlineEvents.forEach( (element) { if (element.eventId == eventid) { offlineExists = true; } else { offlineExists = false; } }, ); } return offlineExists; } getSessionCount() { List sessions = []; eventSessionCount = 0; for (var obj in speakersList) { sessions.addAll(obj.sessionName!.split(',')); } print("Count ${sessions.toSet().toList().length}"); print("Count2 ${sessions.toList().length}"); print("SEE SESSIONS ${sessions.toSet().toList()}"); eventSessionCount = sessions.toSet().toList().length; notifyListeners(); } OnFilters() { isSearch = true; if (isFavSeleted) { searchList = myEvents .where((element) => selectedTherapeutic.contains(element.therapeuticArea)) .toList(); } else { searchList = myEvents .where((element) => selectedTherapeutic.contains(element.therapeuticArea)) .toList(); } print("###$searchList@@@"); notifyListeners(); } onFilterReset() async { if (isFavSeleted) { myEvents = await getMyEvents(1); } else { eventList = await getEvents(1); } isSearch = false; searchList.clear(); notifyListeners(); } onSearchReset() async { if (isFavSeleted) { myEvents = await getMyEvents(1); } else { eventList = await getEvents(1); } isSearch = false; searchList.clear(); notifyListeners(); } OnSearchSpeakers(String searchtxt) { searchtxt = searchtxt.toLowerCase(); isSearchSpeakers = true; srcheventdetailList = eventdetailList .where((element) => element.kolFullName!.toLowerCase().contains(searchtxt) || element.eventTopics!.toLowerCase().contains(searchtxt)) .toList(); notifyListeners(); } onSearchSpeakersReset(String eventid) async { eventdetailList = await getEventsDetails(eventid); isSearchSpeakers = false; srcheventdetailList.clear(); notifyListeners(); } initFiltersData() async { // keywordList = await ApiCall().getkeywords(''); // therapeuticList = await ApiCall().getscopes(); notifyListeners(); } Future> getKeywordList(String keyword) async { keywordList = await ApiCall().getkeywords(keyword); return keywordList; //notifyListeners(); } Future> getEvents(int page, {String? searchtxt, String? startdate, String? enddate}) async { // eventList = await getAllEventsData(); eventList.clear(); eventList = []; offlineEvents = await getOfflineMyEvents(); eventList = isFavSeleted ? await ApiCall().getEventsFromK1(type: 2) : await ApiCall().getEventsFromK1(); print("see the list : $neweventList"); if (eventList.isEmpty) { // eventList = (await ApiCall().getEvents())!; eventList = isFavSeleted ? await ApiCall().getEventsFromK1(type: 2) : await ApiCall().getEventsFromK1(); //eventList = (await ApiCall().getStagingEvents())!; for (var events in eventList) { await saveEventsData(events); } } if (searchtxt != "") { return OnSearch(searchtxt!); } //add to favs old if (myEventids.isNotEmpty) { for (var events in eventList) { if (myEventids.contains(events.eventId)) { events.isfav = true; } } } return eventList; //notifyListeners(); } Future> getMyEvents(int page) async { // eventList = await getAllEventsData(); myEvents = await ApiCall().getEventsFromK1(type: 2); print("see the list : $myEvents"); if (myEvents.isEmpty) { // eventList = (await ApiCall().getEvents())!; myEvents = await ApiCall().getEventsFromK1(type: 2); //eventList = (await ApiCall().getStagingEvents())!; } for (var events in myEvents) { myEventids.add(events.eventId!); } return myEvents; //notifyListeners(); } Future> getAddedSessionNotes() async { addedSessionNotes = await getAllSessionNotesList(); if (addedSessionNotes.isEmpty && eventList.isNotEmpty) { addedSessionNotes = (await ApiCall().getSessionNotes())!; for (SessionNotesModel notes in addedSessionNotes) { int i = eventList.indexWhere((element) => element.name1 == notes.eventname); if (i != -1) { notes.eventid = eventList[i].eventId; } notes.addedBy = "user"; notes.addedDate = CustomDateFormatter().formatDate(); await saveSessionDataFromResp(notes); } } return addedSessionNotes; //notifyListeners(); } Future> getEventsDetails(String eventid) async { eventdetailList = (await ApiCall().getLocalEventsDetail("142682"))!; return eventdetailList; //notifyListeners(); } Future> getInitialSpeakersDetails(String eventid, {required String searchkey}) async { speakersList = await ApiCall().getEventsSpeakersFromK1(eventid); getSessionCount(); if (searchkey.length != 0) { List searchspeakers = speakersList .where((element) => element.kolFullName!.toLowerCase().contains(searchkey)) .toList(); if (searchspeakers.isEmpty) { searchspeakers = speakersList .where((element) => element.eventTopics!.toLowerCase().contains(searchkey)) .toList(); } return searchspeakers; } else { return await ApiCall().getEventsSpeakersFromK1(eventid); } //return speakersList = (await ApiCall().getEventsSpeakersFromK1(eventid)); // notifyListeners(); } Future> getSpeakersDetails(int page, String eventid, String event_unique_id, String searchkey, int lastIndex) async { // speakercount = data["count"]; speakersList = (await ApiCall().getEventsSpeakersFromK1(eventid)); // getSessionCount(); // if (speakersList.isEmpty) { // // } if (page == 0) { speakersList = speakersList.getRange(0, lastIndex).toList(); } else if (page > 0) { int nextIndex = lastIndex + 40; if (nextIndex < speakersList.length) { speakersList = speakersList.getRange(lastIndex, nextIndex).toList(); } else { speakersList = speakersList.getRange(lastIndex, speakersList.length - 1).toList(); } } // if (searchkey.length != 0) { // List searchspeakers = speakersList // .where((element) => // element.kolFullName!.toLowerCase().contains(searchkey)) // .toList(); // return searchspeakers; // } else { return speakersList; // } //notifyListeners(); } Future addEventsToFavs(String eventid) async { message = await ApiCall().followUnfollowEvent("1", eventid); if (myEventids.isNotEmpty) { if (!myEventids.contains(eventid)) { myEventids.add(eventid); } } else { myEventids.add(eventid); } return message; //notifyListeners(); } Future attendNotAttendEvent(String eventid, String flag) async { message = await ApiCall().attendNotAttendEvent(flag, eventid); return message; //notifyListeners(); } Future removeEventsToFavs(String eventid) async { message = await ApiCall().followUnfollowEvent("0", eventid); if (myEventids.isNotEmpty) { if (myEventids.contains(eventid)) { myEventids.remove(eventid); } } return message; //notifyListeners(); } Future markAttending(String eventid) async { message = await ApiCall().attendEvents(eventid); return message; //notifyListeners(); } FutureOr saveEventsData(EventsList eventsData) async { box = await Hive.openBox('EventsListBox'); box.add(eventsData); offlineEvents.clear(); offlineEvents = await getOfflineMyEvents(); print("OFFLINE TOTAL :${offlineEvents.length}"); notifyListeners(); } FutureOr favsEventsData(EventsList eventsData) async { box = await Hive.openBox('EventsListBox'); List data = box.values.toList(); int index = data.indexWhere( (element) => element.eventId == eventsData.eventId, ); box.putAt(index, eventsData); // List eventsData2 = await getMyEventsData(); notifyListeners(); } Future> getOfflineMyEvents() async { box = await Hive.openBox('EventsListBox'); Iterable data = box.values; myEvents = data.toList(); // EventsList eventsdata = EventsList(); // for (var obj in data) { // eventsdata = new EventsList.fromJson(obj.toJson()); // } return myEvents; } Future> getAllSessionNotesList() async { sessionBox = await Hive.openBox('SessionNotesModelBox'); Iterable data = sessionBox.values; addedSessionNotes = data.toList(); // EventsList eventsdata = EventsList(); // for (var obj in data) { // eventsdata = new EventsList.fromJson(obj.toJson()); // } return addedSessionNotes; } FutureOr deleteAllEventsData() async { box = await Hive.openBox('EventsListBox'); box.clear(); } FutureOr delateOfflineEvent(EventsList event) async { //DELETE // box = await Hive.openBox('EventsListBox'); // List data = box.values.toList(); // int index = data.indexWhere( // (element) => element.eventId == eventid, // ); // await box.deleteAt(index); //REMOVE FROM FAVS box = await Hive.openBox('EventsListBox'); List data = box.values.toList(); int index = data.indexWhere( (element) => element.eventId == event.eventId, ); event.isfav = false; // box.putAt(index, event); box.deleteAt(index); //eventList = await getMyEventsData(); offlineEvents.clear(); offlineEvents = await getOfflineMyEvents(); notifyListeners(); } FutureOr saveSessionDataFromResp(SessionNotesModel notesModel) async { sessionBox = await Hive.openBox('SessionNotesModelBox'); sessionBox.add(notesModel); notifyListeners(); } FutureOr getTopicsCloud(EventsList event, {bool? client}) async { List topics = await ApiCall().insightsTopicsCloud( eventid: event.eventId!, startDate: CustomDateFormatter().formatYearDate( CustomDateFormatter().convertStringToDate(event.start!)), endDate: CustomDateFormatter().formatYearDate( CustomDateFormatter().convertStringToDate(event.end!)), client: client ?? false); // for (var events in eventList) { // await saveEventsData(events); // } //notifyListeners(); kFlutterHashtags = []; for (var topic in topics) { kFlutterHashtags.add( FlutterHashtag(topic.topic!, int.parse(topic.total!) + 500, false)); } notifyListeners(); } FutureOr getSpecialtyData(EventsList event, {bool? client}) async { String date = CustomDateFormatter().formatYearDate( CustomDateFormatter().convertStringToDate(event.start!)); print("Formatted_DATE :$date"); specialtyList = await ApiCall().specialtyOfSpeakers( eventid: event.eventId!, startDate: CustomDateFormatter().formatYearDate( CustomDateFormatter().convertStringToDate(event.start!)), endDate: CustomDateFormatter().formatYearDate( CustomDateFormatter().convertStringToDate(event.end!)), client: client ?? false); //isLoadingInsights = false; notifyListeners(); } FutureOr getAffiliations(EventsList event, {bool? client}) async { // isLoadingInsights = true; affiliations = await ApiCall().insightsBarChart( eventid: event.eventId!, startDate: CustomDateFormatter().formatYearDate( CustomDateFormatter().convertStringToDate(event.start!)), endDate: CustomDateFormatter().formatYearDate( CustomDateFormatter().convertStringToDate(event.end!)), client: client ?? false); //isLoadingInsights = false; notifyListeners(); } FutureOr getOverviewData( String eventid, String startDate, String endDate) async { overviewData = (await ApiCall().getEventsOverview(eventid, startDate, endDate)); notifyListeners(); } FutureOr getAllSessionNotesFromApi(String eid) async { print("Check hcp id"); // print("$eid,$hcpid"); allSessionNotes = await ApiCall().getAllSessionNotes(eid); notifyListeners(); } FutureOr callInsightsDataForClientSpecific( EventsList event, bool client) async { await getTopicsCloud(event, client: client); await getSpecialtyData(event, client: client); await getAffiliations(event, client: client); isLoadingInsights = false; } //INTERNET CONNECTIVITY }