kotlin-android-extensions

Additional

Language
Kotlin
Version
1.0.0 (Nov 30, 2015)
Created
Nov 28, 2015
Updated
Nov 8, 2017 (Retired)
Owner
Andrew Chen (yongjhih)
Contributors
Andrew Chen (yongjhih)
The Gitter Badger (gitter-badger)
Josef Petrák (jspetrak)
Jovche Mitrejchevski (mitrejcevski)
JoonHo Park (pjhjohn)
5
Activity
Badge
Generate
Download
Source code

kotlin-extensions

Usage

Database

Before:

db.beginTransaction();
try {
  db.delete("users", "first_name = ?", new String[] { "Andrew" });
  db.setTransactionSuccessful();
} finally {
  db.endTransaction()
}

or

Databases.from(db).inTransaction(it -> {
  it.delete("users", "first_name = ?", new String[] { "Andrew" });
});

After:

db.inTransation {
  delete("users", "first_name = ?", arrayOf("Andrew"))
}

Before:

String firstName = cursor.getString(cursor.getColumnIndexOrThrow("first_name"));

After:

val firstName = cursor.getString("first_name");

Before:

String firstName = null;
int firstNameColumnIndex = cursor.getColumnIndexOrThrow("first_name");
if (!cursor.isNull(firstNameColumnIndex)) {
  firstName = cursor.getString(firstNameColumnIndex);
}
firstNameView.setText(firstName != null ? firstName : "Andrew");

After:

val firstName = cursor.getStringOrNull("first_name")
firstNameView.setText(firstName ?: "Andrew")

SharedPreferences

Before:

SharePreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharePreferences.Editor editor = prefs.edit();
editor.putString("name", "Andrew Chen");
editor.putInt("age", 18, 14);
editor.apply();

After:

User(context).edit {
  name = "Andrew Chen"
  age = "18"
}

public class User(context: Context) : Preferences(context) {
  var name: String by Preference()
  var age: Int by Preference(default = 14)
}

or

User user = User(context)
user.name = "Andrew Chen"
user.age = 18
user.apply()

Bonus - SharedPreferences Extension:

Before:

SharedPreferences.Editor editor = prefs.edit();

editor.putString("first_name", "Andrew");
editor.putString("last_name", "Chen");
editor.remove("age");

editor.apply();

or

SharedPreferencesUtils.from(prefs).apply(editor -> {
  editor.putString("first_name", "Andrew");
  editor.putString("last_name", "Chen");
  editor.remove("age");
});

After:

prefs.edit {
  putString("first_name", "Andrew")
  putString("last_name", "Chen")
  remove("age")
}

System Services

Before:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

After:

val notificationManager = context.getNotificationManager()

Notification

Before:

Notification notification = new NotificationCompat.Builder(context)
  .setContentTitle("Hello")
  .setSubText("World")
  .build();

After:

val notification = Notification.build(context) {
  setContentTitle("Hello")
  setSubText("World")
}

Query

Math

Import from MichaelRocks/MathExtensions.kt

ThreeTen Backport (JSR-310)

LocalDateTime.now().toDate()
new Date().toLocalDateTime()

View

view.fadeOut()
view.fadeIn()
for (view in views.children())
if (view in views) {
  // ..
}
views += view
views -= view
views.forEach {}
views.filter {}

Misc

Installation

jitpack:

repositories {
    // ...
    maven { url "https://jitpack.io" }
}

dependencies {
    compile 'com.github.yongjhih.kotlin-extensions:kotlinx-android-sharedpreferences:-SNAPSHOT'
    compile 'com.github.yongjhih.kotlin-extensions:kotlinx-android-database:-SNAPSHOT'
    compile 'com.github.yongjhih.kotlin-extensions:kotlinx-android-system-services:-SNAPSHOT'
    compile 'com.github.yongjhih.kotlin-extensions:kotlinx-android-notification:-SNAPSHOT'
    compile 'com.github.yongjhih.kotlin-extensions:kotlinx-math:-SNAPSHOT'
    compile 'com.github.yongjhih.kotlin-extensions:kotlinx-threetenbp:-SNAPSHOT'
    compile 'com.github.yongjhih.kotlin-extensions:kotlinx-android-view:-SNAPSHOT'
}

jcenter (not ready yet):

dependencies {
    compile 'com.infstory:kotlinx-android-sharedpreferences:1.0.0'
    compile 'com.infstory:kotlinx-android-database:1.0.0'
    compile 'com.infstory:kotlinx-android-system-services:1.0.0'
    compile 'com.infstory:kotlinx-android-notification:1.0.0'
    compile 'com.infstory:kotlinx-math:1.0.0'
    compile 'com.infstory:kotlinx-android-view:1.0.0'
}

License

Copyright 2015 8tory, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.