Skip to content

Instantly share code, notes, and snippets.

View pitt500's full-sized avatar

Pedro Rojas pitt500

View GitHub Profile
@pitt500
pitt500 / BackgroundGradientView.swift
Created March 27, 2021 23:27
Background with a soft gradient
struct BackgroundGradientView: View {
let color: Color
var body: some View {
ZStack {
Color.black
.ignoresSafeArea()
LinearGradient(
gradient: Gradient(
colors: [
protocol Loggable {
init(filename: String)
func log()
}
class Logger: Loggable {
private var filename = ""
// Required is needed for subclass conforming protocol too.
// Unless class is marked as final
@pitt500
pitt500 / SoccerPlayer.swift
Last active March 17, 2021 13:06
SoccerPlayer code requiered for video "Protocols in Swift Pt 2: Methods, initializers & extensions" https://youtu.be/qiMFIopQJME
protocol Player {
var name: String { get set }
var score: Int { get }
var nickname: String { get }
static var numberOfPlayers: Int { get set }
}
struct SoccerPlayer: Player {
var name: String
@pitt500
pitt500 / noSeparatorLinesInList.swift
Last active March 6, 2021 05:18
Method to hide default separator lines in a SwiftUI's List. You need to call it in the cell (as a modifier), not the list.
// Call this in the cell, for example CellView().noSeparators()
extension View {
@ViewBuilder public func noSeparators(
edgeInsets: EdgeInsets = EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0),
color: Color
) -> some View {
self
.padding(edgeInsets)
.frame(
minWidth: 0,
@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 {
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)")
//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
import SwiftUI
public struct SpinnerView: UIViewRepresentable {
public typealias UIViewType = UIActivityIndicatorView
public init(isAnimating: Binding<Bool>, style: UIActivityIndicatorView.Style) {
self._isAnimating = isAnimating
self.style = style
}
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) {
var pagination: ((() -> Void)?) -> Void
@State var isLoading = false
private var offset: Int