Skip to content

Instantly share code, notes, and snippets.

Avatar

Alen Mujkic alenm

View GitHub Profile
@alenm
alenm / twocolumns.swift
Last active Mar 18, 2021
Equal Width Two Column SwiftUI
View twocolumns.swift
// Source: https://noahgilmore.com/blog/swiftui-two-columns-equal-width/
struct ContentView: View {
var body: some View {
HStack(alignment: .top, spacing: 0) {
Rectangle()
.fill(Color.blue)
.frame(minWidth: 0, maxWidth: .infinity)
Rectangle()
.fill(Color.red)
@alenm
alenm / SettingBackgroundColor.swift
Created Mar 2, 2021
Setting background color when top edge is white
View SettingBackgroundColor.swift
/*
Scenario: Sometimes you want the entire background color to be filled in your SwiftUI
- One solution is to make the color `edgesIgnoringSafeArea(.all)`
- This is not obvious but it seems to work for now
*/
struct Example: View {
var body: some View {
@alenm
alenm / ForEachEnumerated.swift
Created Feb 23, 2021
ForEach and with an index using SwiftUI
View ForEachEnumerated.swift
// 01 Example with struct
// Use ForEach to provide views based on a `RandomAccessCollection` of some data type.
// see documentation: https://developer.apple.com/documentation/swiftui/foreach
// I want the index you can do the following...
// You use `results.enumerated().map({$0}` which takes your results and returns it into an array
// I have seen examples where you could do it like `Array(results.enumerated())`
// NOTE:
// The collection’s elements must conform to Identifiable or you need to provide an id parameter to the ForEach initializer.
@alenm
alenm / ForEachArrayOfTupleExample.swift
Created Feb 14, 2021
ForEach over an Array of Tuples. Note in order for this to work properly you need to have some unique id. In this example that is in the Integer. If it's not unique you will run into issues
View ForEachArrayOfTupleExample.swift
// Example A
// This example it works fine because `id:\.0` is an Int and is unique
struct Example: View {
var tupleListExample: [(Int, String)] = [
(1, "A"),
(2, "B"),
(3, "C"),
@alenm
alenm / multipleSheetsInSwiftUI.swift
Last active Apr 15, 2021
Having multiple sheets within one SwiftUI view can happen. The `sheet(item:content:)` takes an item: Binding<Identifiable> which means in the latest swift you can use an Enum and switch on it in the content closure.
View multipleSheetsInSwiftUI.swift
import SwiftUI
import Combine
// 01 Enum
enum Activesheet: Identifiable {
case twitter
case picker
case web
var id: String {
@alenm
alenm / splitter.swift
Created Feb 5, 2021
Coding problem posted on twitter. The twitter thread has interesting solutions in different languages. This one is in swift
View splitter.swift
// Original Tweet: https://twitter.com/Al_Grigor/status/1357028887209902088
// input : "aaaabbbcca"
// output: [("a", 4), ("b", 3), ("c", 2), ("a", 1)]
func splitter(str: String) -> [(String, Int)] {
// 01 This will give you an Array of letters
let letters = str.map { String($0) }
@alenm
alenm / emberjs-lifecycle-hooks.js
Last active Mar 25, 2017
Emberjs Component Lifecycle
View emberjs-lifecycle-hooks.js
// On Intial Render
init() {
this._super(...arguments);
console.log('init');
},
didReceiveAttrs() {
this._super(...arguments);
console.log('didReceiveAttrs');
},
@alenm
alenm / gist:1649365
Created Jan 20, 2012
Toto Archive Group_by
View gist:1649365
# ------------------------------------------------------------
# The Toto Archive page has a collection called 'archive'
# We use Ruby's group_by method to enumerate over the collection by :year
# And then iterate through the posts
# ------------------------------------------------------------
<% archives.group_by { | archive | archive[:date].year }.each do | year, posts | %>
<%= year %>
<% for post in posts %>
<a href="<%= post.path %>"><%= post.title %></a>