Advertisement
mrblab24

bookmark.dart

Aug 6th, 2022
1,337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.78 KB | None | 0 0
  1. import 'package:cloud_firestore/cloud_firestore.dart';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:news_app/models/article.dart';
  4. import 'package:shared_preferences/shared_preferences.dart';
  5.  
  6. class BookmarkBloc extends ChangeNotifier {
  7.  
  8.  
  9.   Future<List> getArticles() async {
  10.  
  11.     String _collectionName = 'contents';
  12.     String _fieldName = 'bookmarked items';
  13.  
  14.     SharedPreferences sp = await SharedPreferences.getInstance();
  15.     String? _uid = sp.getString('uid');
  16.    
  17.  
  18.     final DocumentReference ref = FirebaseFirestore.instance.collection('users').doc(_uid);
  19.     DocumentSnapshot snap = await ref.get();
  20.     List bookmarkedList = snap[_fieldName];
  21.     print('mainList: $bookmarkedList');
  22.  
  23.     List d = [];
  24.     if(bookmarkedList.isEmpty){
  25.       return d;
  26.     }else if(bookmarkedList.length <= 10){
  27.       await FirebaseFirestore.instance
  28.         .collection(_collectionName)
  29.         .where('timestamp', whereIn: bookmarkedList)
  30.         .get()
  31.         .then((QuerySnapshot snap) {
  32.           d.addAll(snap.docs.map((e) => Article.fromFirestore(e)).toList());
  33.       });
  34.  
  35.     }else if(bookmarkedList.length > 10){
  36.       int size = 10;
  37.       var chunks = [];
  38.  
  39.       for(var i = 0; i< bookmarkedList.length; i+= size){    
  40.         var end = (i+size<bookmarkedList.length)?i+size:bookmarkedList.length;
  41.         chunks.add(bookmarkedList.sublist(i,end));
  42.       }
  43.  
  44.       await FirebaseFirestore.instance
  45.         .collection(_collectionName)
  46.         .where('timestamp', whereIn: chunks[0])
  47.         .get()
  48.         .then((QuerySnapshot snap) {
  49.           d.addAll(snap.docs.map((e) => Article.fromFirestore(e)).toList());
  50.       }).then((value)async{
  51.         await FirebaseFirestore.instance
  52.         .collection(_collectionName)
  53.         .where('timestamp', whereIn: chunks[1])
  54.         .get()
  55.         .then((QuerySnapshot snap) {
  56.           d.addAll(snap.docs.map((e) => Article.fromFirestore(e)).toList());
  57.         });
  58.       });
  59.  
  60.     }else if(bookmarkedList.length > 20){
  61.       int size = 10;
  62.       var chunks = [];
  63.  
  64.       for(var i = 0; i< bookmarkedList.length; i+= size){    
  65.         var end = (i+size<bookmarkedList.length)?i+size:bookmarkedList.length;
  66.         chunks.add(bookmarkedList.sublist(i,end));
  67.       }
  68.  
  69.       await FirebaseFirestore.instance
  70.         .collection(_collectionName)
  71.         .where('timestamp', whereIn: chunks[0])
  72.         .get()
  73.         .then((QuerySnapshot snap) {
  74.           d.addAll(snap.docs.map((e) => Article.fromFirestore(e)).toList());
  75.       }).then((value)async{
  76.         await FirebaseFirestore.instance
  77.         .collection(_collectionName)
  78.         .where('timestamp', whereIn: chunks[1])
  79.         .get()
  80.         .then((QuerySnapshot snap) {
  81.           d.addAll(snap.docs.map((e) => Article.fromFirestore(e)).toList());
  82.         });
  83.       }).then((value)async{
  84.         await FirebaseFirestore.instance
  85.         .collection(_collectionName)
  86.         .where('timestamp', whereIn: chunks[2])
  87.         .get()
  88.         .then((QuerySnapshot snap) {
  89.           d.addAll(snap.docs.map((e) => Article.fromFirestore(e)).toList());
  90.         });
  91.       });
  92.     }
  93.  
  94.    
  95.     return d;
  96.  
  97.   }
  98.  
  99.  
  100.  
  101.  
  102.   Future onBookmarkIconClick(String? timestamp) async {
  103.     final SharedPreferences sp = await SharedPreferences.getInstance();
  104.     String? _uid = sp.getString('uid');
  105.     String _fieldName = 'bookmarked items';
  106.    
  107.     final DocumentReference ref = FirebaseFirestore.instance.collection('users').doc(_uid);
  108.     DocumentSnapshot snap = await ref.get();
  109.     List d = snap[_fieldName];
  110.    
  111.  
  112.     if (d.contains(timestamp)) {
  113.  
  114.       List a = [timestamp];
  115.       await ref.update({_fieldName: FieldValue.arrayRemove(a)});
  116.      
  117.  
  118.     } else {
  119.  
  120.       d.add(timestamp);
  121.       await ref.update({_fieldName: FieldValue.arrayUnion(d)});
  122.      
  123.      
  124.     }
  125.  
  126.     notifyListeners();
  127.   }
  128.  
  129.  
  130.  
  131.  
  132.  
  133.   Future onLoveIconClick(String? timestamp) async {
  134.     final SharedPreferences sp = await SharedPreferences.getInstance();
  135.     final String _collectionName = 'contents';
  136.     String? _uid = sp.getString('uid');
  137.     String _fieldName = 'loved items';
  138.  
  139.  
  140.     final DocumentReference ref = FirebaseFirestore.instance.collection('users').doc(_uid);
  141.     final DocumentReference ref1 = FirebaseFirestore.instance.collection(_collectionName).doc(timestamp);
  142.  
  143.     DocumentSnapshot snap = await ref.get();
  144.     DocumentSnapshot snap1 = await ref1.get();
  145.     List d = snap[_fieldName];
  146.     int? _loves = snap1['loves'];
  147.  
  148.     if (d.contains(timestamp)) {
  149.  
  150.       List a = [timestamp];
  151.       await ref.update({_fieldName: FieldValue.arrayRemove(a)});
  152.       ref1.update({'loves': _loves! - 1});
  153.  
  154.     } else {
  155.  
  156.       d.add(timestamp);
  157.       await ref.update({_fieldName: FieldValue.arrayUnion(d)});
  158.       ref1.update({'loves': _loves! + 1});
  159.  
  160.     }
  161.   }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement