Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion XcodeCleaner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
9DCE1C0B24B582D200E49E6D /* CoreDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9DCE1C0A24B582D200E49E6D /* CoreDataManager.swift */; };
9DD3BA7024B4639400EDA308 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9DD3BA6F24B4639400EDA308 /* AppDelegate.swift */; };
9DD3BA7224B4639400EDA308 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9DD3BA7124B4639400EDA308 /* ContentView.swift */; };
9DD3BA7524B4639400EDA308 /* XcodeCleaner.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 9DD3BA7324B4639400EDA308 /* XcodeCleaner.xcdatamodeld */; };
Expand Down Expand Up @@ -46,9 +47,11 @@
9DD3BAD424B47A1E00EDA308 /* PieChartItemModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9DD3BAD024B47A1E00EDA308 /* PieChartItemModel.swift */; };
9DD3BAD524B47A1E00EDA308 /* PieChartSubSliceModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9DD3BAD124B47A1E00EDA308 /* PieChartSubSliceModel.swift */; };
9DD3BAD824B47A4800EDA308 /* PieChartAnySliceProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9DD3BAD724B47A4800EDA308 /* PieChartAnySliceProtocol.swift */; };
9DDFA81D24B58F47008C2C2A /* DateManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9DDFA81C24B58F47008C2C2A /* DateManager.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
9DCE1C0A24B582D200E49E6D /* CoreDataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataManager.swift; sourceTree = "<group>"; };
9DD3BA6C24B4639400EDA308 /* XcodeCleaner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = XcodeCleaner.app; sourceTree = BUILT_PRODUCTS_DIR; };
9DD3BA6F24B4639400EDA308 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
9DD3BA7124B4639400EDA308 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -91,6 +94,7 @@
9DD3BAD024B47A1E00EDA308 /* PieChartItemModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PieChartItemModel.swift; sourceTree = "<group>"; };
9DD3BAD124B47A1E00EDA308 /* PieChartSubSliceModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PieChartSubSliceModel.swift; sourceTree = "<group>"; };
9DD3BAD724B47A4800EDA308 /* PieChartAnySliceProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PieChartAnySliceProtocol.swift; sourceTree = "<group>"; };
9DDFA81C24B58F47008C2C2A /* DateManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateManager.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -126,8 +130,9 @@
isa = PBXGroup;
children = (
9DD3BA6F24B4639400EDA308 /* AppDelegate.swift */,
9DD3BA8D24B478AE00EDA308 /* DirectoryManager.swift */,
9DD3BA8F24B478B600EDA308 /* DirectoryType.swift */,
9DD3BA8D24B478AE00EDA308 /* DirectoryManager.swift */,
9DCE1C0A24B582D200E49E6D /* CoreDataManager.swift */,
9DD3BA9624B478CC00EDA308 /* Views */,
9DD3BA9124B478C100EDA308 /* Models */,
9DD3BAA924B4795A00EDA308 /* ViewModels */,
Expand Down Expand Up @@ -218,6 +223,7 @@
isa = PBXGroup;
children = (
9DD3BABC24B479BF00EDA308 /* BytesToStringFormatter.swift */,
9DDFA81C24B58F47008C2C2A /* DateManager.swift */,
);
path = Helpers;
sourceTree = "<group>";
Expand Down Expand Up @@ -370,6 +376,7 @@
9DD3BAA424B4794300EDA308 /* DividerButtonView.swift in Sources */,
9DD3BA7024B4639400EDA308 /* AppDelegate.swift in Sources */,
9DD3BAA024B4791900EDA308 /* DirectoryListView.swift in Sources */,
9DDFA81D24B58F47008C2C2A /* DateManager.swift in Sources */,
9DD3BAAF24B4796C00EDA308 /* PieChartViewModel.swift in Sources */,
9DD3BAD224B47A1E00EDA308 /* PieChartSliceModel.swift in Sources */,
9DD3BAB824B4798E00EDA308 /* PieChartViewModelProtocol.swift in Sources */,
Expand All @@ -378,6 +385,7 @@
9DD3BAD324B47A1E00EDA308 /* PieChartObservableItemsModel.swift in Sources */,
9DD3BA7524B4639400EDA308 /* XcodeCleaner.xcdatamodeld in Sources */,
9DD3BAB724B4798E00EDA308 /* DirectoryListViewModelProtocol.swift in Sources */,
9DCE1C0B24B582D200E49E6D /* CoreDataManager.swift in Sources */,
9DD3BAD824B47A4800EDA308 /* PieChartAnySliceProtocol.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
76 changes: 76 additions & 0 deletions XcodeCleaner/CoreDataManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// CoreDataManager.swift
// XcodeCleaner
//
// Created by Kirill Pustovalov on 08.07.2020.
// Copyright © 2020 Kirill Pustovalov. All rights reserved.
//

import AppKit
import CoreData

struct CoreDataManager {
static let shared = CoreDataManager()

func getStatistic() -> StatisticModel {
var statisticModel = StatisticModel()
fetchStatisticIn(statisticModel: &statisticModel)

return statisticModel
}
func saveStatistic(statistic: StatisticModel) {
let newTotalCleanedValue = statistic.totalCleaned
let newDate = statistic.lastTimeCleaned

saveStatistic(newValue: newTotalCleanedValue, newDate: newDate)
}
private func fetchStatisticIn(statisticModel: inout StatisticModel) {
guard let appDelegate = NSApplication.shared.delegate as? AppDelegate else { return }

let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "StatisticEntity")

do {
let fetchRequestResults = try managedContext.fetch(fetchRequest)

let totalCleaned = fetchRequestResults.first?.value(forKey: "totalSize") as? Int64
let date = fetchRequestResults.first?.value(forKey: "date") as? Date

let modelFromFetchRequest = StatisticModel(totalCleaned: totalCleaned, lastTimeCleaned: date)
statisticModel = modelFromFetchRequest

} catch {
print("could not fetch statistic \(error.localizedDescription)")
}
}
private func saveStatistic(newValue: Int64?, newDate: Date?) {
guard let appDelegate = NSApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "StatisticEntity", in: managedContext)!

let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "StatisticEntity")

do {
let fetchRequestResults = try managedContext.fetch(fetchRequest)
if fetchRequestResults.count == 0 {
let statistic = NSManagedObject(entity: entity, insertInto: managedContext)

statistic.setValue(newValue, forKeyPath: "totalSize")
statistic.setValue(newDate, forKeyPath: "date")
} else {
let previousStatistic = fetchRequestResults.first!

let previousTotalSizeValue = previousStatistic.value(forKey: "totalSize") as! Int64
let value = (newValue ?? 0) + previousTotalSizeValue

previousStatistic.setValue(newDate, forKey: "date")
previousStatistic.setValue(value, forKey: "totalSize")
}

try managedContext.save()
} catch {
print("Error, could not save statistic \(error.localizedDescription)")
}
}
}

21 changes: 21 additions & 0 deletions XcodeCleaner/Helpers/DateManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// DateManager.swift
// XcodeCleaner
//
// Created by Kirill Pustovalov on 08.07.2020.
// Copyright © 2020 Kirill Pustovalov. All rights reserved.
//

import Foundation

struct DateManager {
static func getCurrentDate() -> Date {
Date()
}
static func getStringDate(date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
return dateFormatter.string(from: date)
}
}
6 changes: 4 additions & 2 deletions XcodeCleaner/Protocols/StatisticViewModelProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import Foundation

protocol StatisticViewModelProtocol {
var totalCleaned: Int64? { get set }
var lastTimeCleaned: Date? { get set }
var statistic: StatisticModel { get set }

func getLastDate() -> String
func getTotalSize() -> String
}
1 change: 1 addition & 0 deletions XcodeCleaner/Protocols/ViewModelProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ protocol ViewModelProtocol {
func getViewModelForItemList(forType type: DirectoryType) -> DirectoryListViewModelProtocol
func cleanBeforeScan()
func getViewModelForPieChart() -> PieChartViewModelProtocol
func getViewModelForStatistic() -> StatisticViewModelProtocol

func startClean()
}
14 changes: 11 additions & 3 deletions XcodeCleaner/ViewModels/StatisticViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@

import Foundation

class StatisticViewModel: StatisticViewModelProtocol {
var totalCleaned: Int64? = 0
var lastTimeCleaned: Date? = Date()
struct StatisticViewModel: StatisticViewModelProtocol {
var statistic: StatisticModel

func getLastDate() -> String {
guard let date = statistic.lastTimeCleaned else { return "none" }
return DateManager.getStringDate(date: date)
}
func getTotalSize() -> String {
guard let size = statistic.totalCleaned else { return "none" }
return BytesToStringFormatter.format(size: size)
}
}
11 changes: 10 additions & 1 deletion XcodeCleaner/ViewModels/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,21 @@ class ViewModel: ObservableObject, ViewModelProtocol {
self.directoryManager.cleanDirectory(forType: .archives)

DispatchQueue.main.async {
print(self.totalSize)
let statistic = StatisticModel(totalCleaned: self.totalSize, lastTimeCleaned: DateManager.getCurrentDate())

CoreDataManager.shared.saveStatistic(statistic: statistic)

self.isAlertPresented.toggle()
self.objectWillChange.send()

self.isReadyToBeCleaned.toggle()
}
}
}
func getViewModelForStatistic() -> StatisticViewModelProtocol {
let statistic = CoreDataManager.shared.getStatistic()
let viewModel = StatisticViewModel(statistic: statistic)

return viewModel
}
}
2 changes: 1 addition & 1 deletion XcodeCleaner/Views/FooterViews/FooterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct FooterView: View {
.frame(width: 100)
.lineLimit(1)

StatisticView()
StatisticView(viewModel: viewModel.getViewModelForStatistic())
.padding(10)
}
}
Expand Down
10 changes: 4 additions & 6 deletions XcodeCleaner/Views/FooterViews/StatisticView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
import SwiftUI

struct StatisticView: View {
var totalCleaned: String = String(format: "%.2f", 22.7800)
var lastTimeCleaned: String?

var viewModel: StatisticViewModelProtocol
var body: some View {
VStack(alignment: .trailing) {
Text("Total cleaned: \(totalCleaned) GB")
Text("Last time cleaned: \(lastTimeCleaned ?? "none")")
Text("Total cleaned: \(viewModel.getTotalSize())")
Text("Last cleanup: \(viewModel.getLastDate())")
Text("Github: IrelDev")
}
.font(.footnote)
Expand All @@ -24,6 +22,6 @@ struct StatisticView: View {

struct StatisticView_Previews: PreviewProvider {
static var previews: some View {
StatisticView()
StatisticView(viewModel: StatisticViewModel(statistic: StatisticModel(totalCleaned: 250000, lastTimeCleaned: Date())))
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?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="16119" systemVersion="20A4299v" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="StatisticEntity" representedClassName="StatisticEntity" syncable="YES" codeGenerationType="class">
<attribute name="date" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="totalSize" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
</entity>
<elements>
<element name="StatisticEntity" positionX="-63" positionY="-18" width="128" height="59"/>
</elements>
</model>