Tuesday, 30 July 2019

"WorkManager" a server less android application using Kotlin and Firestore

WorkManager is a serverless android application where data is saved in Firestore. We will be creating a cloud function to send the push notification. You can use this application as a reference for implementing Firestore.


Prerequisite




Getting Started

In Firebase there are mainly two main database concepts. One is a Real-time database and the other one is Firestore. Firestore is in beta mode, which means code can change. In this application, We are concentrating on Firestore.

Project creation in firebase

Step 1. Create a project in Firebase Link
Step 2. Provide details like the Package name, SHA1, etc to configure
Step 2. On completing step2, a file named google-services.json.Download this file and add to the app folder. With this process, you are done with configuration.

Push Notification in a serverless application

Since we don't have a server to handle push notification, We are going to develop a cloud function and host them in Googe cloud functions. On hosting you can view them on your firebase console. To start with, you need to install Nodejs on your local system. With the help of nodejs framework and firebase tool, you need to create a cloud function. On completing this process you can upload it on to firebase cloud function. I have kept everything simple in here. The logic is quite simple, We have a collection(For the time being let us assume collection to be a table) named Notification. Whenever there is an entry in notification collection, we need to trigger our cloud function. This cloud function will send the push notification.
Step 1. Install node.js on your local system Click Here to download Nodejs
Step 2. Make a folder for cloud function and open command prompt here
Step 3. Use this command to install firebase tools npm install -g firebase-tools
Step 4. Now login to your account using firebase login
Step 5. Now configure your application using, on completing you can find an index.js file
Step 6. add necessary code in the index.js file and upload the function using firebase deploy
I have added the cloud function code in the firebase function folder please have a look.
[Similarly, you can host your admin website in Firebase Hosting section(I will be updating same)]

Wednesday, 9 May 2018

ML Kit for Firebase


Introduction

Machine learning is one of the trending topics but using the power of machine learning in your application was a hectic process.more over implementation in a mobile application was an area concerning experts. The situation is changing with the launch of ML kit.ML Kit is a mobile SDK with which you can utilize Google's machine learning technologies in your Android and IOS application. Google has bundled Google Cloud Vision API, TensorFlow Lite, and the Android Neural Networks API together into a single SDK. You don't have to know about neural networks or any other deep learning concepts. If you are having some experience in ML, then you can go with your own custom Tensorflow model for prediction or regression or anything that you want to do. Since ML kit is in beta, APIs might change in future. So For production-ready cloud-based APIs, consider using Cloud Vision APIs directly.



Features

  • Easy and Ready to use common use cases

ML  kit provides you with ready to use APIs for common use cases like text recognition, face detection, landmark identification and labeling images.

  • Run-on device and Cloud


ML Kit’s APIs can run on-device as well as in the cloud. on-device APIs can process your data quickly and work even when there’s no network connection. cloud-based APIs, on the other hand, leverage the power of Google Cloud Platform's machine learning technology to give you an even higher level of accuracy. 

These are the features supported on-device

These are the features supported in the cloud

  • Use custom model


You can host your custom Tensorflow model in Firebase, so that it is available to your application. To host custom model navigate to your firebase console and select "ML Kit" from the side panel. Now select "CUSTOM" add your custom model in here.


Appendix




Monday, 12 March 2018

Dependency Inversion , Dependency Injection and DI frameworks

Introduction

When I started researching about Dependency Injection, I had the very same question "Why dependency injection", when I am happy with what I am doing.One day when I was going through best practice in coding, I happened to see a comment by ROBERT MARTIN which Was

"3 important characteristics of a bad design that should be avoided:
Rigidity - It is hard to change because every change affects too many other parts of the system.
Fragility - When you make a change, unexpected parts of the system break.
Immobility - It is hard to reuse in another application because it cannot be disentangled from the current application."

I went through my code, is my code RIGID? yes, it is.Is my code fragile? yes, it is.is my code immobile? again, yes it is. Since my code is a complete mess, I need to change the whole approach.Further research led me to the term SOLID principles and the clean architecture principles of Uncle Bob (Robert C. Martin).

This article is not about SOLID principle.I will be writing about it in my upcoming articles.In this, I will brief through Solid principle.

There exist 5 most recommended design principles, that you should keep in mind while writing your classes. These design principles are called SOLID.Why do we have to follow this principle?

SOLID are five basic principles which help to create good software architecture. SOLID is an acronym where:-
S stands for SRP (Single responsibility principle
O stands for OCP (Open closed principle)
L stands for LSP (Liskov substitution principle)
I stands for ISP ( Interface segregation principle)
D stands for DIP ( Dependency inversion principle)

In this article, we will be concentrating on "D" the Dependency inversion principle.

Dependency inversion principle

"The Dependency Inversion Principle states that high-level modules should not depend on low-level modules; both should depend on abstractions. Abstractions should not depend on details. Details should depend upon abstractions" This might sound bloated, but it is really easy to understand. This principle allows for decoupling.

Implementing proper dependency inversion in our apps allows to have:

Loose coupling
Easily testable code
Code reusability



As to why it is important, in short: changes are risky, and by depending on a concept instead of on an implementation, you reduce the need for change at call sites.
Effectively, the Dependency Inversion Principle reduces coupling between different pieces of code.

Dependency Injection

Dependency injection can be considered as an implementation of Dependency Inversion Principle.
Dependency Injection (DI) is a design pattern which has been around for awhile, but recently it has become more commonly used in the development of Android applications.Dependency injection is a style of object creation in which objects are created by an external entity or technique whereby one object supplies the dependencies of another object.






We have three types of Dependency injection
1) Constructor Injection
2) Setter/Getter Injection
3) Interface Injection

Depending on the technologies, some are supported and others are not.In this article, we will be concentrating on Dependency injection implementation in Android.


When we write code, we will often find that our classes will have dependencies on other classes. So class X might need to have a reference or dependency to class Y. To make things a little clearer let’s look at the case where we have a Car class that needs to use an Engine class.

public class Car {

private Engine engine;

public Car() {
engine = new PetrolEngine();
}
}


This code works fine, but the downside is that the coupling between the Car and the Engine is high. The Car class creates the new Engine object itself and so it has to know exactly what Engine it needs, in this case, a PetrolEngine. Maybe we could do a little better and reduce the coupling, so let’s look at a different way of creating this Car class.


public class Car {

private Engine engine;

public Car(Engine engine) {
this.engine = engine;
}
}

it’s quite straightforward to start using it in our code. We simply look at what dependencies are needed and pass them via a constructor or a method call.
If it is this simple then why we need libraries or a DI frameworks?

An application is never this simple.As the application becomes complex things can start getting a little messy.
In our example of a Car that has a dependency on an Engine, imagine that the engine also has its own set of dependencies. Let’s say it needs a crankshaft, pistons, block, and head. If we follow DI principles we will pass these dependencies into the Engine class, that’s not so bad, we just need to create these objects first and pass them into the Engine object when we create that. Finally, we pass the Engine to the Car.
Next, let’s make our example a little more complicated. If we imagine trying to create classes for each part of an engine we can see that we would soon end up with possibly hundreds of classes with a complicated tree (more accurately it is a graph) structure of dependencies.

From our example, we can see that implementing DI on our own can lead to creating a lot of boilerplate code and the more complex your dependencies the more boilerplate you will have to write. so to solve this issue we can use DI frameworks.These frameworks make it simple to configure dependencies and in some cases generate factory and builder classes for creating objects, making it very straightforward to create complex dependencies that are easily managed.

Working of Dependency injection libraries

Most dependency injectors rely on reflection to create and inject dependencies.Reflection is awesome but is very slow and time-consuming. Also, it performs dependency resolution at runtime which leads unexpected errors and crashes the application.
On the other hand, some injectors use a Pre-compiler that creates all the classes (object graph) it needs to work using Annotation Processor. An Annotation Processor is a way to read the compiled files during build time to generate source code. So it performs the dependency resolution before the application runs and avoids unexpected errors.

We have a lot of dependency injection libraries for Android, let us look into some of them

RoboGuice

Roboguice injects code at runtime using reflection. Using RoboGuice, we can inject Views, Drawable, Resources, System Service, or any other objects. Because of using reflection it is slow.

Traditional code:
TextView mTvName = (TextView) findViewById(R.id.tv_name);

Roboguice injects view code:
@InjectView(R.id.tv_name ) TextView mTvName;


ButterKnife


Butterknife uses annotation processing to generate modified Java classes based on annotations. It allows annotating the views and OnClickListeners. Butterknife also includes findById methods which simplify code that still has to find views on a View, Activity, or Dialog.

Traditional code:
TextView mTvName = (TextView) findViewById(R.id.tv_name );

Butterknife code:
@BindView(R.id.tv_name) TextView mTvName;

Traditional code:
button.setOnClickListener(new OnClickListener(){
@override
public void onClick(View view){
}
})

Butterknife code:
@OnClick(R.id.button)


Dagger

Dagger is designed for low-end devices. It is based on the Java Specification Request (JSR) 330. It uses code generation and is based on annotations. The generated code is very relatively easy to read and debug.

Dagger 2 uses the following annotations:
@Module and @Provides: define classes and methods which provide dependencies
@Inject: request dependencies. Can be used on a constructor, a field, or a method
@Component: enable selected modules and used for performing dependency injection.


Conclusion

Dependency injection helps to simplify the code and also provide an adaptive environment that’s useful for testing and other configuration changes.In the upcomming post, I will be covering implementation of dagger2.














Monday, 5 March 2018

Flutter An Overview



Introduction
Flutter is Google’s open source toolkit for helping developers build iOS and Android apps moreover it is one of the primary methods of developing applications for Google Fuchsia.Flutter uses C, C++, Dart, and Skia (a 2D rendering engine) as its backbone.
To get a clear picture go through the architecture diagram.

The Flutter framework makes it easy for you to build user interfaces that react smoothly in your app while reducing the amount of code required to synchronize and update your app's view.
Flutter makes it easy to get started building beautiful apps, You will get material Design in case of Android and Cupertino widgets in case of iOS.In simple, flutter provides a native experience to Users.For developers, Flutter lowers the bar to entry for building mobile apps. It speeds up development of mobile apps and reduces the cost and complexity of app production across iOS and Android.For designers, Flutter helps deliver the original design vision, without loss of fidelity or compromises. It also acts as a productive prototyping tool.



The initial question that arises as we here about a technology that supports multiple platforms is about its performance .flutter provides an excellent performance.Developers can easily achieve a constant 60fps. Flutter apps run via natively compiled code – no interpreters are involved. This means Flutter apps start quickly. Now about native API access.flutter gives access to some platform-specific services and APIs from the operating system.

Coding
How we are going to code?Do we have to use JAVA ?or KOTLIN? Or something else?
You'll write your Flutter apps in Dart.Dart is a general-purpose programing language developed by Google and its syntax is similar to Java, JavaScript, C#, or Swift. Dart is compiled using the standard Android and iOS toolchains for the specific mobile platform where your app needs to run.

Follow the link to get more details on Dart

How to Start
To start with you need flutter SDK and an IDE with flutter plugin as per the documentation
Flutter's IDE tools are available for Android Studio, IntelliJ IDEA Community (free), and IntelliJ IDEA Ultimate.

To build and run Flutter apps on iOS:
  • a computer running macOS
  • Xcode 9 or newer
  • iOS Simulator, or a physical iOS device
To build and run Flutter apps on Android:
  • a computer running macOS, Windows, or Linux
  • Android Studio
  • Android Emulator (comes with Android Studio), or a physical Android device
Follow the link to get more details on getting started


"WorkManager" a server less android application using Kotlin and Firestore

WorkManager is a serverless android application where data is saved in Firestore. We will be creating a cloud function to send the push not...