Angular event and action timer

Angular 2 is already in Beta but I am now still working on a project with ionic which is based on angular 1.4.3. Maybe too late but there are two small pieces of code I think worthing sharing, so I put them into two tiny libs for angular 1: angular-event and angular-action-timer.

angular-event

Some people are using $rootScope.$emit() for event based communication. angular-event is basicly a wrapper arount it but no more string event names typing around.

Read More

FormationLayout and Protocol-Oriented Programming in Swift

My favourite talk of WWDC 2015 was Protocol-Oriented Programming in Swift. However, when Dave Abrahams says, “Swift is the first Protocol-Oriented Programming language”, I didn’t get it. Isn’t protocol in Swift the same as interface in other languages? Isn’t “Protocol-Oriented Programming” the same as one of the SOLID principles - Dependency inversion principle - “Depend upon Abstractions. Do not depend upon concretions”? It is not new at all.

Then in the later part of the talk I learned from Dave how powerful protocol is in Swift 2, especially Self Requirement and Protocol Extensions, and why we should use protocols instead of classes for abstraction. I felt excited and already Protocol-Oriented myself after watching the talk. I need to do something to check if I really am :)

So here comes FormationLayout, a Protocol-Oriented AutoLayout library in Swift 2. Now I will show you how I made it Protocol-Oriented and why it is good.

Read More

BDD Demo with GameOfLife in Swift and Quick

BDD(Behavior Driven development) was firstly developed by Dan North as a response to the issues encountered teaching test-driven development:

  • Where to start in the process
  • What to test and what not to test
  • How much to test in one go
  • What to call the tests
  • How to understand why a test fails

That was version 1 of BDD in mid-2003. BDD v3 now is more about communications between all different roles in agile teams using the same language: GivenWhenThen([1] [2]).

I made some videos before to demonstrate TDD on Android platform using Conway’s Game of Life as an example. This time I will use this example again to demonstrate BDD on iOS platform in Swift and a BDD framework Quick.
(Code at GitHub)

1. Create Project and Add Quick Framework

Read More

AngularJS unit testing with Jasmine

I am currently working on a hybrid mobile app using Ionic which is based on AngularJS. AngularJS, just like my favorite ActionScript 3 framework Robotlegs, was designed to be testable. With helps of dependency injection and built in mocks, it is quite easy to write tests but still can be painful to get everything sorted out. Here are some tips to make life easier. Like a template (in WebStorm) to setup a test for a service:

1
2
3
4
5
6
7
describe('$MODULE$.$NAME$', function () {
var $NAME$;
beforeEach(module('$MODULE$'));
beforeEach(inject(function (_$NAME$_) {
$NAME$ = _$NAME$_;
}));
});

Mock dependencies

At start of tests when we load the module we can mock dependencies at the same time:

1
2
3
4
5
var $VALUE$;
beforeEach(module('$MODULE$', function ($provide) {
$VALUE$ = {$END$};
$provide.value('$VALUE$', $VALUE$);
}));

Test controllers

Unlike services, controllers can not be injected so we need to instantiate them ourselves.

Read More

C++ unit testing with AppCode and GoogleTest

AppCode started to support Google Test since 2.5 EAP. Steps to get it work:

  1. Get Google Test
  2. Open {gtest_path}/xcode/gtest.xcodeproj and build gtest.framework
  3. Create a “Command Line Tool” project or target
  4. Add gtest.framework to project and the new target
  5. Set Up the Executable Run Environment. 3 options:
  6. Make sure your project and google test use the same C++ standard library

    Read More

Android app Draw Shapes

My first Android App Draw Shapes, which is actually my first assignment of android programming class, is now on Google Play. The code is shared at GitHub.

ActionBar

I want to show the user what they are drawing on the action bar. So I put a DrawingShapeView in action_bar layout, and set it up in my MainActivity class.

MainActivity.setupActionBar()link
1
2
3
4
5
6
7
8
View actionBarView = LayoutInflater.from(this).inflate(R.layout.action_bar, null);
((DrawingShapeView) actionBarView.findViewById(R.id.drawingShape)).setup(model, painter);
//...
ActionBar actionBar = getActionBar();
actionBar.setCustomView(actionBarView);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);

Read More

TDD Demo GameOfLife with Android Studio

On my Java class the teacher used Conway’s Game of Life as an example for TDD and I found it a really good example. So I made some videos to demonstrate how I tried TDD on it. In the videos when I say “grid” or “gred” it should actually be “cell” (And I pronounced “live” wrongly). Sorry for my poor English. If the videos are not clear please try to change quality to HD by the settings button.

For each step I also posted new or changed code below, and links of full code after each step on GitHub. I posted some keyboard shortcuts as well. I don’t know what they are on windows but you can find them (and change them if you would like) in “Keymap” section of Android Studio’s preference (or settings) panel.

1. Create GameOfLife Project

The first step simply create a new project on Android Studio. Android Studio is still in beta (I’m using 0.8.6) but I am very happy with it so far. On the GDG Auckland September Meetup, Julius Spencer said his team (JSA) has already been using Android Studio on production for one year now. So give it a try if you haven’t yet. It’s so much better than eclipse!

Read More

Run different hsqldb databases

I have different projects using hsqldb with slightly different tables and I want to have different databases for them. After I found out how to run hsqldb on Mac OS X, it turns out quite straightforward to do so.

What the windows runner of hsqldb runServer.bat does is going to the data folder and run the server there. Then the server will create and save all data in this data folder.

runServer.bat
1
2
cd ..\data
@java -classpath ../lib/hsqldb.jar org.hsqldb.server.Server %1 %2 %3 %4 %5 %6 %7 %8 %9

So my new script make a new folder under each project named db or whatever and go into it then run the server. All projects can now have their own hsqldb databases.

runServer.sh
1
2
3
mkdir db
cd db
java -cp path/to/hsqldb/lib/hsqldb.jar org.hsqldb.server.Server

I name this script db.sh and put it in my workspace then in each project I run ../db.sh. If I do need some new porject have the same data to start with I only need to copy the db folder.

Tips of using Realm iOS

I am trying to use Realm on an iOS project by CocoaPods. After hours debugging, I finally got it working and there are 3 tips I’d like to record.

Tip 1: Work on other CocoaPods library project

I am using CocoaPods to separate the iOS project to libraries. Realm works fine with the main app and libraries separately, but the libraries couldn’t be compiled as Pods project in the main app. I solved this problem by adding Realm framework to the Pods project and add it to the library targets myself. But I need to do this every time after I run pod update which I need to do frequently. I need to write a script to do it for me later.

(Update 21.09.2014: Realm 0.85.0 use source code instead of framework file so Tip 1 is not needed now)

Tip 2: Runtime errors when try to write Realm model objects

If you need to use a RLMArray of one model object SomeObject in another, you need to use RLM_ARRAY_TYPE(SomeObject) and declare it as RLMArray<SomeObject> *someObjects. At first I couldn’t get it work so I marked the someObjects property ignored. Then I can’t get it work any more and the runtime error tell me nothing about what happened. The problems is, when you change one RLMObject schema and you already stored some data as the old schema, you need to delete the old Realm file (or migrate).

Tip 3: Do not work with <NSObject> protocol

After I upgraded to Realm 0.85.0 and Xcode 6, Realm complained about a ‘hash’ property. After debugging for a while I found out that it is because some of my RLMObjects conform to <NSObject> protocol. It was fine with Realm 0.83.0 and Xcode 5. I’m not sure if it’s because of my upgrade of Realm or Xcode but it was fixed just by removing the <NSObject> protocol from the RLMObjects.

hsqldb on Mac OS X

It took me a while googling how to run hsqldb on Mac OS X. It turned out very easy when I looked back at its runners for Windows:

runServer.bat
1
2
cd ..\data
@java -classpath ../lib/hsqldb.jar org.hsqldb.server.Server %1 %2 %3 %4 %5 %6 %7 %8 %9

Get it running on OS X just by typing it on Terminal, or create a runServer.sh file and run it anywhere.

runServer.sh
1
2
cd path/to/hsqldb/data
java -cp ../lib/hsqldb.jar org.hsqldb.server.Server