Skip to content

Commit

Permalink
fix: Remove hardcode
Browse files Browse the repository at this point in the history
  • Loading branch information
BaptGrv committed Oct 21, 2024
1 parent 31e8a01 commit b647c3f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
41 changes: 30 additions & 11 deletions kDrive/UI/Controller/Menu/PhotoSyncSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ final class PhotoSyncSettingsViewController: BaseGroupedTableViewController {
@LazyInjectService var accountManager: AccountManageable
@LazyInjectService var photoLibraryUploader: PhotoLibraryUploader

private enum PhotoSyncSection {
private enum PhotoSyncSection: Int {
case syncSwitch
case syncLocation
case syncSettings
case syncDenied
}

private enum PhotoSyncSwitchRows: CaseIterable {
private enum PhotoSyncSwitchRows: Int, CaseIterable {
case syncSwitch
}

private enum PhotoSyncLocationRows: CaseIterable {
private enum PhotoSyncLocationRows: Int, CaseIterable {
case driveSelection
case folderSelection
}

private enum PhotoSyncSettingsRows: CaseIterable {
private enum PhotoSyncSettingsRows: Int, CaseIterable {
case syncMode
case importPicturesSwitch
case importVideosSwitch
Expand Down Expand Up @@ -495,7 +495,7 @@ extension PhotoSyncSettingsViewController {
navigationController?.pushViewController(selectPhotoFormatViewController, animated: true)
case .wifiSync:
let wifiSyncSettingsViewController = WifiSyncSettingsViewController
.instantiate(selectedMod: newSyncSettings.wifiSync)
.instantiate(selectedMode: newSyncSettings.wifiSync)
wifiSyncSettingsViewController.delegate = self
navigationController?.pushViewController(wifiSyncSettingsViewController, animated: true)
default:
Expand All @@ -512,7 +512,14 @@ extension PhotoSyncSettingsViewController: SelectDriveDelegate {
driveFileManager = accountManager.getDriveFileManager(for: drive.id, userId: drive.userId)
selectedDirectory = nil
updateSaveButtonState()
tableView.reloadRows(at: [IndexPath(row: 0, section: 1), IndexPath(row: 1, section: 1)], with: .fade)
tableView.reloadRows(
at: [IndexPath(row: PhotoSyncSettingsRows.syncMode.rawValue, section: PhotoSyncSection.syncLocation.rawValue),
IndexPath(
row: PhotoSyncSettingsRows.importPicturesSwitch.rawValue,
section: PhotoSyncSection.syncLocation.rawValue
)],
with: .fade
)
}
}

Expand All @@ -522,7 +529,13 @@ extension PhotoSyncSettingsViewController: SelectFolderDelegate {
func didSelectFolder(_ folder: File) {
selectedDirectory = folder
updateSaveButtonState()
tableView.reloadRows(at: [IndexPath(row: 1, section: 1)], with: .fade)
tableView.reloadRows(
at: [IndexPath(
row: PhotoSyncSettingsRows.importPicturesSwitch.rawValue,
section: PhotoSyncSection.syncLocation.rawValue
)],
with: .fade
)
}
}

Expand All @@ -532,7 +545,10 @@ extension PhotoSyncSettingsViewController: SelectPhotoFormatDelegate {
func didSelectPhotoFormat(_ format: PhotoFileFormat) {
newSyncSettings.photoFormat = format
updateSaveButtonState()
tableView.reloadRows(at: [IndexPath(row: 6, section: 2)], with: .fade)
tableView.reloadRows(
at: [IndexPath(row: PhotoSyncSettingsRows.photoFormat.rawValue, section: PhotoSyncSection.syncSettings.rawValue)],
with: .fade
)
}
}

Expand Down Expand Up @@ -566,8 +582,11 @@ extension PhotoSyncSettingsViewController: PhotoSyncSettingsTableViewCellDelegat
}

extension PhotoSyncSettingsViewController: WifiSyncSettingsDelegate {
func didSelectSyncMode(_ mod: SyncMode) {
newSyncSettings.wifiSync = mod
tableView.reloadRows(at: [IndexPath(row: 7, section: 2)], with: .fade)
func didSelectSyncMode(_ mode: SyncMode) {
newSyncSettings.wifiSync = mode
tableView.reloadRows(
at: [IndexPath(row: PhotoSyncSettingsRows.wifiSync.rawValue, section: PhotoSyncSection.syncSettings.rawValue)],
with: .fade
)
}
}
20 changes: 10 additions & 10 deletions kDrive/UI/Controller/Menu/WifiSyncSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import kDriveResources
import UIKit

protocol WifiSyncSettingsDelegate: AnyObject {
func didSelectSyncMode(_ mod: SyncMode)
func didSelectSyncMode(_ mode: SyncMode)
}

class WifiSyncSettingsViewController: BaseGroupedTableViewController {
@LazyInjectService private var appNavigable: AppNavigable

private var tableContent: [SyncMode] = SyncMode.allCases
private var selectedMod: SyncMode!
private var selectedMode: SyncMode!
weak var delegate: WifiSyncSettingsDelegate?

override func viewDidLoad() {
Expand All @@ -41,12 +41,12 @@ class WifiSyncSettingsViewController: BaseGroupedTableViewController {
tableView.register(cellView: ParameterSyncTableViewCell.self)
tableView.allowsMultipleSelection = false

selectedMod = UserDefaults.shared.syncMode
selectedMode = UserDefaults.shared.syncMode
}

static func instantiate(selectedMod: SyncMode) -> WifiSyncSettingsViewController {
static func instantiate(selectedMode: SyncMode) -> WifiSyncSettingsViewController {
let viewController = WifiSyncSettingsViewController()
viewController.selectedMod = selectedMod
viewController.selectedMode = selectedMode
return viewController
}

Expand All @@ -66,18 +66,18 @@ class WifiSyncSettingsViewController: BaseGroupedTableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(type: ParameterSyncTableViewCell.self, for: indexPath)
cell.initWithPositionAndShadow(isFirst: true, isLast: true)
let currentMod = tableContent[indexPath.row]
if currentMod == selectedMod {
let currentMode = tableContent[indexPath.row]
if currentMode == selectedMode {
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
}
cell.syncTitleLabel.text = currentMod.title
cell.syncDetailLabel.text = currentMod.selectionTitle
cell.syncTitleLabel.text = currentMode.title
cell.syncDetailLabel.text = currentMode.selectionTitle
return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let mode = tableContent[indexPath.row]
MatomoUtils.track(eventWithCategory: .settings, name: "mod\(mode.rawValue.capitalized)")
MatomoUtils.track(eventWithCategory: .settings, name: "mode\(mode.rawValue.capitalized)")
UserDefaults.shared.syncMode = mode
delegate?.didSelectSyncMode(tableContent[indexPath.row])
navigationController?.popViewController(animated: true)
Expand Down
4 changes: 2 additions & 2 deletions kDriveCore/Utils/DriveUserDefaults+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ public extension UserDefaults {
var syncMode: SyncMode {
get {
if let rawValue = object(forKey: key(.syncMode)) as? String,
let mod = SyncMode(rawValue: rawValue) {
return mod
let mode = SyncMode(rawValue: rawValue) {
return mode
}
return .onlyWifi
}
Expand Down

0 comments on commit b647c3f

Please sign in to comment.