@@ -17,6 +17,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
17
17
import com.google.common.util.concurrent.MoreExecutors
18
18
import io.reactivex.disposables.Disposable
19
19
import org.joda.time.DateTime
20
+ import org.joda.time.format.DateTimeFormat
20
21
import org.librarysimplified.ui.R
21
22
import org.nypl.simplified.books.api.Book
22
23
import org.nypl.simplified.books.api.BookFormat
@@ -64,9 +65,12 @@ class CatalogFeedPagingDataAdapter(
64
65
private val onBookViewerOpen : (Book , BookFormat ) -> Unit ,
65
66
private val onBookDelete : (CatalogBookStatus <* >) -> Unit ,
66
67
private val onShowTaskError : (TaskResult .Failure <* >) -> Unit ,
67
- ) : PagingDataAdapter<FeedEntry, CatalogFeedPagingDataAdapter.ViewHolder>(diffCallback) {
68
+ ) : PagingDataAdapter<FeedEntry, CatalogFeedPagingDataAdapter.ViewHolder>(org.nypl.simplified.ui.catalog. CatalogFeedPagingDataAdapter . Companion . diffCallback) {
68
69
69
70
companion object {
71
+ private val loanEndFormatter =
72
+ DateTimeFormat .forPattern(" MMM d, yyyy" )
73
+
70
74
private val diffCallback =
71
75
object : DiffUtil .ItemCallback <FeedEntry >() {
72
76
override fun areContentsTheSame (
@@ -110,8 +114,8 @@ class CatalogFeedPagingDataAdapter(
110
114
this .view.findViewById<ProgressBar >(R .id.bookCellIdleCoverProgress)
111
115
private val idleTitle =
112
116
this .idle.findViewById<TextView >(R .id.bookCellIdleTitle)
113
- private val idleMeta =
114
- this .idle.findViewById<TextView >(R .id.bookCellIdleMeta )
117
+ private val idleTime =
118
+ this .idle.findViewById<TextView >(R .id.bookCellIdleTime )
115
119
private val idleAuthor =
116
120
this .idle.findViewById<TextView >(R .id.bookCellIdleAuthor)
117
121
private val idleButtons =
@@ -182,7 +186,7 @@ class CatalogFeedPagingDataAdapter(
182
186
.filter { event -> event.bookId == item.bookID }
183
187
.subscribe { event -> this .onStatusChangedForFeedEntry(item) }
184
188
185
- for (v in listOf (view, this .idle, this .error, this .corrupt, this .progress)) {
189
+ for (v in listOf (this . view, this .idle, this .error, this .corrupt, this .progress)) {
186
190
v.setOnClickListener { this @CatalogFeedPagingDataAdapter.onBookSelected(item) }
187
191
}
188
192
@@ -192,6 +196,7 @@ class CatalogFeedPagingDataAdapter(
192
196
this .errorTitle.text = item.feedEntry.title
193
197
this .idleTitle.text = item.feedEntry.title
194
198
this .idleAuthor.text = item.feedEntry.authorsCommaSeparated
199
+ this .idleTime.text = " "
195
200
196
201
val f =
197
202
this @CatalogFeedPagingDataAdapter.covers.loadThumbnailInto(
@@ -587,42 +592,30 @@ class CatalogFeedPagingDataAdapter(
587
592
this .setVisible(this .progress, false )
588
593
589
594
this .idleButtons.removeAllViews()
595
+ this .setIdleTime(this .getLoanDuration(book))
590
596
591
597
when (val format = book.findPreferredFormat()) {
592
598
is BookFormat .BookFormatPDF ,
593
599
is BookFormat .BookFormatEPUB -> {
594
- val loanDuration = getLoanDuration(book)
595
600
this .idleButtons.addView(
596
- if (loanDuration.isNotEmpty()) {
597
- this .buttonCreator.createReadButtonWithLoanDuration(loanDuration) {
601
+ this .buttonCreator.createReadButton(
602
+ onClick = {
598
603
this .onBookViewerOpen(book, format)
599
- }
600
- } else {
601
- this .buttonCreator.createReadButton(
602
- onClick = {
603
- this .onBookViewerOpen(book, format)
604
- },
605
- heightMatchParent = true
606
- )
607
- }
604
+ },
605
+ heightMatchParent = true
606
+ )
608
607
)
609
608
}
610
609
611
610
is BookFormat .BookFormatAudioBook -> {
612
- val loanDuration = getLoanDuration (book)
611
+ val loanDuration = this .getLoanDurationText (book)
613
612
this .idleButtons.addView(
614
- if (loanDuration.isNotEmpty()) {
615
- this .buttonCreator.createListenButtonWithLoanDuration(loanDuration) {
613
+ this .buttonCreator.createListenButton(
614
+ onClick = {
616
615
this .onBookViewerOpen(book, format)
617
- }
618
- } else {
619
- this .buttonCreator.createListenButton(
620
- onClick = {
621
- this .onBookViewerOpen(book, format)
622
- },
623
- heightMatchParent = true
624
- )
625
- }
616
+ },
617
+ heightMatchParent = true
618
+ )
626
619
)
627
620
}
628
621
@@ -631,7 +624,7 @@ class CatalogFeedPagingDataAdapter(
631
624
}
632
625
}
633
626
634
- if (isBookReturnable(book)) {
627
+ if (this . isBookReturnable(book)) {
635
628
this .idleButtons.addView(this .buttonCreator.createButtonSpace())
636
629
this .idleButtons.addView(
637
630
this .buttonCreator.createRevokeLoanButton(
@@ -641,7 +634,7 @@ class CatalogFeedPagingDataAdapter(
641
634
heightMatchParent = true
642
635
)
643
636
)
644
- } else if (isBookDeletable(book)) {
637
+ } else if (this . isBookDeletable(book)) {
645
638
this .idleButtons.addView(this .buttonCreator.createButtonSpace())
646
639
this .idleButtons.addView(
647
640
this .buttonCreator.createRevokeLoanButton(
@@ -654,6 +647,20 @@ class CatalogFeedPagingDataAdapter(
654
647
}
655
648
}
656
649
650
+ private fun setIdleTime (
651
+ loanDuration : DateTime ?
652
+ ) {
653
+ if (loanDuration != null ) {
654
+ this .idleTime.text =
655
+ this .idleTime.resources.getString(
656
+ R .string.catalogBookCellBorrowingUntil,
657
+ loanEndFormatter.print (loanDuration)
658
+ )
659
+ } else {
660
+ this .idleTime.text = " "
661
+ }
662
+ }
663
+
657
664
private fun onBookStatusLoanedNotDownloaded (
658
665
book : Book ,
659
666
status : Loaned .LoanedNotDownloaded
@@ -664,17 +671,10 @@ class CatalogFeedPagingDataAdapter(
664
671
this .setVisible(this .progress, false )
665
672
666
673
this .idleButtons.removeAllViews()
667
-
668
- val loanDuration = getLoanDuration(book)
674
+ this .setIdleTime(this .getLoanDuration(book))
669
675
670
676
this .idleButtons.addView(
671
677
when {
672
- loanDuration.isNotEmpty() -> {
673
- this .buttonCreator.createDownloadButtonWithLoanDuration(loanDuration) {
674
- this .onBookBorrow(book)
675
- }
676
- }
677
-
678
678
status.isOpenAccess -> {
679
679
this .buttonCreator.createGetButton(
680
680
onClick = {
@@ -695,7 +695,7 @@ class CatalogFeedPagingDataAdapter(
695
695
}
696
696
)
697
697
698
- if (isBookReturnable(book)) {
698
+ if (this . isBookReturnable(book)) {
699
699
this .idleButtons.addView(this .buttonCreator.createButtonSpace())
700
700
this .idleButtons.addView(
701
701
this .buttonCreator.createRevokeLoanButton(
@@ -705,7 +705,7 @@ class CatalogFeedPagingDataAdapter(
705
705
heightMatchParent = true
706
706
)
707
707
)
708
- } else if (isBookDeletable(book)) {
708
+ } else if (this . isBookDeletable(book)) {
709
709
this .idleButtons.addView(this .buttonCreator.createButtonSpace())
710
710
this .idleButtons.addView(
711
711
this .buttonCreator.createRevokeLoanButton(
@@ -788,27 +788,34 @@ class CatalogFeedPagingDataAdapter(
788
788
* existing code.
789
789
*/
790
790
791
- private fun getLoanDuration (book : Book ): String {
791
+ private fun getLoanDurationText (
792
+ book : Book
793
+ ): String {
794
+ val endDate = this .getLoanDuration(book)
795
+ return if (endDate != null ) {
796
+ CatalogBookAvailabilityStrings .intervalStringLoanDuration(
797
+ this .view.context.resources,
798
+ DateTime .now(),
799
+ endDate
800
+ )
801
+ } else {
802
+ " "
803
+ }
804
+ }
805
+
806
+ private fun getLoanDuration (
807
+ book : Book
808
+ ): DateTime ? {
792
809
val status = BookStatus .fromBook(book)
793
810
return if (status is Loaned .LoanedDownloaded ||
794
811
status is Loaned .LoanedNotDownloaded
795
812
) {
796
813
val endDate = (status as ? Loaned .LoanedDownloaded )?.loanExpiryDate
797
814
? : (status as ? Loaned .LoanedNotDownloaded )?.loanExpiryDate
798
815
799
- if (
800
- endDate != null
801
- ) {
802
- CatalogBookAvailabilityStrings .intervalStringLoanDuration(
803
- this .view.context.resources,
804
- DateTime .now(),
805
- endDate
806
- )
807
- } else {
808
- " "
809
- }
816
+ endDate
810
817
} else {
811
- " "
818
+ null
812
819
}
813
820
}
814
821
0 commit comments