Shifting from Java to Kotlin in SUSI Android
SUSI Android (https://github.com/fossasia/susi_android) is written in Java. After the announcement of Google to officially support Kotlin as a first class language for Android development we decided to shift to Kotlin as it is more robust and code friendly than Java.
Advantages of Kotlin over Java
- Kotlin is a null safe language. It changes all the instances used in the code to non nullable type thus it ensures that the developer don’t get any nullPointerException.
- Kotlin provides the way to declare Extensive function similar to that of C#. We can use this function in the same way as we use the member functions of our class.
- Kotlin also provides support for Lambda function and other high order functions.
For more details refer to this link.
After seeing the above points it is now clear that Kotlin is much more effective than Java and there is harm in switching the code from Java to Kotlin. Lets now see the implementation in Susi Android.
Implementation in Susi Android
In the Susi Android App we are implementing the MVP design with Kotlin. We are converting the code by one activity each time from java to Kotlin. The advantage here with Kotlin is that it is totally compatible with java at any time. Thus allowing the developer to change the code bit by bit instead of all at once.Let’s now look at SignUp Activity implementation in Susi Android.
The SignUpView interface contains all the function related to the view.
interface ISignUpView { fun alertSuccess() fun alertFailure() fun alertError(message: String) fun setErrorEmail() fun setErrorPass() fun setErrorConpass(msg: String) fun setErrorUrl() fun enableSignUp(bool: Boolean) fun clearField() fun showProgress() fun hideProgress() fun passwordInvalid() fun emptyEmailError() fun emptyPasswordError() fun emptyConPassError() }
The SignUpActivity implements the view interface in the following way. The view is responsible for all the interaction of user with the UI elements of the app. It does not contain any business logic related to the app.
class SignUpActivity : AppCompatActivity(), ISignUpView { var signUpPresenter: ISignUpPresenter? = null var progressDialog: ProgressDialog? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_sign_up) addListeners() setupPasswordWatcher() progressDialog = ProgressDialog(this@SignUpActivity) progressDialog?.setCancelable(false) progressDialog?.setMessage(this.getString(R.string.signing_up)) signUpPresenter = SignUpPresenter() signUpPresenter?.onAttach(this) } fun addListeners() { showURL() hideURL() signUp() } override fun onOptionsItemSelected(item: MenuItem): Boolean { if (item.itemId == android.R.id.home) { finish() return true } return super.onOptionsItemSelected(item) }
Now we will see the implementation of models in Susi Android in Kotlin and compare it with Java.
Lets First see the implementation in Java
public class WebSearchModel extends RealmObject { private String url; private String headline; private String body; private String imageURL; public WebSearchModel() { } public WebSearchModel(String url, String headline, String body, String imageUrl) { this.url = url; this.headline = headline; this.body = body; this.imageURL = imageUrl; } public void setUrl(String url) { this.url = url; } public void setHeadline(String headline) { this.headline = headline; } public void setBody(String body) { this.body = body; } public void setImageURL(String imageURL) { this.imageURL = imageURL; } public String getUrl() { return url; } public String getHeadline() { return headline; } public String getBody() { return body; } public String getImageURL() { return imageURL; } }
open class WebSearchModel : RealmObject { var url: String? = null var headline: String? = null var body: String? = null var imageURL: String? = null constructor() {} constructor(url: String, headline: String, body: String, imageUrl: String) { this.url = url this.headline = headline this.body = body this.imageURL = imageUrl } }
You can yourself see the difference and how easily with the help of Kotlin we can reduce the code drastically.
For diving more into the code, we can refer to the GitHub repo of Susi Android (https://github.com/fossasia/susi_android).
Resources
- First impression using Kotlin by Pedro Lima https://dzone.com/articles/kotlin-vs-java-first-impressions-using-kotlin-for
- Comparison between Java and Kotlin by Jessica Thornsby https://code.tutsplus.com/articles/java-vs-kotlin-should-you-be-using-kotlin-for-android-development–cms-27846
You must be logged in to post a comment.