Skip to content

Commit

Permalink
SwiftLint: fix the low hanging fruits
Browse files Browse the repository at this point in the history
  • Loading branch information
sitomani committed Oct 7, 2023
1 parent 55fbfe9 commit 86b158f
Show file tree
Hide file tree
Showing 24 changed files with 165 additions and 166 deletions.
2 changes: 1 addition & 1 deletion 4champ/Data/Structs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ struct MMD: Identifiable {
}

extension MMD: Equatable {}
func ==(lhs: MMD, rhs: MMD) -> Bool {
func == (lhs: MMD, rhs: MMD) -> Bool {
let eq = lhs.id == rhs.id && lhs.id != nil
return eq
}
5 changes: 2 additions & 3 deletions 4champ/Networking/ModuleFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ enum FetcherError: Error {
ModuleFetcher state delegate.
Implemented in classes that use a fetcher to download modules, e.g. RadioInteractor and SearchInteractor
*/
protocol ModuleFetcherDelegate {
protocol ModuleFetcherDelegate: class {
/**
Fetcher calls delegate on state changes.
- parameters:
Expand Down Expand Up @@ -166,8 +166,7 @@ class ModuleFetcher {
- returns: module data, unzipped
*/
private func gzipInflate(data: Data) -> Data? {
if data.isGzipped {
let inflated = try! data.gunzipped()
if data.isGzipped, let inflated = try? data.gunzipped() {
return inflated
}
log.error("FAILED TO UNZIP")
Expand Down
22 changes: 9 additions & 13 deletions 4champ/Replay/ModulePlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,13 @@ class ModulePlayer: NSObject {

/// called when app resigns to clear non-stored modules
func cleanup() {
for mod in playQueue {
if mod.hasBeenSaved() == false {
if let url = mod.localPath {
log.info("Deleting module \(url.lastPathComponent)")
do {
try FileManager.default.removeItem(at: url)
} catch {
log.error("Deleting file at \(url) failed, \(error)")
}
for mod in playQueue where !mod.hasBeenSaved() {
if let url = mod.localPath {
log.info("Deleting module \(url.lastPathComponent)")
do {
try FileManager.default.removeItem(at: url)
} catch {
log.error("Deleting file at \(url) failed, \(error)")
}
}
}
Expand Down Expand Up @@ -333,10 +331,8 @@ extension ModulePlayer: ModuleStorageObserver {
currentModule?.favorite = mmd.favorite
}
if playQueue.count == 0 { return }
for i in 0...playQueue.count-1 {
if playQueue[i] == mmd {
playQueue[i].favorite = mmd.favorite
}
for qIndex in 0...playQueue.count-1 where playQueue[qIndex] == mmd {
playQueue[qIndex].favorite = mmd.favorite
}
_ = observers.map {
$0.queueChanged(changeType: .other)
Expand Down
1 change: 0 additions & 1 deletion 4champ/Scenes/About/AboutViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ class AboutViewController: UIViewController, AboutDisplayLogic {

// MARK: Display Logic
func displayNowPlaying(_ viewModel: About.Status.ViewModel) {
// TODO
}

@objc func reviewNow() {
Expand Down
6 changes: 5 additions & 1 deletion 4champ/Scenes/Local/LocalInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ class LocalInteractor: NSObject, LocalBusinessLogic, LocalDataStore {
fetchRequest.predicate = NSPredicate.init(format: filterString)
frc = moduleStorage.createFRC(fetchRequest: fetchRequest, entityName: "ModuleInfo")
frc?.delegate = self
try! frc?.performFetch()
do {
try frc?.performFetch()
} catch {
log.error("Fetch failed \(error)")
}
presenter?.presentModules(response: Local.SortFilter.Response())
}

Expand Down
2 changes: 1 addition & 1 deletion 4champ/Scenes/Local/ModuleStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class ModuleStorage: NSObject {
let filterString = "plId == 'default'"
fetchRequest.predicate = NSPredicate.init(format: filterString)
let frc = createFRC(fetchRequest: fetchRequest, entityName: "Playlist")
try! frc.performFetch()
try? frc.performFetch()
guard let defaultPl = frc.fetchedObjects?.first else {
// No Default playlist yet, must create it
return createPlaylist(name: "default", id: "default")
Expand Down
9 changes: 3 additions & 6 deletions 4champ/Scenes/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ class MainViewController: UITabBarController {
npView?.addGestureRecognizer(lpr)

let titles = ["TabBar_Local", "TabBar_Playlist", "TabBar_Search", "TabBar_Radio", "TabBar_About"]
for t in tabBar.items! {
if let index = tabBar.items!.index(of: t) {
t.title = titles[index].l13n()
for tab in tabBar.items! {
if let index = tabBar.items!.index(of: tab) {
tab.title = titles[index].l13n()
}
}
}
Expand Down Expand Up @@ -96,13 +96,10 @@ class MainViewController: UITabBarController {
switch event.subtype {
case .remoteControlPlay:
modulePlayer.resume()
break
case .remoteControlPause:
modulePlayer.pause()
break
case .remoteControlStop:
modulePlayer.stop()
break
case .remoteControlNextTrack:
modulePlayer.playNext()
case .remoteControlPreviousTrack:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class PlaylistSelectorInteractor: PlaylistSelectorBusinessLogic, PlaylistSelecto
let filterString = "plId != 'radioList'"
fetchRequest.predicate = NSPredicate.init(format: filterString)
frc = moduleStorage.createFRC(fetchRequest: fetchRequest, entityName: "Playlist")
try! frc?.performFetch()
try? frc?.performFetch()

if let plObjects = frc?.fetchedObjects {
var plMetaData = plObjects.map {
Expand All @@ -46,7 +46,7 @@ class PlaylistSelectorInteractor: PlaylistSelectorBusinessLogic, PlaylistSelecto

for pl in plObjects {
for mi in pl.modules ?? [] {
let modId = (mi as! ModuleInfo).modId?.intValue ?? 0
let modId = (mi as? ModuleInfo)?.modId?.intValue ?? 0
if let index = plMetaData.firstIndex(where: { $0.id == pl.plId }) {
plMetaData[index].modules.append(modId)
}
Expand Down Expand Up @@ -86,7 +86,7 @@ class PlaylistSelectorInteractor: PlaylistSelectorBusinessLogic, PlaylistSelecto
moduleStorage.saveContext()
presenter?.presentAppend(response: completed)
} else {
if let _ = module?.localPath {
if module?.localPath != nil {
// 2. Module is downloaded (radio/search) but not yet in database
moduleStorage.addModule(module: module!)
if let modInfo = moduleStorage.fetchModuleInfo(modId) {
Expand All @@ -112,7 +112,7 @@ class PlaylistSelectorInteractor: PlaylistSelectorBusinessLogic, PlaylistSelecto
let filterString = "plId == '\(id)'"
fetchRequest.predicate = NSPredicate.init(format: filterString)
let tmp = moduleStorage.createFRC(fetchRequest: fetchRequest, entityName: "Playlist")
try! tmp.performFetch()
try? tmp.performFetch()
return tmp.fetchedObjects?.first
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class PlaylistSelectorPresenter: PlaylistSelectorPresentationLogic {
}

var moduleName = response.module.name ?? ""
if let mod_name = response.module.name, let composer_name = response.module.composer, composer_name.count > 0 {
moduleName = String.init(format: "LockScreen_Playing".l13n(), mod_name, composer_name)
if let mName = response.module.name, let cName = response.module.composer, cName.count > 0 {
moduleName = String.init(format: "LockScreen_Playing".l13n(), mName, cName)
}

let status: DownloadStatus = response.module.hasBeenSaved() ? .complete : .unknown
Expand Down
12 changes: 6 additions & 6 deletions 4champ/Scenes/PlaylistSelector/PlaylistSelectorStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PlaylistSelectorStore: ObservableObject, PlaylistSelectorDisplayLogic {
weak var hostingController: UIHostingController<PlaylistPickerView>?

@Published var viewModel: PlaylistSelector.PrepareSelection.ViewModel

init() {
self.viewModel = PlaylistSelector.PrepareSelection.ViewModel(module: "<rnd>", currentPlaylistIndex: 0, playlistOptions: [], status: .unknown)
}
Expand All @@ -32,16 +32,16 @@ class PlaylistSelectorStore: ObservableObject, PlaylistSelectorDisplayLogic {
let pls = PlaylistSelectorStore()
var contentView = PlaylistPickerView(dismissAction: { pls.hostingController?.dismiss(animated: true, completion: nil)},
shareAction: {
pls.shareModule(module)
pls.shareModule(module)
},
deleteAction: {
pls.deleteModule(module)
pls.deleteModule(module)
},
store: pls)
pls.setup()
pls.doPrepare(mod: module)
contentView.addToPlaylistAction = { b in
pls.addToPlaylist(playlistIndex: b)
contentView.addToPlaylistAction = { pIndex in
pls.addToPlaylist(playlistIndex: pIndex)
}
let hvc = UIHostingController(rootView: contentView)
pls.hostingController = hvc
Expand Down Expand Up @@ -93,7 +93,7 @@ class PlaylistSelectorStore: ObservableObject, PlaylistSelectorDisplayLogic {
guard viewModel.status == .complete else {
return
}

interactor?.deleteModule(request: PlaylistSelector.Delete.Request(module: module))
hostingController?.dismiss(animated: true, completion: nil)
}
Expand Down
21 changes: 13 additions & 8 deletions 4champ/Scenes/Playlists/PlaylistInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@ class PlaylistInteractor: NSObject, PlaylistBusinessLogic, PlaylistDataStore {
var selectedPlaylistId: String?
var frc: NSFetchedResultsController<Playlist>?
// var name: String = ""

private var downloadController = DownloadController.init()

override init() {
super.init()
let filterString = "plId != 'radioList'"

let fetchRequest = NSFetchRequest<Playlist>.init(entityName: "Playlist")
fetchRequest.sortDescriptors = []
fetchRequest.predicate = NSPredicate.init(format: filterString)
frc = moduleStorage.createFRC(fetchRequest: fetchRequest, entityName: "Playlist")
frc?.delegate = self
try! frc?.performFetch()
do {
try frc?.performFetch()
} catch {
log.error("Failed to perform fetch: \(error)")
}

if selectedPlaylistId == nil {
selectedPlaylistId = "default"
Expand Down Expand Up @@ -125,11 +129,12 @@ class PlaylistInteractor: NSObject, PlaylistBusinessLogic, PlaylistDataStore {
private func rebuildQueue() {
if let pl = frc?.fetchedObjects?.first(where: { ($0 as Playlist).plId == selectedPlaylistId }) {
var playlistQueue: [MMD] = []
if let moduleinfos = pl.modules {
for mod in moduleinfos {
playlistQueue.append(MMD(cdi: (mod as! ModuleInfo)))
pl.modules?.forEach {
if let modInfo = $0 as? ModuleInfo {
playlistQueue.append(MMD(cdi: modInfo))
}
}

if pl.playmode?.boolValue ?? false {
playlistQueue.shuffle()
}
Expand All @@ -152,14 +157,14 @@ extension PlaylistInteractor: NSFetchedResultsControllerDelegate {
}

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {

guard modulePlayer.radioOn == false else {
return
}

if let pl = anObject as? Playlist, pl.plId == selectedPlaylistId {
rebuildQueue()
}
}
}

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
Expand Down
6 changes: 3 additions & 3 deletions 4champ/Scenes/Playlists/PlaylistPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class PlaylistPresenter: PlaylistPresentationLogic {
let shuffle = (pl.playmode?.intValue ?? 0) == 1

var mods: [MMD] = []
if let moduleinfos = pl.modules {
for mod in moduleinfos {
mods.append(MMD(cdi: (mod as! ModuleInfo)))
pl.modules?.forEach {
if let modInfo = $0 as? ModuleInfo {
mods.append(MMD(cdi: modInfo))
}
}

Expand Down
10 changes: 5 additions & 5 deletions 4champ/Scenes/Playlists/PlaylistView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct SUIModule: View {

struct PlaylistView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@State private var show_modal: Bool = false
@State private var showModal: Bool = false
@State var showNowPlaying: Bool = false
@State var isEditing: Bool = false
@State var navigationButtonID = UUID()
Expand Down Expand Up @@ -93,13 +93,13 @@ struct PlaylistView: View {
var body: some View {
VStack {
Button(action: {
self.show_modal = true
self.showModal = true
}) {
Text(store.viewModel.playlistName).underline()
.foregroundColor(Color(.white))
.padding(EdgeInsets.init(top: 5, leading: 0, bottom: -5, trailing: 0))
}.sheet(isPresented: self.$show_modal) {
PlaylistSelectorSUI(show_modal: self.$show_modal).environment(\.managedObjectContext, self.managedObjectContext).onDisappear {
}.sheet(isPresented: self.$showModal) {
PlaylistSelectorSUI(showModal: self.$showModal).environment(\.managedObjectContext, self.managedObjectContext).onDisappear {
self.navigationButtonID = UUID()
}.background(Color(Appearance.darkBlueColor))
}
Expand Down Expand Up @@ -171,7 +171,7 @@ func randomMMD() -> MMD {
var st = PlaylistStore(viewModel: Playlists.Select.ViewModel(playlistName: "foo", shuffle: false, modules: [randomMMD(), randomMMD(), randomMMD()])
)

struct Playlist_Preview: PreviewProvider {
struct PlaylistPreview: PreviewProvider {
static var previews: some View {
Group {
NavigationView {
Expand Down
6 changes: 3 additions & 3 deletions 4champ/Scenes/Playlists/SelectPlaylistView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct PlaylistCell: View {
}

struct PlaylistSelectorSUI: View {
@Binding var show_modal: Bool
@Binding var showModal: Bool
@State private var listId = UUID()
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(entity: Playlist.entity(),
Expand Down Expand Up @@ -126,7 +126,7 @@ struct PlaylistSelectorSUI: View {
if pl.plName != "radioList" {
PlaylistCell(pl: pl).contentShape(Rectangle()).onTapGesture {
moduleStorage.currentPlaylist = pl
self.show_modal.toggle()
self.showModal.toggle()
}
.onLongPressGesture {
self.edit(pl: pl)
Expand All @@ -136,7 +136,7 @@ struct PlaylistSelectorSUI: View {
}.contentShape(Rectangle()).id(listId)
}.navigationBarTitle("PlaylistView_Playlists", displayMode: .inline)
.navigationBarItems(leading: Button(action: {
self.show_modal.toggle()
self.showModal.toggle()
}) {
Image(systemName: "xmark").imageScale(.large)
},
Expand Down
Loading

0 comments on commit 86b158f

Please sign in to comment.