Skip to content

Instantly share code, notes, and snippets.

View pitt500's full-sized avatar

Pedro Rojas pitt500

View GitHub Profile
@pitt500
pitt500 / GetNearestDegree.swift
Created May 29, 2020 18:21
Given a clock time in hh:mm format, determine, to the nearest degree, the angle between the hour and the minute hands.
func getNearestDegree(time: String) -> Int {
let array = time.split(separator: ":")
let hours = Int(array[0])!
let minutes = Int(array[1])!
let hourPosition = hours*60 + minutes
let minutesPosition = 12*minutes
let actualPosition = hourPosition - minutesPosition
let degrees = actualPosition/2
public var body: some View {
List {
//1.
ForEach(items.indices, id: \.self) { index in
VStack {
//2.
self.content(self.items[index])
//3.
if self.isLoading && self.isLastItem(index: index) {
public init (items: [Item], offset: Int = 5,
pagination: @escaping (_ completion: (() -> Void)?) -> Void,
@ViewBuilder content: @escaping (_ item: Item) -> Content) {
self.items = items
self.content = content
self.pagination = pagination
self.offset = offset
}
public struct ListPagination<Item: Identifiable, Content: View>: View {
private var items: [Item]
var content: (_ item: Item) -> Content
var pagination: ((() -> Void)?) -> Void
@State var isLoading = false
private var offset: Int
private func isLastItem(index: Int) -> Bool {
index == (items.count - 1)
}
private func isOffsetReached(at index: Int) -> Bool {
index == (items.count - offset)
}
private func itemAppears(at index: Int) {
if isOffsetReached(at: index) {
import SwiftUI
public struct SpinnerView: UIViewRepresentable {
public typealias UIViewType = UIActivityIndicatorView
public init(isAnimating: Binding<Bool>, style: UIActivityIndicatorView.Style) {
self._isAnimating = isAnimating
self.style = style
}
//1
class ItemListViewModel: ObservableObject {
//2
@Published var items: [Int] = []
//3
static private var itemsPerPage = 25
private var start = -ItemListViewModel.itemsPerPage
private var stop = -1
private let maxData = 250
struct TestPaginationView: View {
//1
@ObservedObject var viewModel = ItemListViewModel()
var body: some View {
NavigationView {
//2
ListPagination(items: viewModel.items, offset: 8, pagination: viewModel.getData) { item in
//3
Text("Item #\(item)")
@pitt500
pitt500 / TXTabView.swift
Last active July 6, 2020 16:21
TXTabView is a custom view controller representable to support lazy loading and keeping the screen state for SwiftUI Views without reloading every time.
import SwiftUI
protocol TXTabBarElementView: View {
associatedtype Content
var content: Content { get set }
var item: TXTabItem.Item { get set }
}
public struct TXTabItem: TXTabBarElementView {