Skip to content

Commit

Permalink
Minor refactor on banned books removal
Browse files Browse the repository at this point in the history
  • Loading branch information
ExplorerNautilus committed Nov 19, 2023
1 parent 7c633ad commit 909a295
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Simplified/Book/Models/NYPLBook.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@
/// @return The default NYPLBookContentType
- (NYPLBookContentType)defaultBookContentType;

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

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

- (void)addCustomExpirateDate:(nonnull NSDate *)date
- (void)addBannedBookExpiration
{
if (self.defaultAcquisitionIfOpenAccess.type &&
[self.defaultAcquisitionIfOpenAccess.type isEqualToString:ContentTypeAxis360] &&
if ([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];
until:[self createBannedBookExpirationDate]];
[self.defaultAcquisition setAvailability:newAvailability];
}
}

// Create an expiration date object with value of 2 months from now
- (NSDate *)createBannedBookExpirationDate {
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:2];
NSCalendar *calendar = [NSCalendar currentCalendar];
return [calendar dateByAddingComponents:dateComponents toDate:[NSDate new] options:0];
}
@end
4 changes: 2 additions & 2 deletions Simplified/Book/Models/NYPLBookRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ typedef NS_ENUM(NSInteger, NYPLBookState);
- (void)syncWithStandardAlertsOnCompletion;

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

// 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
Expand Down
6 changes: 2 additions & 4 deletions Simplified/Book/Models/NYPLBookRegistry.m
Original file line number Diff line number Diff line change
Expand Up @@ -448,16 +448,14 @@ - (void)syncWithStandardAlertsOnCompletion
}];
}

- (void)removeExpiredBooksWithoutFeed
- (void)removeExpiredBannedBooks
{
@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.type isEqualToString:ContentTypeAxis360]) {
if (record.book.defaultAcquisition.availability.until &&
[record.book.defaultAcquisition.availability.until compare:[NSDate date]] == NSOrderedAscending) {
[booksToRemove addObject:bookIdentifer];
Expand Down
2 changes: 1 addition & 1 deletion Simplified/Catalog/NYPLCatalogFeedViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ - (void)viewDidLoad

[[NYPLBookRegistry sharedRegistry] justLoad];
if (!NYPLUserAccount.sharedAccount.requiresUserAuthentication) {
[[NYPLBookRegistry sharedRegistry] removeExpiredBooksWithoutFeed];
[[NYPLBookRegistry sharedRegistry] removeExpiredBannedBooks];
}
UIApplicationState applicationState = [[UIApplication sharedApplication] applicationState];
if (applicationState == UIApplicationStateActive) {
Expand Down
8 changes: 1 addition & 7 deletions Simplified/Catalog/NYPLCatalogGroupedFeed.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ - (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 @@ -120,7 +114,7 @@ - (instancetype)initWithOPDSFeed:(NYPLOPDSFeed *)feed
/// 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];
[book addBannedBookExpiration];
}

NYPLBook *updatedBook = [[NYPLBookRegistry sharedRegistry] updatedBookMetadata:book];
Expand Down
8 changes: 1 addition & 7 deletions Simplified/Catalog/NYPLCatalogUngroupedFeed.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ - (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 @@ -60,7 +54,7 @@ - (instancetype)initWithOPDSFeed:(NYPLOPDSFeed *const)feed
/// 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];
[book addBannedBookExpiration];
}

NYPLBook *updatedBook = [[NYPLBookRegistry sharedRegistry] updatedBookMetadata:book];
Expand Down
4 changes: 1 addition & 3 deletions Simplified/OPDS/NYPLOPDSAcquisition.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ NYPLOPDSAcquisitionRelationString(NYPLOPDSAcquisitionRelation relation);
@property (nonatomic, readonly, nonnull) NSArray<NYPLOPDSIndirectAcquisition *> *indirectAcquisitions;

/// The availability of the result of the acquisition.
@property (nonatomic, readonly, nonnull) id<NYPLOPDSAcquisitionAvailability> availability;
@property (nonatomic, nonnull) id<NYPLOPDSAcquisitionAvailability> availability;

+ (instancetype _Null_unspecified)new NS_UNAVAILABLE;
- (instancetype _Null_unspecified)init NS_UNAVAILABLE;
Expand Down Expand Up @@ -100,6 +100,4 @@ NYPLOPDSAcquisitionRelationString(NYPLOPDSAcquisitionRelation relation);
/// @c acquisitionWithDictionary: method for later deserialization.
- (NSDictionary *_Nonnull)dictionaryRepresentation;

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

@end
7 changes: 2 additions & 5 deletions Simplified/OPDS/NYPLOPDSAcquisition.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ @interface NYPLOPDSAcquisition ()
@property (nonatomic, copy, nonnull) NSString *type;
@property (nonatomic, nonnull) NSURL *hrefURL;
@property (nonatomic, nonnull) NSArray<NYPLOPDSIndirectAcquisition *> *indirectAcquisitions;
@property (nonatomic, nonnull) id<NYPLOPDSAcquisitionAvailability> availability;
/// The NYPLOPDSAcquisitionAvailability property is omitted here because it's no longer a read only object.
/// Therefore, we do not need a private declaration in order to write to it.

@end

Expand Down Expand Up @@ -276,8 +277,4 @@ - (NSDictionary *_Nonnull)dictionaryRepresentation
};
}

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

@end

0 comments on commit 909a295

Please sign in to comment.