Skip to content

Commit

Permalink
userDefaults, garbage button, dragItem.localObject = indexPath
Browse files Browse the repository at this point in the history
  • Loading branch information
BestKora committed Nov 26, 2018
1 parent c9168db commit 2f51313
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 14 deletions.
4 changes: 4 additions & 0 deletions ImageGallery.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
E40146B721AAA30300241E1B /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = E40146B621AAA30300241E1B /* Utilities.swift */; };
E40146B921AAC41B00241E1B /* GalleryTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = E40146B821AAC41B00241E1B /* GalleryTableViewCell.swift */; };
E40146BB21AAC42E00241E1B /* GalleriesTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E40146BA21AAC42E00241E1B /* GalleriesTableViewController.swift */; };
E4CDFF5521AC2CC600988502 /* GarbageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4CDFF5421AC2CC600988502 /* GarbageView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -34,6 +35,7 @@
E40146B621AAA30300241E1B /* Utilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = "<group>"; };
E40146B821AAC41B00241E1B /* GalleryTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GalleryTableViewCell.swift; sourceTree = "<group>"; };
E40146BA21AAC42E00241E1B /* GalleriesTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GalleriesTableViewController.swift; sourceTree = "<group>"; };
E4CDFF5421AC2CC600988502 /* GarbageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GarbageView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -73,6 +75,7 @@
E40146B821AAC41B00241E1B /* GalleryTableViewCell.swift */,
E40146B421AA89B600241E1B /* ImageViewController.swift */,
E40146B021A9A45700241E1B /* ImageGallery.swift */,
E4CDFF5421AC2CC600988502 /* GarbageView.swift */,
E40146A621A9A14E00241E1B /* Info.plist */,
E40146AD21A9A1C100241E1B /* Supporting Files */,
);
Expand Down Expand Up @@ -166,6 +169,7 @@
E40146B121A9A45700241E1B /* ImageGallery.swift in Sources */,
E401469B21A9A14C00241E1B /* AppDelegate.swift in Sources */,
E40146BB21AAC42E00241E1B /* GalleriesTableViewController.swift in Sources */,
E4CDFF5521AC2CC600988502 /* GarbageView.swift in Sources */,
E40146B521AA89B600241E1B /* ImageViewController.swift in Sources */,
E40146B321A9A60300241E1B /* ImageCollectionViewCell.swift in Sources */,
E40146AF21A9A2B000241E1B /* ImageGalleryCollectionViewController.swift in Sources */,
Expand Down
17 changes: 12 additions & 5 deletions ImageGallery/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,18 @@
</connections>
</tableView>
<navigationItem key="navigationItem" id="XT0-KR-2et">
<barButtonItem key="rightBarButtonItem" systemItem="add" id="F6E-oP-NHU">
<connections>
<action selector="newGallery:" destination="V2J-w4-FFF" id="ZEn-7n-gLQ"/>
</connections>
</barButtonItem>
<rightBarButtonItems>
<barButtonItem systemItem="add" id="F6E-oP-NHU">
<connections>
<action selector="newGallery:" destination="V2J-w4-FFF" id="ZEn-7n-gLQ"/>
</connections>
</barButtonItem>
<barButtonItem systemItem="save" id="sxD-f5-pB1">
<connections>
<action selector="save:" destination="V2J-w4-FFF" id="Wnd-Dy-dFg"/>
</connections>
</barButtonItem>
</rightBarButtonItems>
</navigationItem>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="nGh-c0-e4l" userLabel="First Responder" sceneMemberID="firstResponder"/>
Expand Down
45 changes: 42 additions & 3 deletions ImageGallery/GalleriesTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ class GalleriesTableViewController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()
imageGalleries = [[ImageGallery(name: "Galery 1")]]
if let imageGalleriesFROM = imageGalleriesJSON {
imageGalleries = imageGalleriesFROM
} else {
imageGalleries =
[[ImageGallery(name: "Gallery 1")]]
}
/* imageGalleries =
[
[ImageGallery(name: "Galery 1"),
Expand Down Expand Up @@ -53,6 +58,30 @@ class GalleriesTableViewController: UITableViewController {
return imageGalleries[indexPath.section][indexPath.row].name
}

let defaults = UserDefaults.standard
var imageGalleriesJSON : [[ImageGallery]]? {
get {
if let savedGalleries =
defaults.object(forKey: "SavedGalleries") as? Data {
let decoder = JSONDecoder()
if let loadedGalleries =
try? decoder.decode([[ImageGallery]].self,
from: savedGalleries) {
return loadedGalleries
}
}
return nil
}
set {
if newValue != nil {
let encoder = JSONEncoder()
if let json = try? encoder.encode(newValue!) {
defaults.set(json, forKey: "SavedGalleries")
}
}
}
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let currentIndex = lastIndexPath != nil
Expand All @@ -61,6 +90,15 @@ class GalleriesTableViewController: UITableViewController {
selectRow(at: currentIndex)
}

@IBAction func save(_ sender: UIBarButtonItem) {
imageGalleriesJSON = imageGalleries
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
imageGalleriesJSON = imageGalleries
}

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if splitViewController?.preferredDisplayMode != .primaryOverlay {
Expand Down Expand Up @@ -222,10 +260,11 @@ class GalleriesTableViewController: UITableViewController {
numberOfRowsInSection: indexPath.section) >= indexPath.row {
Timer.scheduledTimer(withTimeInterval: timeDelay,
repeats: false,
block: { (timer) in
self.tableView.selectRow(at: indexPath,
block: {[weak self](timer) in
self?.tableView.selectRow(at: indexPath,
animated: false,
scrollPosition: .none)
self?.showCollection(at: indexPath)
})
}
}
Expand Down
138 changes: 138 additions & 0 deletions ImageGallery/GarbageView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//
// GarbageView.swift
// ImageGallery
//
// Created by Tatiana Kornilova on 15/06/2018.
// Copyright © 2018 Stanford University. All rights reserved.
//

import UIKit

class GarbageView: UIView, UIDropInteractionDelegate {

// MARK: - Initialization

override init(frame: CGRect) {
super.init(frame: frame)
setup()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

var garbageViewDidChanged: (() -> Void)?

private func setup() {
let dropInteraction = UIDropInteraction(delegate: self)
addInteraction(dropInteraction)

backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0)

let trashImage = UIImage.imageFromSystemBarButton(.trash)
let myButton = UIButton()
myButton.setImage(trashImage, for: .normal)
self.addSubview(myButton)
}

override func layoutSubviews() {
super.layoutSubviews()
if self.subviews.count > 0 {
self.subviews[0].frame = CGRect(x: bounds.width - bounds.height,
y: 0,
width: bounds.height,
height: bounds.height)
}
}

// MARK: - UIDropInteractionDelegate

func dropInteraction(_ interaction: UIDropInteraction,
canHandle session: UIDropSession) -> Bool {
return session.canLoadObjects(ofClass: UIImage.self)
}

func dropInteraction(_ interaction: UIDropInteraction,
sessionDidUpdate session: UIDropSession) -> UIDropProposal {
if session.localDragSession != nil {
return UIDropProposal(operation: .copy)
} else {
return UIDropProposal(operation: .forbidden)
}
}

func dropInteraction(_ interaction: UIDropInteraction,
previewForDropping item: UIDragItem,
withDefault defaultPreview: UITargetedDragPreview
) -> UITargetedDragPreview? {

let target = UIDragPreviewTarget(
container: self,
center: CGPoint(x: bounds.width - bounds.size.height * 1 / 2,
y: bounds.size.height * 1 / 2),
transform: CGAffineTransform(scaleX: 0.1, y: 0.1)
)
return defaultPreview.retargetedPreview(with: target)
}

func dropInteraction(_ interaction: UIDropInteraction,
performDrop session: UIDropSession) {
session.loadObjects(ofClass: UIImage.self) { providers in
if let collection = (session.localDragSession?.localContext as?
UICollectionView),
let images = (collection.dataSource as? ImageGalleryCollectionViewController)?.imageGallery.images,
let items = session.localDragSession?.items {

var indexPaths = [IndexPath] ()
var indexes = [Int]()

for item in items {
if let indexPath = item.localObject as? IndexPath {
let index = indexPath.item
indexes += [index]
indexPaths += [indexPath]
}
}
collection.performBatchUpdates({
collection.deleteItems(at: indexPaths)
(collection.dataSource as? ImageGalleryCollectionViewController)?.imageGallery.images
= images
.enumerated()
.filter { !indexes.contains($0.offset) }
.map { $0.element }
})
self.garbageViewDidChanged?()
}
}
}
}

extension UIImage{

class func imageFromSystemBarButton(_ systemItem: UIBarButtonItem.SystemItem,
renderingMode:UIImage.RenderingMode = .automatic)-> UIImage {

let tempItem = UIBarButtonItem(barButtonSystemItem: systemItem,
target: nil, action: nil)

// add to toolbar and render it
let bar = UIToolbar()
bar.setItems([tempItem], animated: false)
bar.snapshotView(afterScreenUpdates: true)

// got image from real uibutton
let itemView = tempItem.value(forKey: "view") as! UIView

for view in itemView.subviews {
if view is UIButton {
let button = view as! UIButton
let image = button.imageView!.image!
image.withRenderingMode(renderingMode)
return image
}
}

return UIImage()
}
}
4 changes: 2 additions & 2 deletions ImageGallery/ImageGallery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import Foundation

struct ImageModel{
struct ImageModel: Codable{
var url: URL
var aspectRatio: Double
}

class ImageGallery {
class ImageGallery:Codable {
var name: String
var images = [ImageModel]()

Expand Down
23 changes: 19 additions & 4 deletions ImageGallery/ImageGalleryCollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class ImageGalleryCollectionViewController: UICollectionViewController,
}
}
}
// var imageCollection = [ImageModel]()

// MARK: - Live cycle methods

Expand All @@ -34,15 +33,31 @@ class ImageGalleryCollectionViewController: UICollectionViewController,
target: self,
action: #selector(ImageGalleryCollectionViewController.zoom(_:)))
)
navigationItem.leftBarButtonItem =
splitViewController?.displayModeButtonItem
// navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
flowLayout?.invalidateLayout()
}

var garbageView = GarbageView()

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if let navBounds = navigationController?.navigationBar.bounds {
garbageView.frame = CGRect(
x: navBounds.width*0.6,
y: 0.0,
width: navBounds.width*0.4,
height: navBounds.height)
let barButton = UIBarButtonItem(customView: garbageView)
navigationItem.rightBarButtonItem = barButton
navigationItem.leftBarButtonItem =
splitViewController?.displayModeButtonItem
}
}

// MARK: Vars, Constants

var flowLayout: UICollectionViewFlowLayout? {
Expand Down Expand Up @@ -140,7 +155,7 @@ class ImageGalleryCollectionViewController: UICollectionViewController,
as? ImageCollectionViewCell,
let image = itemCell.imageView.image {
let dragItem = UIDragItem(itemProvider: NSItemProvider(object: image))
dragItem.localObject = imageGallery.images[indexPath.item]
dragItem.localObject = indexPath //imageGallery.images[indexPath.item]
return [dragItem]
} else {
return []
Expand Down

0 comments on commit 2f51313

Please sign in to comment.