sábado, 2 de agosto de 2014

[Quicktips] iOS - Follow Keyboard Animation Behaviour

On getting back to iOS programming I restarted to check into interesting subjects, like the right way of creating view controllers, behaviours and more.

One thing I had to create these last days was some code for a text field to follow the keyboard open/close animation. Then it come to me that we were going to use that behaviour was needed in more places and that it didn't belong to the view controller neither to the view using it.

So in order to achieve that I decided to create a behaviour class which features are:
  • To receive a target view
  • Register under keyboard events
  • Animate the target view to follow the keyboard open/close animation
  • Keep target view before (on top) of keyboard, meaning that target will be hidden when keyboard is closed
This set of features don't break the single responsibility principle since it is responsible only for controlling the animation behaviour of a view following the iOS keyboard.

To achieve our desire the class must subscribe to keyboard notifications and follow keyboard info dictionary in order to animate the same way the keyboard does.



In 1. we retrieve the type of animation, the duration and the final position of the keyboard. The problem with the animation type is that Apple uses a internal value which only works directly with the old animation style.
To use it with animationWithDuration methods we would have to shift the value, this isn't easy to understand and read later so is preferable to keep the old style in this case.

In 2. we use the animation calls on UIView to prepare animation with keyboard info.

In 3. we animate using the keyboard final position, in my case I'm positioning the view right above keyboard, and when dismissed the view will be hidden outside of the screen in the bottom. Also we have to commitAnimations so evertyhing will work properly.

That is all we need to create a behaviour that works with any view.

The class and the demo are available on github.



segunda-feira, 14 de abril de 2014

A few things I've learned with Ruby and Rails

The past 3 months have been a heavy storm with hail rain and everything, still there is always a bright side on life events.

During my last job at Code Miner I had the pleasure to work with:
  • Ruby on Rails (and the huge amount of things that come along with it)
  • Good professionals
This short time lead me to decide to share my small knowledge with the others that may struggle, like I did at some points. 

So first things first: Ruby on Rails. This pack made it appearance several times in all those years and deserves a cool place in things you would like to learn or at least should learn.

The Ruby and the Rails community, along with my teammates, taught me lots of new skills and improved old ones, so let's get to the sweeties.

1 - Debug, debug... and.... did I said debug?

The most important thing to learn when programming in any language is how to debug your code. Ruby has this quite good Gem to help you: pry

All you have to do to use it is requiring: require 'pry' then you add binding.pry anywhere on your code and ta-da! when you run your app it will stop at that command and from this time on there is access to code variables, next steps and a few more useful things (take a look at github)

2 - My precious! My gems, gollum gollum

You probably already heard that cliche line: Don't reinvent the wheel.

Ruby has those precious libraries of working software called Gems. 
Need something to debug your code? Bam: Pry gem. 
Need something to parse xml? Boom: Nokogiri gem.
Need some background processing? Pow: Sidekiq gem.
Need your sidekiq jobs to be scheduled? Bang: Sidetiq gem.

There is a huge sort of gems sitting there just waiting to help in solving pesky problems so you can focus on creating an awesome app. Oh and there is a lot of them dedicated directly to Rails like the famous ActiveAdmin.

Ruby Toolbox and Ruby Gems may help you finding the ones you need.

3 - Did you saw that bug?

Sadly Test Driven Development (TDD) isn't used as much as it should be and  I can understand that. 

Most of  the non TDD teams have lots of excuses but almost none of them spent or want to spend enough time using it, otherwise they would see how beautiful and useful it is.

TDD has proven worthy to me after much struggling still with objective-c in iOS world, which community is not so visibly into it. 

Here is 4 reasons why I love it:

1. Bugs reduction and faster bug finding:

The first happens because you're thinking ahead, you need a way to check your code output even before it exists. The later happens because when changes are made you know what is the desired output.

2. Improves code readability and quality

It's hard to believe, but you tend to be more careful with what you're writing since you and others will be getting back to the tests a thousand of times. 

Since you're being more careful with the code it's architecture will receive a boost, be prepared to read tons of articles/book on the subject.

3.  Changes the way you see refactoring

You start to hate when its difficult to test your code, and that will be more often than you think. Refactoring begins to be more clear and this feeling fades away  before you know.

Besides with a good testing suite refactoring  will be easier and delightful.

4. Working in teams is nicer

Hey have you never hated that hard to use friend's code? Now if you have tests you probably will just have to run them a few times and there you go. 

4. Everyone loves free candies, but do you give them too?

This one is short, we all love to use open source projects (yep Gems for example), but most of us don't have the guts to contribute.

Hey no one will shove you off if you're really helping, just give it a try. Oh and those beautiful projects of yours, release them properly as Open Source too, who knows, maybe you get some katas for that attitude.

I didn't learned this here with Ruby, but this feeling is stronger amongst its community.

This absolutely isn't all the things I wanted to share, there is more, but I think this is enough to my purpose, to give you a glimpse of the good things from the many which will improve yourself as a programmer when you enter, even for a moment, in the Ruby and Rails world.

By the way, I can't address how fulfilling is working with a great team, so if you have the chance of doing this please just go for it.

sexta-feira, 14 de fevereiro de 2014

[Ruby] Using gzip with string

Gzipping a string is quite straight forward once you know how to do it, so to help myself and others in the future I'm making a note here.



I'm not doing anything new or fancy here but I guess someone have to let some bread crumbles in the way.

terça-feira, 27 de agosto de 2013

[QuickTip] SublimeText Build System for Love2D

For those using Love2D as game engine and SublimeText2 as IDE this *.sublime-build can come handy if you want to rapidly generate *.love file for testing. (using command+B in mac for instance)

{ "cmd": ["zip -9 -r --exclude=.love game.love .; ls"], "selector": "source.lua.love", "shell": true }

This will generate a game.love inside your current folder excluding the .love files itself, also will show on SublimeText shell the output of the command.

By the way, if you want to run .love in the sequence just use the following

{ "cmd": ["zip -9 -r --exclude=.love game.love .; ls; /Applications/love.app/Contents/MacOS/love game.love"], "selector": "source.lua.love", "shell": true }
CMD receives a string used in terminal so using: /pathToLoveExecutable name.love; Will call the love executable as you would do in Terminal. 

And that should do the trick ;)

terça-feira, 16 de julho de 2013

Signing your TAG with Git - MacOSX

Recently I was asked to start versioning our master with tags and while doing some research about it I found a relevant blog post from Mike Gerwitz Git Horror Story about signing git commits.

I won't get inside the subject but I think signing at least your tags is a good initial step towards security.

In this guide you will:
- Install GPG (Gnu Privacy Guard)
- Create a signed key
- Create a signed tag
- Push tag to git server

You should already have git installed, of course.

So let's get started.

Installing MacGPG


You should follow MacGPG instructions at MacGPG at Sourceforge

Or if you like tools that solves your problems hidding the neat part use GPG Tools, altough I personally didn't test it.

Creating a signed key

Before we can really use tag command we must create a key, in a similar way that we do when using ssh private keys for GitHub.

In a terminal use:
gpg --gen-key
The terminal should ask you to select your key type like this:
Please select what kind of key you want:
(1) RSA and RSA (default)
(2) DSA and Elgamal
(3) DSA (sign only)
(4) RSA (sign only)
Your selection?
The default selection is prefered in this case, unless you REALLY KNOWS why you would want to choose other options.

Next he asks you to choose a size:
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
I guess 2048 is good enough for me, pick a size you need.

Now it will asks for an expire date, even if you have really strong reasons for not setting an expire date THINK AGAIN.
Please specify how long the key should be valid.
0 = key does not expire
= key expires in n daysw = key expires in n weeksm = key expires in n monthsy = key expires in n years Key is valid for? (0)
On this step you need to use your REAL information: name, email and passphrase. A comment can be inserted also, but isn't required (I recommend using it to differ this key amongst other keys).

You need a user ID to identify your key; the software constructs the user ID from the Real Name, Comment and Email Address in this form: "Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de="">

Type O when you have finished and the terminal will prompt for a passphrase
And you're done for this step, let's create some tags.

Type O when you have finished and the terminal will prompt for a passphrase

And you're done for this step, let's create some tags.

Creating Tags with Git

I won't go in deep details about git tags, so if you want to read some about the subject first this is the right place: Git Basics Tagging

First you should get your key identifier, run the following command

gpg --list-secret-keys
This should print anything similar to:
sec 2048R/8EE30EAB 2013-07-16 [expires: 2015-07-16] uid Name (Comment) <youremail@email.com=""> ssb 2048R/hexvalueB 2013-07-16
Copy the 8EE30EAB from sec (yours probably is different)

Go to your repository folder on terminal and config git to use this key (remove the --global param to apply only for current repository)

git config --global user.signingkey 8EE30EAB
Finally create the tag
git tag -s v1.5 -m 'my signed 1.5 tag'
Show it with
git show v1.5
Push it with
git push origin v1.5
If you have more than one tag you can use
git push origin --tags
And that's it. If you have any problems leave it on comments I'll be glad to help if I can.