Skip to content

Commit

Permalink
Merge pull request #43 from boostcamp-2020/develop
Browse files Browse the repository at this point in the history
2주차 코드 프리징
  • Loading branch information
KYHyeon authored Nov 27, 2020
2 parents acd13c0 + 0e4d3eb commit aa7d0a5
Show file tree
Hide file tree
Showing 32 changed files with 73,812 additions and 154 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/Auto_Reviewer.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

name: "Auto Reviewer"
name: "Reviewer lottery"
on:
pull_request:
types: [opened, ready_for_review]
Expand All @@ -8,8 +7,7 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1.1.0
- name: Auto Reviewer
uses: uesteibar/reviewer-lottery@v1
- uses: actions/checkout@v1
- uses: uesteibar/reviewer-lottery@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions BoostClusteringMaB/.swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
disabled_rules:
- trailing_whitespace
- todo
- shorthand_operator

included:

Expand Down
158 changes: 142 additions & 16 deletions BoostClusteringMaB/BoostClusteringMaB.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1220"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "700A666F25626CF800184916"
BuildableName = "BoostClusteringMaBTests.xctest"
BlueprintName = "BoostClusteringMaBTests"
ReferencedContainer = "container:BoostClusteringMaB.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
47 changes: 0 additions & 47 deletions BoostClusteringMaB/BoostClusteringMaB/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}

// MARK: - Core Data stack

lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "BoostClusteringMaB")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()

// MARK: - Core Data Saving support

func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}

}

Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="1" systemVersion="11A491" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithCloudKit="false" userDefinedModelVersionIdentifier="">
<elements/>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="17511" systemVersion="20B29" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="POI" representedClassName="POI" syncable="YES" codeGenerationType="class">
<attribute name="category" optional="YES" attributeType="String"/>
<attribute name="id" optional="YES" attributeType="String"/>
<attribute name="imageURL" optional="YES" attributeType="String"/>
<attribute name="latitude" optional="YES" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/>
<attribute name="longitude" optional="YES" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/>
<attribute name="name" optional="YES" attributeType="String"/>
</entity>
<elements>
<element name="POI" positionX="-63" positionY="-18" width="128" height="119"/>
</elements>
</model>
59 changes: 59 additions & 0 deletions BoostClusteringMaB/BoostClusteringMaB/CSVParser.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// CSVParser.swift
// BoostClusteringMaB
//
// Created by ParkJaeHyun on 2020/11/24.
//

import Foundation

class CSVParser {
enum CSVParSerError: Error {
case empty
case invalidFileName
case invalidCSVForm
}

var pois = [Place]()

func convertCSVIntoArray(file name: String) throws {
guard let filepath = Bundle.main.path(forResource: name, ofType: "csv"),
let data = try? String(contentsOfFile: filepath) else {
throw CSVParSerError.invalidFileName
}

var rows = data.components(separatedBy: "\n")

rows.removeFirst()

for row in rows {
let columns = row.components(separatedBy: ",")

if columns.count == 6 {
let id = columns[0]
let name = columns[1]
let category = columns[2]
let x = columns[3]
let y = columns[4]
let imageURL = columns[5]

let place = Place(id: id, name: name, x: x, y: y, imageURL: imageURL, category: category)
pois.append(place)
} else {
throw CSVParSerError.invalidCSVForm
}
}
}

func add(to coreDataManager: CoreDataManager) throws {
guard !pois.isEmpty else {
throw CSVParSerError.empty
}

try pois.forEach({ place in
try coreDataManager.add(place: place, completion: nil)
})

try coreDataManager.save()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// CoreDataContainer.swift
// BoostClusteringMaB
//
// Created by 현기엽 on 2020/11/23.
//

import CoreData

final class CoreDataContainer {
static var shared: CoreDataContainer = CoreDataContainer()
private let containerName = "BoostClusteringMaB"

var mainContext: NSManagedObjectContext {
return persistentContainer.viewContext
}

// MARK: - Core Data stack
private lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: containerName)
container.loadPersistentStores(completionHandler: { (_, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()

// MARK: - Core Data Saving support
func saveContext() {
let context = persistentContainer.viewContext
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
111 changes: 111 additions & 0 deletions BoostClusteringMaB/BoostClusteringMaB/CoreData/CoreDataLayer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//
// CoreDataLayer.swift
// BoostClusteringMaB
//
// Created by 현기엽 on 2020/11/23.
//

import CoreData

protocol CoreDataManager {
func add(place: Place, completion handler: (() -> Void)?) throws
func fetch(sorted: Bool) throws -> [POI]
func fetch(by classification: String, sorted: Bool) throws -> [POI]
func fetch(southWest: LatLng, northEast: LatLng, sorted: Bool) throws -> [POI]
func remove(poi: POI) throws
func removeAll() throws
func save() throws
}

final class CoreDataLayer: CoreDataManager {
enum CoreDataError: Error {
case invalidCoordinate
case saveError(String)
}

private lazy var childContext: NSManagedObjectContext = {
let childContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)

childContext.parent = CoreDataContainer.shared.mainContext
return childContext
}()

func add(place: Place, completion handler: (() -> Void)? = nil) throws {
guard let latitude = Double(place.y),
let longitude = Double(place.x) else {
throw CoreDataError.invalidCoordinate
}

childContext.perform { [weak self] in
guard let self = self else {
return
}
let poi = POI(context: self.childContext)
poi.id = place.id
poi.category = place.category
poi.imageURL = place.imageURL
poi.latitude = latitude
poi.longitude = longitude
poi.name = place.name
handler?()
}
}

func fetch(sorted: Bool = true) throws -> [POI] {
let request: NSFetchRequest = POI.fetchRequest()
request.sortDescriptors = makeSortDescription(sorted: sorted)

return try childContext.fetch(request)
}

func fetch(by classification: String, sorted: Bool = true) throws -> [POI] {
let request: NSFetchRequest = POI.fetchRequest()
request.predicate = NSPredicate(format: "category == %@", classification)
request.sortDescriptors = makeSortDescription(sorted: sorted)

let pois = try childContext.fetch(request)

return pois
}

func fetch(southWest: LatLng, northEast: LatLng, sorted: Bool = true) throws -> [POI] {
guard northEast.lat > southWest.lat,
northEast.lng > southWest.lng else {
throw CoreDataError.invalidCoordinate
}

let latitudePredicate = NSPredicate(format: "latitude BETWEEN {%@, %@}",
argumentArray: [southWest.lat, northEast.lat])
let longitudePredicate = NSPredicate(format: "longitude BETWEEN {%@, %@}",
argumentArray: [southWest.lng, northEast.lng])
let predicate = NSCompoundPredicate(type: .and, subpredicates: [latitudePredicate, longitudePredicate])

let request: NSFetchRequest = POI.fetchRequest()
request.predicate = predicate
request.sortDescriptors = makeSortDescription(sorted: sorted)

return try childContext.fetch(request)
}

private func makeSortDescription(sorted: Bool) -> [NSSortDescriptor]? {
let latitudeSort = NSSortDescriptor(key: "latitude", ascending: true)
let longitudeSort = NSSortDescriptor(key: "longitude", ascending: true)

return sorted ? [latitudeSort, longitudeSort] : nil
}

func remove(poi: POI) {
childContext.delete(poi)
}

func removeAll() throws {
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "POI")
let removeAll = NSBatchDeleteRequest(fetchRequest: request)
try childContext.execute(removeAll)
}

func save() throws {
try childContext.save()
CoreDataContainer.shared.saveContext()
}
}
Loading

0 comments on commit aa7d0a5

Please sign in to comment.