5 notes on MVP architecture pattern for Android

                                                           Image credits Macoscope

MVP (Model View and Presenter) is an architectural pattern inspired by the popular MVC pattern.

MVP addresses two main points :

  1. Make views as dumb as possible. The dumber the better.
  2. Make each layer loosely coupled and easily testable in isolation.

I am using MVP in one of my production project and have used in some dem0 apps. Here are my 5 notes on using MVP for android.

  1. Package Structure :

Android project contains lots of code and files even for application of medium complexity. Even when not following MVP I have found that arranging the project files in such a way that files that are accessed together are put in same package is more efficient and intuitive than any other approach.

What I prefer doing is create separate package for separate verticals of the app and put all related files like activities, fragments, views, presenters, adapters etc in that package.

ex. packages like add task, view task, list task for a To-Do app.

2. Libraries that are useful for MVP :

In MVP you want your model and presenter to be independent of the life cycle of view. For this, you can use dependency injector library like Dagger2.

Other than that, using RxJava and reactive programming principles for creating presenter is also becoming increasingly popular.

Libraries you can use for this purpose are : RxAndroid and EventBus.

3. Managing Remote and local data sources in the Model :

Android apps have to fetch data from the server. At the same time fetched data must be cached to make the app usable offline and increase the speed.

What I prefer doing is to create three model classes :

1. Remote Data Source

2. Local Data Source

3. Data Repository

All presenters talk to Data Repository class. Data repository model contains references to Local and Remote data repository and calls data from either according to situation.

As the name suggests Local Data Source deals with cached data and disk storage whereas Remote Data Source deals with API calls and responses.

4. User Experience is the top priority :

One thing that we all have to keep in mind that the real test of application is, if it is able to provide user a nice experience.

At the end of the day, user only notices the user experience of the application and not the architecture used. So if you have to make some design sacrifices to make the UX better, do it.

The real test of the machine is the satisfaction it provides to the mind. There is no other bigger test.

5. Testing Advantages :

Main motive behind MVP pattern was to make the testing of layers easy.

Basic idea is to keep the presenter and model android free, so that they can be tested without Android instrumentation by the JVM itself.

Views can then be tested by Android Instrumentation tests.

Mockito and Espresso can come handy for testing purposes.

Conclusion :

MVP, in my opinion is so far the best way to architect your android application project. It simplifies many issues like testing and making views lighter. Combine it it RxJava and dependency injection and you’ve got a nice recipe for android projects.

I am learning more about RxJava and testing frameworks will share my views on that soon.

Thanks.

How to detect emails, phone numbers, web URLs, hashtags in Android TextView? : SmartTextView library

Smart Text View

Star On GitHub Follow On GitHub

Many applications in android use TextView that detects and highlight various string patterns like emails, phone numbers, web URLs and even hashtags.

This functionality is very common and and is used extensively and frequently in many app. Thus to ease the process of adding this functionality I have authored an Android library SmartTextView.

Find and Star it on GitHubhttps://github.com/priyankvex/SmartTextView

smart_text_view_small

Features

  • Detect emails, mobile numbers, URLs
  • Detect #hash_tags and @mentions
  • Use default intents or set custom callbacks.
  • Set different colors for each pattern.

 

Usage

compile 'com.priyankvex:smarttextview:1.0.1'

In layout xml file

  <com.wordpress.priyankvex.smarttextview.SmartTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="9pt"
        android:id="@+id/textView"
        />

In java Activity or Fragment

mSmartTextView = (SmartTextView) findViewById(R.id.textView);
        mSmartTextView.setEmailColorCode("#3cb371");
        mSmartTextView.setPhoneNumberColorCode("#ff33aa");
        mSmartTextView.setHashTagColorCode("#f37735");
        mSmartTextView.setUrlColorCode("#ffc425");
        mSmartTextView.setMentionColorCode("#57b884");
        mSmartTextView.setDetectMentions(true);
        mSmartTextView.setDetectHashTags(true);
        mSmartTextView.setText(sampleText);
        mSmartTextView.setSmartTextCallback(this);

To use custom callbacks

implements SmartTextCallback

Business in Boxers : 1 Month Into Running a StartUp

Why the Series? 

(Inspired by book : Business In Blue Jeans)

Hi! This is Priyank. This series is about me pen downing my start-up ride. Don’t know if it’s fast or slow, all I really know is I’m gonna enjoy the ride.

Why the name?

Boxers is what you’ll find me almost every time in and business is what’s always in my mind and thus the name “Business In Boxers”.

The Beginning

Last month on 15th Feb, me and my friend sat down seriously deciding on which business to start. The interesting thing was I did this with 3 friends. 3 awesome business ideas that are practical and scalable. Next, we 4 formed a team. A team with a vision to launch multiple businesses in a very short period of time.

Our first startup what we call as Pebbles Media deals with creating engaging applications for local business owners that can help them discover new customers in their locality. Cool idea that we all thought will get us many clients.

We approached 5 clients in different domains from food business to education to mechanical work. Two of them showed interest and we started working on their products.

15 days later one of our clients refused to pay the advance thus we dropped him. Other client wanted a custom product built for them at a very unreasonable price, again dropped him.

The Pivot

Witnessing all this, we all after 1 hour long discussion decided that it is the time for an early pivot. We never wanted to enter service based B2B sector.

The pivot that will need us to make products that will be used by multiple small businesses and their customers making us a product based company.

A pivot at this early stage raised doubts in our minds but I guess it was the red flag that we all managed to see and decided to pivot and not to run for quick money.

In Love With Business Books

lately I have been reading a lets of business books. Getting ideas and knowing how various aspects of a startup works. My favorite is The Lean Startup. A must read for everyone.

The Speed Breakers

No surprise here, first month has been filled with speed breakers. I was not expecting any smooth ride either. This month we have seen from clients saying no to all saying yes at the same time. Client giving us advance and then we turning him down because we wanted to pivot.

Wrap Up!

This month we dealt with real clients. Realized that we don’t want to go into service based sector.

For next month we are planning to get our MVP out in the market and test our leap of faiths. Too much action for next month. Let’s see what’s in the store next.

See you next time.

 

PS : It is fun to write blog when you are pretty certain that no one will read it 😀

 

 

 

 

 

 

 

 

Practice Shapes : Android App To Learn Shapes

Only way to learn something is to learn it by yourself.

Recently I have been playing with Canvas in android and it has been fun! 😀

As they say knowledge is knowing tomato is a fruit. Wisdom is not putting it in vegetable basket. And best way to persist new gained “wisdom” is to put it in use.

Practice Shapes is a fun app both for kids and grown-up kids (Yes. You!), to test their drawing skills. Though I know drawing on a mobile screen with your fingers is not your favorite thing, but still you will enjoy it! 😀

Download the app from play store :

 

This app is open source : You can fork it at https://github.com/priyankvex/Practice-Shapes/

 

TECH STACK : 

  • Android SDK
  • Bitmap Images of Shapes

The power lies in not reinventing the wheel, but to use it to move forward.

OPEN SOURCE LIBRARIES USED : 

  1. Universal Image Loader
  2. Sugar ORM
  3. Fancy Buttons

To Create Images : 

To create images for the app on which user draws, i used GIMP. These are just simple images with transparent background.

Right now, there are total of 18 images in the app under 3 difficulty levels.

If you can add more images feel free to do so.

Screenshots of app : 

drawing_ss

hard_shapes_ssscore

Download the app from play store :


Rails Number Helper Port to Android

A polyglot developer will understand, there is always something you like about one language and you wish was available for another language too.

So, recently I started learning rails and found number_helper API very cool and fun to use. It gives you easy API to convert numbers into many formats like number_to_human which will convert 20000 to 20 Thousands or number_to_human_size that will convert 1024 to 1 KB.

That was it I wanted this stuff for Android too. It just makes processing numbers to fit the UI needs so damn easy.

Here is my attempt to port this rails module to Android.

Here is the source code :

https://github.com/priyankvex/Rails-Number-Helper-For-Android

Usage : 

Just copy the module number_helper in your application folder and include module dependency.

Examples : 

 


        // Number to human converter
        NumberToHumanConverterBuilder builder2 = new NumberToHumanConverterBuilder(123456);
        NumberToHumanConverter converter2 = builder2.build();
        try {
            String humanNumber = converter2.convert();
            // OUTPUT : 123.456 Thousands
            Log.d(Config.TAG, humanNumber);
        } catch (InvalidSeparatorException | InvalidDelimiterException
                | InvalidPrecisionException e) {
            e.printStackTrace();
        }

Examples can be found here.
https://github.com/priyankvex/Rails-Number-Helper-For-Android/blob/master/app/src/main/java/com/wordpress/priyankvex/numberhelperdemo/MainActivity.java

Features : 

  • Easy to use API.
  • High utility for UI needs.
  • Small in size.
  • Custom exceptions to fit the needs of Android.

 

Making GCM client in Android and Application server in Ruby on Rails

It is always fun to play with new APIs and tools. GCM is not something I was getting my hands on for the first time but it changed completely since last time I did a project using this.

But the good news is it has changed for good. New service classes makes it easier to write GCM clients for Android, GCM default configuration makes writing test servers and apps to play with very less tedious. In fact my first reaction was this is GCM reincarnated. 

GCM Client and App server
GCM Client and App server

New API : New Party!

You can have a look at the source code here :

https://github.com/priyankvex/GCM-Android-Client-and-Rails-Server

To create Android client I stuck with sample provided in the docs. Sweet and simple to implement. Just little tweaks done to make it fit my needs.

For creating application server i.e the 3rd party server required by the GCM stack, it used Ruby on Rails. In that specifically I used “gcm” gem.

Again not a very complex server but good enough to get a hold of things.

Server features : 

  1. Save incoming device tokens (registration ids).
  2. Web form to send message to all devices registered.
  3. Send welcome notification when device is registered.

Client features (Android App) :

  1. Get GCM token for the device.
  2. Send token to applications server.
  3. Show notification when message is pushed from server.

 

Paint App in Android

Recently I was learning about Canvas and Custom views in Android and to put the theory in practice I made a pretty simple Paint App. 

Source code of the app can be found https://github.com/priyankvex/Paint-App-Android.

Screenshot of the app
Screenshot of the app

 

The App is pretty simple with features
1.  Create Drawings (Obvious one :P).
2.  Select different brush sizes.
3.  Eraser with different sizes.
4.  Color pallet.
5.  Save drawings in Gallery.

Source code of the app can be found https://github.com/priyankvex/Paint-App-Android.

Brownie points if you can guess the character I drew! 😀