Skip to content

Instantly share code, notes, and snippets.

View tifoaudii's full-sized avatar
🎯
Focusing

Tifo Audi A.P tifoaudii

🎯
Focusing
View GitHub Profile
@tifoaudii
tifoaudii / Anchor.swift
Created July 27, 2019 04:30
Create custom extension to UIView instance to implement constraint easily
//
// Anchor.swift
// AppStore
//
// Created by Tifo Audi Alif Putra on 27/07/19.
// Copyright © 2019 BCC FILKOM. All rights reserved.
//
import UIKit
@tifoaudii
tifoaudii / Node.swift
Created July 30, 2019 06:17
Create a class represent a Node object that consist of value and next reference
class Node<Value> {
var value: Value
var next: Node<Value>?
init(value: Value, next: Node<Value>? = nil ) {
self.value = value
self.next = next
}
}
@tifoaudii
tifoaudii / Linkedlist.swift
Created July 30, 2019 06:37
Create a linkedlist class that consist of more than one node objects
public struct Linkedlist<Value> {
public var head: Node<Value>?
public var tail: Node<Value>?
public var isEmpty: Bool {
return head == nil
}
init() {}
public mutating func push(_ value: Value) {
head = Node(value: value, next: head)
func getCharacterFromString(str: String) -> Dictionary<Character,Int> {
var stringDictionary = Dictionary<Character, Int>()
for (_,char) in str.enumerated() {
if !stringDictionary.keys.contains(char) {
stringDictionary[char] = 1
} else {
stringDictionary[char]! += 1
}
fileprivate func createOnGoingMovieSection() -> NSCollectionLayoutSection {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalWidth(2/3))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalWidth(1.0))
let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitem: item, count: 3)
group.contentInsets = .init(top: 5, leading: 0, bottom: 5, trailing: 20)
let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(44))
let sectionHeader = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: MovieViewController.sectionHeaderElementKind, alignment: .top)
class Node<Value> {
var value: Value
var next: Node<Value>?
init(value: Value, next: Node<Value>? = nil ) {
self.value = value
self.next = next
}
}
let layout = UICollectionViewCompositionalLayout { (sectionIndex: Int, _ ) -> NSCollectionLayoutSection? in
let movieSections = MovieSection.allCases[sectionIndex]
switch movieSections {
case .topRated: return self.createTopRatedMovieSection()
case .onGoing: return self.createOnGoingMovieSection()
case .upcoming: return self.createUpcomingMovieSection()
case .popular: return self.createPopularMovieSection()
}
}
return layout
movieDataSource = UICollectionViewDiffableDataSource<MovieSection, Movie>(collectionView: movieCollectionView, cellProvider: { (collectionView, indexPath, movie) -> UICollectionViewCell? in
let movieSections = MovieSection.allCases[indexPath.section]
switch movieSections {
case .topRated:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TopRatedMovieCell.cellIdentifier, for: indexPath) as? TopRatedMovieCell else {
return TopRatedMovieCell()
}
cell.movie = movie
return cell
case .onGoing:
var snapshot = NSDiffableDataSourceSnapshot<MovieSection, Movie>()
snapshot.appendSections([MovieSection.topRated])
snapshot.appendItems(viewModel.topRatedMovies)
snapshot.appendSections([MovieSection.onGoing])
snapshot.appendItems(viewModel.ongoingMovies)
snapshot.appendSections([MovieSection.upcoming])
snapshot.appendItems(viewModel.upcomingMovies)
//1
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalWidth(1.0))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
//2
let groupSize = NSCollectionLayoutSize(widthDimension: .absolute(140), heightDimension: .absolute(220))
let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitem: item, count: 1)
group.contentInsets = .init(top: 5, leading: 5, bottom: 5, trailing: 5)
//3