mrhumbility

Series Grid View Logic

Jul 22nd, 2021
1,045
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     AppManager appManager = Provider.of<AppManager>(context, listen: true);
  2.     List<dynamic> books = [];
  3.     List<Book> bbooks = [];
  4.     appManager = Provider.of<AppManager>(context, listen: false);
  5.     if (appManager.selectedAuthor == null) {
  6.       bbooks = appManager.books;
  7.       bbooks.sort((a, b) => (a.title.startsWith('The ')
  8.               ? a.title.replaceFirst('The ', '').toLowerCase()
  9.               : a.title.toLowerCase())
  10.           .compareTo(
  11.               (b.title.startsWith('The ') ? b.title.replaceFirst('The ', '').toLowerCase() : b.title.toLowerCase())));
  12.       books.add(bbooks);
  13.     } else if (appManager.libraryBooksViewType == 'Series View') {
  14.       List<Book> authorBooks = [
  15.         for (Book b in appManager.books)
  16.           if (b.author.name == appManager.selectedAuthor.name) b
  17.       ].toList();
  18.       // this seems to reorder them into alphabetical order by series. Thats the goal at least.
  19.       //authorBooks.sort((a, b) => a.series == null ? 0 : a.series.compareTo(b.series == null ? 1 : b.series));
  20.       authorBooks.sort((a, b) => a.getSeries.compareTo(b.getSeries));
  21.       for (Book b in authorBooks) {
  22.         if (b.series != null) {
  23.           bool found = false;
  24.           for (var entry in books) {
  25.             if (entry is Series && entry.name == b.series) {
  26.               found = true;
  27.               if (b.subseries != null) {
  28.                 if (entry.subseries.length > 0) {
  29.                   bool foundmore = false;
  30.                   for (Subseries subseries in entry.subseries) {
  31.                     if (subseries.name == b.subseries) {
  32.                       foundmore = true;
  33.                       subseries.books.add(b);
  34.                     }
  35.                   }
  36.                   if (foundmore == false) {
  37.                     Subseries sub = Subseries();
  38.                     sub.name = b.subseries;
  39.                     sub.books.add(b);
  40.                     entry.subseries.add(sub);
  41.                   }
  42.                 } else {
  43.                   Subseries sub = Subseries();
  44.                   sub.name = b.subseries;
  45.                   sub.books.add(b);
  46.                   entry.subseries.add(sub);
  47.                 }
  48.               } else {
  49.                 entry.books.add(b);
  50.               }
  51.             }
  52.           }
  53.           if (found == false) {
  54.             Series s = Series();
  55.             s.name = b.series;
  56.             if (b.subseries != null) {
  57.               Subseries sub = Subseries();
  58.               sub.name = b.subseries;
  59.               sub.books.add(b);
  60.               s.subseries.add(sub);
  61.             } else {
  62.               s.books.add(b);
  63.             }
  64.             books.add(s);
  65.           }
  66.         } else {
  67.           bbooks.add(b);
  68.         }
  69.       }
  70.       if (bbooks.length > 0) {
  71.         books.insert(0, bbooks);
  72.       }
  73.     } else {
  74.       List<Book> bbooks = [];
  75.       for (Book b in appManager.books) {
  76.         if (b.author.name == appManager.selectedAuthor.name) {
  77.           bbooks.add(b);
  78.         }
  79.       }
  80.       bbooks.sort((a, b) => (a.title.startsWith('The ')
  81.               ? a.title.replaceFirst('The ', '').toLowerCase()
  82.               : a.title.toLowerCase())
  83.           .compareTo(
  84.               (b.title.startsWith('The ') ? b.title.replaceFirst('The ', '').toLowerCase() : b.title.toLowerCase())));
  85.       books.add(bbooks);
  86.     }
RAW Paste Data