@@ -11,6 +11,7 @@ import Foundation
1111import  UIKit
1212import  CoreLocation
1313import  MapCache
14+ import  CoreServices
1415
1516/// Units Section Id in PreferencesTableViewController
1617let  kUnitsSection  =  0 
@@ -27,6 +28,9 @@ let kActivityTypeSection = 3
2728/// Default Name Section Id in PreferencesTableViewController
2829let  kDefaultNameSection  =  4 
2930
31+ /// GPX Files Location Section Id in PreferencesTableViewController
32+ let  kGPXFilesLocationSection  =  5 
33+ 
3034/// Cell Id of the Use Imperial units in UnitsSection
3135let  kUseImperialUnitsCell  =  0 
3236
@@ -44,7 +48,7 @@ let kClearCacheCell = 1
4448/// Preferences are kept on UserDefaults with the keys `kDefaultKeyTileServerInt` (Int)
4549/// and `kDefaultUseCache`` (Bool)
4650///
47- class  PreferencesTableViewController :  UITableViewController ,  UINavigationBarDelegate  { 
51+ class  PreferencesTableViewController :  UITableViewController ,  UINavigationBarDelegate ,   UIDocumentPickerDelegate  { 
4852
4953    /// Delegate for this table view controller.
5054    weak var  delegate :  PreferencesTableViewControllerDelegate ? 
@@ -102,7 +106,7 @@ class PreferencesTableViewController: UITableViewController, UINavigationBarDele
102106    /// Returns 4 sections: Units, Cache, Map Source, Activity Type
103107    override   func  numberOfSections( in tableView:  UITableView ? )  ->  Int  { 
104108        // Return the number of sections.
105-         return  5 
109+         return  6 
106110    } 
107111
108112    /// Returns the title of the existing sections.
@@ -115,6 +119,7 @@ class PreferencesTableViewController: UITableViewController, UINavigationBarDele
115119        case  kMapSourceSection:  return  NSLocalizedString ( " MAP_SOURCE " ,  comment:  " no comment " ) 
116120        case  kActivityTypeSection:  return  NSLocalizedString ( " ACTIVITY_TYPE " ,  comment:  " no comment " ) 
117121        case  kDefaultNameSection:  return  NSLocalizedString ( " DEFAULT_NAME_SECTION " ,  comment:  " no comment " ) 
122+         case  kGPXFilesLocationSection:  return  NSLocalizedString ( " GPX_FILES_LOCATION_SECTION " ,  comment:  " no comment " ) 
118123        default :  fatalError ( " Unknown section " ) 
119124        } 
120125    } 
@@ -129,6 +134,7 @@ class PreferencesTableViewController: UITableViewController, UINavigationBarDele
129134        case  kMapSourceSection:  return  GPXTileServer . count
130135        case  kActivityTypeSection:  return  CLActivityType . count
131136        case  kDefaultNameSection:  return  1 
137+         case  kGPXFilesLocationSection:  return  1 
132138        default :  fatalError ( " Unknown section " ) 
133139        } 
134140    } 
@@ -214,6 +220,18 @@ class PreferencesTableViewController: UITableViewController, UINavigationBarDele
214220            cell. accessoryType =  . disclosureIndicator
215221        } 
216222
223+         if  indexPath. section ==  kGPXFilesLocationSection { 
224+             cell =  UITableViewCell ( style:  . subtitle,  reuseIdentifier:  " GPXFilesLocation " ) 
225+             cell. textLabel? . text =  NSLocalizedString ( " PRESS_TO_SELECT_FOLDER " ,  comment:  " no comment " ) 
226+             if  let  url =  preferences. gpxFilesFolderURL { 
227+                 cell. detailTextLabel? . lineBreakMode =  . byTruncatingHead
228+                 cell. detailTextLabel? . text =  url. lastPathComponent
229+             }  else  { 
230+                 cell. detailTextLabel? . text =  NSLocalizedString ( " USING_DEFAULT_FOLDER " ,  comment:  " no comment " ) 
231+             } 
232+             cell. accessoryType =  . disclosureIndicator
233+         } 
234+         
217235        return  cell
218236    } 
219237
@@ -312,7 +330,26 @@ class PreferencesTableViewController: UITableViewController, UINavigationBarDele
312330            self . navigationController? . pushViewController ( DefaultNameSetupViewController ( style:  . grouped) ,  animated:  true ) 
313331        } 
314332
333+         if  indexPath. section ==  kGPXFilesLocationSection { 
334+             print ( " PreferencesTableView GPX Files Location cell clicked " ) 
335+             let  documentVC  =  UIDocumentPickerViewController ( documentTypes:  [ kUTTypeFolder as  String ] ,  in:  . open) 
336+             documentVC. allowsMultipleSelection =  false 
337+             documentVC. delegate =  self 
338+             self . present ( documentVC,  animated:  true ) 
339+         } 
340+         
315341        //unselect row
316342        tableView. deselectRow ( at:  indexPath,  animated:  true ) 
317343    } 
344+     
345+     // MARK: - UIDocumentPickerDelegate
346+     
347+     func  documentPicker( _ controller:  UIDocumentPickerViewController ,  didPickDocumentsAt urls:  [ URL ] )  { 
348+         guard  let  folderURL =  urls. first else  { 
349+             print ( " Didn't select any folder " ) 
350+             return 
351+         } 
352+         preferences. gpxFilesFolderURL =  folderURL
353+         tableView. reloadData ( ) 
354+     } 
318355} 
0 commit comments