Przeglądaj źródła

network browser VC: add pull-to-refresh (refs #14731)

Felix Paul Kühne 9 lat temu
rodzic
commit
1d37786e8c

+ 25 - 2
Sources/LocalNetworkConnectivity/ServerBrowsing/VLCNetworkServerBrowserViewController.m

@@ -25,6 +25,9 @@
 #import "VLCNetworkServerBrowser-Protocol.h"
 
 @interface VLCNetworkServerBrowserViewController () <VLCNetworkServerBrowserDelegate,VLCNetworkListCellDelegate, UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate>
+{
+    UIRefreshControl *_refreshControl;
+}
 @property (nonatomic) id<VLCNetworkServerBrowser> serverBrowser;
 @property (nonatomic) NSByteCountFormatter *byteCounterFormatter;
 @property (nonatomic) NSArray<id<VLCNetworkServerBrowserItem>> *searchArray;
@@ -50,6 +53,12 @@
 {
     [super viewDidLoad];
 
+    _refreshControl = [[UIRefreshControl alloc] init];
+    _refreshControl.backgroundColor = [UIColor VLCDarkBackgroundColor];
+    _refreshControl.tintColor = [UIColor whiteColor];
+    [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
+    [self.tableView addSubview:_refreshControl];
+
     self.title = self.serverBrowser.title;
     [self update];
 }
@@ -57,7 +66,7 @@
 - (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser {
     [self.tableView reloadData];
     [[VLCActivityManager defaultManager] networkActivityStopped];
-
+    [_refreshControl endRefreshing];
 }
 
 - (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error {
@@ -77,6 +86,21 @@
     [[VLCActivityManager defaultManager] networkActivityStarted];
 }
 
+-(void)handleRefresh
+{
+    //set the title while refreshing
+    _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
+    //set the date and time of refreshing
+    NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
+    [formattedDate setDateFormat:@"MMM d, h:mm a"];
+    NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
+    NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
+    _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
+    //end the refreshing
+
+    [self update];
+}
+
 #pragma mark -
 - (NSByteCountFormatter *)byteCounterFormatter {
     if (!_byteCounterFormatter) {
@@ -294,7 +318,6 @@
     }
 
     if (item.isContainer) {
-
         VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:item.containerBrowser];
         [self.navigationController pushViewController:targetViewController animated:YES];
     } else {