Skip to content

Commit

Permalink
Implement logic to add expiration date to banned books and remove exp…
Browse files Browse the repository at this point in the history
…ired banned books
  • Loading branch information
ExplorerNautilus committed Nov 17, 2023
1 parent 8183fb6 commit 7078bc8
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Simplified.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2556,8 +2556,8 @@
73FB0AC824EB403D0072E430 /* NYPLBookContentTypeConverter.swift */,
1139DA6319C7755D00A07810 /* NYPLBookCoverRegistry.h */,
1139DA6419C7755D00A07810 /* NYPLBookCoverRegistry.m */,
1164F104199AC236009BF8BF /* NYPLBookLocation.h */,
1164F105199AC236009BF8BF /* NYPLBookLocation.m */,
1164F104199AC236009BF8BF /* NYPLBookLocation.h */,
11616E0F196B0531003D60D9 /* NYPLBookRegistry.h */,
11616E10196B0531003D60D9 /* NYPLBookRegistry.m */,
112C694B197301FE00C48F95 /* NYPLBookRegistryRecord.h */,
Expand Down
5 changes: 5 additions & 0 deletions Simplified/Book/Models/NYPLBook.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,9 @@
/// @return The default NYPLBookContentType
- (NYPLBookContentType)defaultBookContentType;

/// Add a custom expiration date to book if
/// 1. book is distributed by Axis360
/// 2. book does not contain an expiration date
- (void)addCustomExpirateDate:(nonnull NSDate *)date;

@end
15 changes: 15 additions & 0 deletions Simplified/Book/Models/NYPLBook.m
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,19 @@ - (NYPLBookContentType)defaultBookContentType
return defaultType;
}

- (void)addCustomExpirateDate:(nonnull NSDate *)date
{
if (self.defaultAcquisitionIfOpenAccess.type &&
[self.defaultAcquisitionIfOpenAccess.type isEqualToString:ContentTypeAxis360] &&
!self.defaultAcquisitionIfOpenAccess.availability.until)
{
NYPLOPDSAcquisitionAvailabilityLimited *currentAvailability = (NYPLOPDSAcquisitionAvailabilityLimited *)self.defaultAcquisition.availability;
NYPLOPDSAcquisitionAvailabilityLimited *newAvailability = [[NYPLOPDSAcquisitionAvailabilityLimited alloc]
initWithCopiesAvailable:currentAvailability.copiesAvailable
copiesTotal:currentAvailability.copiesTotal
since:currentAvailability.since
until:date];
[self.defaultAcquisition updateAvailability:newAvailability];
}
}
@end
6 changes: 6 additions & 0 deletions Simplified/Book/Models/NYPLBookRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ typedef NS_ENUM(NSInteger, NYPLBookState);
*/
- (void)syncWithStandardAlertsOnCompletion;

/**
Goes through all books in registry and remove the expired books distributed by Axis 360.
@note: Only call this function if the library does not require authentication
*/
- (void)removeExpiredBooksWithoutFeed;

// Adds a book to the book registry until it is manually removed. It allows the application to
// present information about obtained books when offline. Attempting to add a book already present
// will overwrite the existing book as if |updateBook:| were called. The location may be nil. The
Expand Down
23 changes: 23 additions & 0 deletions Simplified/Book/Models/NYPLBookRegistry.m
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,29 @@ - (void)syncWithStandardAlertsOnCompletion
}];
}

- (void)removeExpiredBooksWithoutFeed
{
@synchronized(self) {
NSMutableArray *booksToRemove = [[NSMutableArray alloc] init];
for (NSString *bookIdentifer in self.identifiersToRecords) {
NYPLBookRegistryRecord *record = self.identifiersToRecords[bookIdentifer];
// Add the book to remove list if it is distributed by Axis360 and expired
if(record &&
record.book.defaultAcquisition.type &&
[record.book.defaultAcquisition.type isEqualToString:ContentTypeAxis360]) {
if (record.book.defaultAcquisition.availability.until &&
[record.book.defaultAcquisition.availability.until compare:[NSDate date]] == NSOrderedAscending) {
[booksToRemove addObject:bookIdentifer];
}
}
}
for (NSString *bookIdentifier in booksToRemove) {
[self removeBookForIdentifier:bookIdentifier];
}
[self save];
}
}

- (void)addBook:(NYPLBook *const)book
location:(NYPLBookLocation *const)location
state:(NSInteger)state
Expand Down
3 changes: 3 additions & 0 deletions Simplified/Catalog/NYPLCatalogFeedViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ - (void)viewDidLoad
}

[[NYPLBookRegistry sharedRegistry] justLoad];
if (!NYPLUserAccount.sharedAccount.requiresUserAuthentication) {
[[NYPLBookRegistry sharedRegistry] removeExpiredBooksWithoutFeed];
}
UIApplicationState applicationState = [[UIApplication sharedApplication] applicationState];
if (applicationState == UIApplicationStateActive) {
[self syncBookRegistryForNewFeed];
Expand Down
13 changes: 13 additions & 0 deletions Simplified/Catalog/NYPLCatalogGroupedFeed.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ - (instancetype)initWithOPDSFeed:(NYPLOPDSFeed *)feed
NSMutableDictionary *const groupTitleToMutableBookArray = [NSMutableDictionary dictionary];
NSMutableDictionary *const groupTitleToURLOrNull = [NSMutableDictionary dictionary];

// Create an expiration date object with value of 2 months from now
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:2];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *expirationDate = [calendar dateByAddingComponents:dateComponents toDate:[NSDate new] options:0];

for(NYPLOPDSEntry *const entry in feed.entries) {
if(!entry.groupAttributes) {
NYPLLOG(@"Ignoring entry with missing group.");
Expand All @@ -110,6 +116,13 @@ - (instancetype)initWithOPDSFeed:(NYPLOPDSFeed *)feed
continue;
}

/// Add a custom expiration date (2 months from now) to banned book.
/// This expiration date will be overwritten by the updatedBookMetadata function below
/// if the book is already checked out.
if (!NYPLUserAccount.sharedAccount.requiresUserAuthentication) {
[book addCustomExpirateDate:expirationDate];
}

NYPLBook *updatedBook = [[NYPLBookRegistry sharedRegistry] updatedBookMetadata:book];
if(updatedBook) {
book = updatedBook;
Expand Down
13 changes: 13 additions & 0 deletions Simplified/Catalog/NYPLCatalogUngroupedFeed.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ - (instancetype)initWithOPDSFeed:(NYPLOPDSFeed *const)feed

self.books = [NSMutableArray array];

// Create an expiration date object with value of 2 months from now
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:2];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *expirationDate = [calendar dateByAddingComponents:dateComponents toDate:[NSDate new] options:0];

for(NYPLOPDSEntry *const entry in feed.entries) {
NYPLBook *book = [NYPLBook bookWithEntry:entry];
if(!book) {
Expand All @@ -50,6 +56,13 @@ - (instancetype)initWithOPDSFeed:(NYPLOPDSFeed *const)feed
continue;
}

/// Add a custom expiration date (2 months from now) to banned book.
/// This expiration date will be overwritten by the updatedBookMetadata function below
/// if the book is already checked out.
if (!NYPLUserAccount.sharedAccount.requiresUserAuthentication) {
[book addCustomExpirateDate:expirationDate];
}

NYPLBook *updatedBook = [[NYPLBookRegistry sharedRegistry] updatedBookMetadata:book];
if(updatedBook) {
book = updatedBook;
Expand Down
2 changes: 2 additions & 0 deletions Simplified/OPDS/NYPLOPDSAcquisition.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,6 @@ NYPLOPDSAcquisitionRelationString(NYPLOPDSAcquisitionRelation relation);
/// @c acquisitionWithDictionary: method for later deserialization.
- (NSDictionary *_Nonnull)dictionaryRepresentation;

- (void)updateAvailability:(id<NYPLOPDSAcquisitionAvailability> _Nonnull)availability;

@end
4 changes: 4 additions & 0 deletions Simplified/OPDS/NYPLOPDSAcquisition.m
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,8 @@ - (NSDictionary *_Nonnull)dictionaryRepresentation
};
}

- (void)updateAvailability:(id<NYPLOPDSAcquisitionAvailability> _Nonnull)availability {
self.availability = availability;
}

@end

0 comments on commit 7078bc8

Please sign in to comment.