Comment Scene Like Instagram

In my short stint with iOS programming thus far, I found that one of the most difficult and crucial part of the process is deciding which controller to use. Conceivably, you could use a plain UIViewController to design everything but that’s a poor man’s way of going about it. Here’s how I did the comment page for Instagram.

There are a few requirements:

  • Comment box should stick to the bottom
  • Autolayout should be used
  • UITableViewCell should resize accordingly to the amount of text required to fit the cell nicely
  • Bonus: works in landscape and portrait modes. Instagram doesn’t support landscape.

2014-04-24 12.26.36

2014-04-24 12.23.33

 

 

 

 

 

 

General process:

  • Insert a TableView into a UIViewController
  • Add a subview programmatically or via storyboard
  • Set the constraints up correctly
  • Animate the bottom constraint from the comment box to it’s superview as and when the keyboard appears

Some gotchas:

  • You can’t do this using a UITableViewController and then laying a subview into it.
    • The view will move along with the tableview as you scroll which is not the behaviour you want
  • You can’t do this using a UITableViewController and then adding the comment box in the footer
    • The footer will always be below the last cell in the tableview which is not what you want

Code: https://github.com/nychng/AutoSizeTableCell

Advertisements

Buying AmazonGlobal Eligible Products From Singapore

Amazon free shipping to Singapore! Yay~~

Amazon free shipping to Singapore! Yay~~

Singapore recently came under Amazon’s GlobalSaver programme. What this means is that orders above $125 USD will have free shipping. Do note that if you’re spending over $400 SGD, you will probably need to pay taxes on those items.

So how does one go about buying these free shipping goodies?

To check if your product qualifies for the free shipping, use the following steps:

  1. Begin your search here: http://www.amazon.com/International-Shipping-Direct/b?ie=UTF8&node=230659011
  2. On the left hand nav bar, select the specific department e.g. Books or Electronics
  3. This should refresh the page with the left hand nav bar fully expanded. Scroll down until you find AmazonGlobal Eligible
  4. Check the box to filter it again
  5. The product must be sold by Amazon.com LLC and not one of its re-sellers.
  6. On checkout, choose
    1. Group products into as few shipments as possible
    2. AmazonGlobal Eligible

However, at this point in time, there seems to be a bug with the system. Having gone through this process, I still see delivery charges on my checkout page. I emailed customer service about this and essentially this is the workaround:

  1. Make sure what you what to buy is eligible for AmazonGlobal Eligible by going through the search above
  2. Pay for your product under the normal http://www.amazon.com
  3. Submit a refund request under through this link: http://www.amazon.com/rsvp-mi?c=bdrgxhhw3740683188&q=o2r

I would however make sure to first email the CS reps before proceeding with the purchase to confirm the refund process. The service is pretty prompt.

Relevant Links

http://www.amazon.com/gp/help/customer/display.html/ref=hp_596184_srcheligintlship?nodeId=201117930

Install Graphite on OSX 10.7 Lion

Installing Graphite locally on my machine was a bit annoying. Hope this helps the next person.

1. Virtualenv

Because we are clean and tidy people, let’s start by first creating a new virtualenv

mkvirtualenv graphite

2. Pip

This installs graphite into the default location at /opt/graphite

pip install whisper
pip install carbon
pip install graphite-web

3. Install py2cairo

Follow the instructions here

    1. Download, unpack, and cd into the the py2cairo directory
    2. Activate your virtual environment
    3. Follow the standard build procedure
./waf configure --prefix=$VIRTUAL_ENV
./waf build
./waf install

4. Configure conf files

cp /opt/graphite/carbon.conf.example /opt/graphite/carbon.conf
cp /opt/graphite/storage-schemas.conf.example /opt/graphite/storage-schemas.conf

5. Configure graphite

cp /opt/graphite/webapp/graphite/local_settings.py.example /opt/graphite/webapp/graphite/local_settings.py

Then edit settings.py

STORAGE_DIR = os.environ.get('GRAPHITE_STORAGE_DIR', join(GRAPHITE_ROOT, 'storage'))

DATABASES = {
    'default': {
        'NAME': os.path.join(STORAGE_DIR, 'graphite.db'),
        'ENGINE': 'django.db.backends.sqlite3',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': ''
        }
}

# Default sqlite db file
# This is set here so that a user-set STORAGE_DIR is available
#if 'sqlite3' in DATABASE_ENGINE \
    #and not DATABASE_NAME:
  #DATABASE_NAME = join(STORAGE_DIR, 'graphite.db')

SECRET_KEY = '<insert your secrey key>'

ALLOWED_HOSTS = '*'

This makes the settings file compatible with Django 1.5. You can get a secret key generator here.

6. Syncdb

cd /opt/graphite/webapp/graphite
sudo python manage.py syncdb

You should see a file called graphite.db in /opt/graphite/storage

7. Run

sudo python /opt/graphite/bin/carbon-cache.py start
sudo python /opt/graphite/bin/run-graphite-devel-server.py /opt/graphite

This is the expected output

graphite hello world

Here’s a test script to see if your graphite is working locally. https://gist.github.com/nychng/5883607. Got it off http://coreygoldberg.blogspot.sg/2012/04/python-getting-data-into-graphite-code.html

Then under tree, graphite, you should see foo/bar/.

Updates to my Vim workflow

I know vimfu

I know vimfu

The best way to improve your vim-fu is to do so incrementally. There have been a few inefficiencies in my current workflow that I’ve tolerated for the longest time. I’ve squashed a few of them today.

The first is auto-completing brackets. I used delimitMate for this. So now typing < automatically results in <> with my cursor in insert mode inside the triangular brackets.

The next one is auto-completing HTML tags. I have no idea why I’ve waited this long to get it fixed but closetag does the job well. To use, simple type <div> followed by ctrl-_.

The last one is great for finding bugs in your HTML. What matchit does is that when you press %, it cycles between the opening and closing tag where your cursor is currently located. Again, I’ve waited too long to fix this annoyance. This probably says more about my laziness than my patience.

Here’s a quick glance at all my plugins and my dotfiles for anyone interested.

Vim Plugins

Vim Plugins

Auto Start MongoDB Mac OSX

Follow the instructions to install MongoDB, after that, run through the creation of the database. The default location is /data/db but if you’re using Macports to install, you can choose to install your database alongside mongodb in /opt/local/var/db/mongodb.

To start the mongo server, simply run mongodb. MongoDB doesn’t auto launch itself. To do so, we have to edit the plist and add it to LaunchDaemon.

This is based on a MacPorts installation. If you’re using Brew, your mileage might vary.

  1. Edit /Library/LaunchDaemons/org.macports.mongodb.plist. My plist file looks like this.
  2. sudo launchctl load /Library/LaunchDaemons/org.macports.mongodb.plist
  3. Restart your system
  4. Go back into terminal and run mongo. You should be able to get into the interactive Mongo shell directly.

To stop this auto-start behaviour, simply use the unload command like so: sudo launchctl unload /Library/LaunchDaemons/org.macports.mongodb.plist

Installing MySQL51 Using MacPorts from Scratch for Lion

  1. Install the packages
    1. sudo port install mysql51 mysql51-server
  2. Initialize the db
    1. sudo -u _mysql /opt/local/lib/mysql51/bin/mysql_install_db
  3. Make mysql51 the default
    1. sudo port select mysql mysql51
  4. Run the daemon
    1. cd /opt/local ; sudo /opt/local/lib/mysql51/bin/mysqld_safe &
  5. Check to see its working by running mysql -uroot which should bring you into the interactive mysql shell as root user
  6. Setup
    1. /opt/local/lib/mysql51/bin/mysql_secure_installation

Other Notes:

  • For MySQL51, running cd /opt/local/mysql-test ; perl mysql-test-run.pl results in an error Can’t open perl script “mysql-test-run.pl”: No such file or directory. This is a reported bug.
  • Using zsh, running which mysql results in mysql: aliased to nocorrect mysql. To see the path. If you’re having problems getting mysql command to work, try this solution.
  • If you’re running into this error while connecting to MySQL Work Bench or Sequel Pro, MySQL said: Lost connection to MySQL server at ‘reading initial communication packet’, system error: 61, the fix is to comment out skip-networking in the my.cnf or macports-default.cnf file found at /opt/local/etc/mysql51

My Top 6 Productivity Tips for Developers or People Who Spend a Lot of Time on the Computer

Top Hit for 'Dilbert Productivity'

Top Hit for ‘Dilbert Productivity’

Every developer has their own little ‘secret’ cache of productivity tricks. Here are some of mine that I use every single day.

Cinch

Cinch is an awesome free app that allows you to snap your opened windows to the side of the screen. This feature is first introduced in Windows 7 (I believe) as Snap and it’s probably one of  the best UX features that have been introduced by the Windows team. My typical setup is to have my browser on snapped on the left side and my terminal screen on my right.

Clip Menu

There are quite a few clipboard managers around, the one that I use is called Clip Menu. If you’re not using a clipboard manager, you’re seriously under-changing yourself. cmd-1 brings up a history of my recently copied items and pressing the corresponding number allows me to paste the contents.

zsh (not applicable for non-developers)

I discovered zsh and oh-my-zsh recently and boy oh boy, am I loving every minute of it. Essentially, it’s gives you all the power of bash but with autocomplete goodness on steroids. Depending on your development setup, you will need to install the relevant plugins by modifying the .zshrc file which replaces the your bashrc/bash_profile settings.

Cloudapp

Does your work involve you needing to share an image/document/anything? I guess most of you would answer ‘yes’. Cloudapp enables you to do this by dragging and dropping the item you want to share onto it’s icon. It uploads the file and spits out a link which you can share immediately. A nice touch is that you get to see the number of times that file has been accessed.

Hot Key Your Mouse

I use two different mice. At home, I have a Razor Naga which I have configured the side buttons to do the following:

  • next/prev tab in chrome hotkeyed to numbers 1 & 2
  • back/forward in hotkeyed to 4 & 5
  • open/close tabs to 3 & 6

At work, I use a Logitech M705. The two side buttons I’ve set to back/forward. I’ve not had to change the batteries in this baby since I’ve gotten it 2 years ago (roughly). Logitech isn’t joking when it says its the ‘Marathon Mouse’.

Also, as any gamer would tell you, a good mouse pad helps.

Always Be Learning While Travelling

You can only do that many days of people watching and blanking out on your commute to work. Why not learn something new everyday instead? Two of my favourite podcasts that I like to tune in to work are: Radiolab and 99% Invisible.

Bonus Hack: Conversation Starter

One of my favourite questions to ask a newly acquainted friend or someone whom I haven’t met for a while is this: what’s the most interesting/meaningful thing that has happened to you since we last met/this year. It’s an awesome starting point to start a conversation as it gets the person talking about something close to his/her heart.

Do you know any good productivity tips? Do share!