|
47 | 47 | import open.furaffinity.client.adapter.savedSearchListAdapter; |
48 | 48 | import open.furaffinity.client.dialogs.textDialog; |
49 | 49 | import open.furaffinity.client.listener.EndlessRecyclerViewScrollListener; |
| 50 | +import open.furaffinity.client.pages.adRetrieval; |
50 | 51 | import open.furaffinity.client.pages.loginCheck; |
51 | 52 | import open.furaffinity.client.sqlite.searchContract.searchItemEntry; |
52 | 53 | import open.furaffinity.client.sqlite.searchDBHelper; |
@@ -513,65 +514,93 @@ public void requestSucceeded(abstractPage abstractPage) { |
513 | 514 | resetRecycler(); |
514 | 515 | } |
515 | 516 | } else { |
516 | | - List<HashMap<String, String>> pageResults = ((open.furaffinity.client.pages.search) abstractPage).getPageResults(); |
517 | | - |
518 | | - int curSize = mAdapter.getItemCount(); |
519 | | - |
520 | | - //Deduplicate results |
521 | | - List<String> newPostPaths = pageResults.stream().map(currentMap -> currentMap.get("postPath")).collect(Collectors.toList()); |
522 | | - List<String> oldPostPaths = mDataSet.stream().map(currentMap -> currentMap.get("postPath")).collect(Collectors.toList()); |
523 | | - newPostPaths.removeAll(oldPostPaths); |
524 | | - pageResults = pageResults.stream().filter(currentMap -> newPostPaths.contains(currentMap.get("postPath"))).collect(Collectors.toList()); |
525 | | - mDataSet.addAll(pageResults); |
526 | | - mAdapter.notifyItemRangeInserted(curSize, mDataSet.size()); |
527 | | - |
528 | | - if (((open.furaffinity.client.pages.search) abstractPage).getPageResults() != null && ((open.furaffinity.client.pages.search) abstractPage).getPageResults().size() > 0 && ((open.furaffinity.client.pages.search) abstractPage).getCurrentPage().equals("1")) { |
529 | | - //Find any saved searches that meet the current search criteria and apply the most recent link to them |
530 | | - searchDBHelper dbHelper = new searchDBHelper(getActivity()); |
531 | | - SQLiteDatabase db = dbHelper.getWritableDatabase(); |
532 | | - |
533 | | - ContentValues values = new ContentValues(); |
534 | | - values.put(searchItemEntry.COLUMN_NAME_MOSTRECENTITEM, ((open.furaffinity.client.pages.search) abstractPage).getPageResults().get(0).get("postPath")); |
535 | | - |
536 | | - String selection = ""; |
537 | | - selection += searchItemEntry.COLUMN_NAME_Q + " = ? AND "; |
538 | | - selection += searchItemEntry.COLUMN_NAME_ORDERBY + " = ? AND "; |
539 | | - selection += searchItemEntry.COLUMN_NAME_ORDERDIRECTION + " = ? AND "; |
540 | | - selection += searchItemEntry.COLUMN_NAME_RANGE + " = ? AND "; |
541 | | - selection += searchItemEntry.COLUMN_NAME_RATINGGENERAL + " = ? AND "; |
542 | | - selection += searchItemEntry.COLUMN_NAME_RATINGMATURE + " = ? AND "; |
543 | | - selection += searchItemEntry.COLUMN_NAME_RATINGADULT + " = ? AND "; |
544 | | - selection += searchItemEntry.COLUMN_NAME_TYPEART + " = ? AND "; |
545 | | - selection += searchItemEntry.COLUMN_NAME_TYPEMUSIC + " = ? AND "; |
546 | | - selection += searchItemEntry.COLUMN_NAME_TYPEFLASH + " = ? AND "; |
547 | | - selection += searchItemEntry.COLUMN_NAME_TYPESTORY + " = ? AND "; |
548 | | - selection += searchItemEntry.COLUMN_NAME_TYPEPHOTO + " = ? AND "; |
549 | | - selection += searchItemEntry.COLUMN_NAME_TYPEPOETRY + " = ? AND "; |
550 | | - selection += searchItemEntry.COLUMN_NAME_MODE + " = ? "; |
551 | | - |
552 | | - String[] selectionArgs = { |
553 | | - ((open.furaffinity.client.pages.search) abstractPage).getCurrentQuery(), |
554 | | - ((open.furaffinity.client.pages.search) abstractPage).getCurrentOrderBy(), |
555 | | - ((open.furaffinity.client.pages.search) abstractPage).getCurrentOrderDirection(), |
556 | | - ((open.furaffinity.client.pages.search) abstractPage).getCurrentRange(), |
557 | | - ((((open.furaffinity.client.pages.search) abstractPage).getCurrentRatingGeneral().equals("")) ? ("0") : ("1")), |
558 | | - ((((open.furaffinity.client.pages.search) abstractPage).getCurrentRatingMature().equals("")) ? ("0") : ("1")), |
559 | | - ((((open.furaffinity.client.pages.search) abstractPage).getCurrentRatingAdult().equals("")) ? ("0") : ("1")), |
560 | | - ((((open.furaffinity.client.pages.search) abstractPage).getCurrentTypeArt().equals("")) ? ("0") : ("1")), |
561 | | - ((((open.furaffinity.client.pages.search) abstractPage).getCurrentTypeMusic().equals("")) ? ("0") : ("1")), |
562 | | - ((((open.furaffinity.client.pages.search) abstractPage).getCurrentTypeFlash().equals("")) ? ("0") : ("1")), |
563 | | - ((((open.furaffinity.client.pages.search) abstractPage).getCurrentTypeStory().equals("")) ? ("0") : ("1")), |
564 | | - ((((open.furaffinity.client.pages.search) abstractPage).getCurrentTypePhoto().equals("")) ? ("0") : ("1")), |
565 | | - ((((open.furaffinity.client.pages.search) abstractPage).getCurrentTypePoetry().equals("")) ? ("0") : ("1")), |
566 | | - ((open.furaffinity.client.pages.search) abstractPage).getCurrentMode() |
567 | | - }; |
568 | | - |
569 | | - db.update(searchItemEntry.TABLE_NAME, values, selection, selectionArgs); |
570 | | - db.close(); |
571 | | - } |
| 517 | + final open.furaffinity.client.pages.search searchPage = ((open.furaffinity.client.pages.search) abstractPage); |
| 518 | + |
| 519 | + new open.furaffinity.client.pages.adRetrieval(getActivity(), new abstractPage.pageListener() { |
| 520 | + @Override |
| 521 | + public void requestSucceeded(open.furaffinity.client.abstractClasses.abstractPage abstractPage) { |
| 522 | + List<HashMap<String, String>> pageResults = searchPage.getPageResults(); |
| 523 | + List<HashMap<String, String>> adResults = ((open.furaffinity.client.pages.adRetrieval) abstractPage).getAdData(); |
| 524 | + |
| 525 | + int curSize = mAdapter.getItemCount(); |
| 526 | + |
| 527 | + //Deduplicate results |
| 528 | + List<String> newPostPaths = pageResults.stream().map(currentMap -> currentMap.get("postPath")).collect(Collectors.toList()); |
| 529 | + List<String> oldPostPaths = mDataSet.stream().map(currentMap -> currentMap.get("postPath")).collect(Collectors.toList()); |
| 530 | + newPostPaths.removeAll(oldPostPaths); |
| 531 | + pageResults = pageResults.stream().filter(currentMap -> newPostPaths.contains(currentMap.get("postPath"))).collect(Collectors.toList()); |
| 532 | + |
| 533 | + if(!adResults.isEmpty()){ |
| 534 | + int totalAddItems = pageResults.size() + adResults.size(); |
| 535 | + int spacing = totalAddItems/adResults.size(); |
| 536 | + |
| 537 | + for(int i = 0; i < adResults.size(); i++) { |
| 538 | + int addPosition = (spacing * i) + i; |
| 539 | + if(addPosition <= pageResults.size()) { |
| 540 | + pageResults.add(addPosition, adResults.get(i)); |
| 541 | + } |
| 542 | + } |
| 543 | + } |
| 544 | + |
| 545 | + mDataSet.addAll(pageResults); |
| 546 | + mAdapter.notifyItemRangeInserted(curSize, mDataSet.size()); |
| 547 | + |
| 548 | + if (searchPage.getPageResults() != null && searchPage.getPageResults().size() > 0 && searchPage.getCurrentPage().equals("1")) { |
| 549 | + //Find any saved searches that meet the current search criteria and apply the most recent link to them |
| 550 | + searchDBHelper dbHelper = new searchDBHelper(getActivity()); |
| 551 | + SQLiteDatabase db = dbHelper.getWritableDatabase(); |
| 552 | + |
| 553 | + ContentValues values = new ContentValues(); |
| 554 | + values.put(searchItemEntry.COLUMN_NAME_MOSTRECENTITEM, searchPage.getPageResults().get(0).get("postPath")); |
| 555 | + |
| 556 | + String selection = ""; |
| 557 | + selection += searchItemEntry.COLUMN_NAME_Q + " = ? AND "; |
| 558 | + selection += searchItemEntry.COLUMN_NAME_ORDERBY + " = ? AND "; |
| 559 | + selection += searchItemEntry.COLUMN_NAME_ORDERDIRECTION + " = ? AND "; |
| 560 | + selection += searchItemEntry.COLUMN_NAME_RANGE + " = ? AND "; |
| 561 | + selection += searchItemEntry.COLUMN_NAME_RATINGGENERAL + " = ? AND "; |
| 562 | + selection += searchItemEntry.COLUMN_NAME_RATINGMATURE + " = ? AND "; |
| 563 | + selection += searchItemEntry.COLUMN_NAME_RATINGADULT + " = ? AND "; |
| 564 | + selection += searchItemEntry.COLUMN_NAME_TYPEART + " = ? AND "; |
| 565 | + selection += searchItemEntry.COLUMN_NAME_TYPEMUSIC + " = ? AND "; |
| 566 | + selection += searchItemEntry.COLUMN_NAME_TYPEFLASH + " = ? AND "; |
| 567 | + selection += searchItemEntry.COLUMN_NAME_TYPESTORY + " = ? AND "; |
| 568 | + selection += searchItemEntry.COLUMN_NAME_TYPEPHOTO + " = ? AND "; |
| 569 | + selection += searchItemEntry.COLUMN_NAME_TYPEPOETRY + " = ? AND "; |
| 570 | + selection += searchItemEntry.COLUMN_NAME_MODE + " = ? "; |
| 571 | + |
| 572 | + String[] selectionArgs = { |
| 573 | + searchPage.getCurrentQuery(), |
| 574 | + searchPage.getCurrentOrderBy(), |
| 575 | + searchPage.getCurrentOrderDirection(), |
| 576 | + searchPage.getCurrentRange(), |
| 577 | + ((searchPage.getCurrentRatingGeneral().equals("")) ? ("0") : ("1")), |
| 578 | + ((searchPage.getCurrentRatingMature().equals("")) ? ("0") : ("1")), |
| 579 | + ((searchPage.getCurrentRatingAdult().equals("")) ? ("0") : ("1")), |
| 580 | + ((searchPage.getCurrentTypeArt().equals("")) ? ("0") : ("1")), |
| 581 | + ((searchPage.getCurrentTypeMusic().equals("")) ? ("0") : ("1")), |
| 582 | + ((searchPage.getCurrentTypeFlash().equals("")) ? ("0") : ("1")), |
| 583 | + ((searchPage.getCurrentTypeStory().equals("")) ? ("0") : ("1")), |
| 584 | + ((searchPage.getCurrentTypePhoto().equals("")) ? ("0") : ("1")), |
| 585 | + ((searchPage.getCurrentTypePoetry().equals("")) ? ("0") : ("1")), |
| 586 | + searchPage.getCurrentMode() |
| 587 | + }; |
| 588 | + |
| 589 | + db.update(searchItemEntry.TABLE_NAME, values, selection, selectionArgs); |
| 590 | + db.close(); |
| 591 | + } |
| 592 | + |
| 593 | + isLoading = false; |
| 594 | + swipeRefreshLayout.setRefreshing(false); |
| 595 | + } |
572 | 596 |
|
573 | | - isLoading = false; |
574 | | - swipeRefreshLayout.setRefreshing(false); |
| 597 | + @Override |
| 598 | + public void requestFailed(open.furaffinity.client.abstractClasses.abstractPage abstractPage) { |
| 599 | + isLoading = false; |
| 600 | + swipeRefreshLayout.setRefreshing(false); |
| 601 | + Toast.makeText(getActivity(), "Failed to load ad data", Toast.LENGTH_SHORT).show(); |
| 602 | + } |
| 603 | + }, searchPage.getAdZones()).execute(); |
575 | 604 | } |
576 | 605 | } |
577 | 606 |
|
|
0 commit comments