Skip to content

Instantly share code, notes, and snippets.

View alenm's full-sized avatar

Alen Mujkic alenm

View GitHub Profile
@alenm
alenm / gist:1649365
Created January 20, 2012 20:20
Toto Archive Group_by
# ------------------------------------------------------------
# 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>
@alenm
alenm / emberjs-lifecycle-hooks.js
Last active March 25, 2017 22:18
Emberjs Component Lifecycle
// On Intial Render
init() {
this._super(...arguments);
console.log('init');
},
didReceiveAttrs() {
this._super(...arguments);
console.log('didReceiveAttrs');
},
@alenm
alenm / splitter.swift
Created February 5, 2021 13:28
Coding problem posted on twitter. The twitter thread has interesting solutions in different languages. This one is in 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 / multipleSheetsInSwiftUI.swift
Last active April 15, 2021 00:55
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.
import SwiftUI
import Combine
// 01 Enum
enum Activesheet: Identifiable {
case twitter
case picker
case web
var id: String {
@alenm
alenm / ForEachArrayOfTupleExample.swift
Created February 14, 2021 23:17
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
// 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 / ForEachEnumerated.swift
Created February 23, 2021 15:57
ForEach and with an index using SwiftUI
// 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 / SettingBackgroundColor.swift
Created March 2, 2021 15:05
Setting background color when top edge is white
/*
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 / twocolumns.swift
Last active March 18, 2021 01:04
Equal Width Two Column SwiftUI
// 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)