Skip to content
This repository has been archived by the owner on Feb 12, 2018. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoav Sternberg committed Mar 4, 2015
0 parents commit 2e26ce6
Show file tree
Hide file tree
Showing 50 changed files with 30,155 additions and 0 deletions.
80 changes: 80 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Created by .ignore support plugin (hsz.mobi)
### Android template
# Built application files
*.apk
*.ap_

# Files for the Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/

# Gradle files
.gradle/
build/
/*/build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files
*.log


### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties


280 changes: 280 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
Android Kotlin Extensions
=========================
A collection of Kotlin extensions for Android,
based on [KotlinAndroidLib](https://github.com/vladlichonos/kotlinAndroidLib) and [Android Kotlin Extensions](https://github.com/ragunathjawahar/android-kotlin-extensions).

Activities & Services
-------------------------
#### Example: Start an Activity
**Java**
```java
Intent intent = new Intent(this, SuperPowersActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
```

**Kotlin**
```kotlin
val flags = flags(Intent.FLAG_ACTIVITY_NEW_TASK, Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity<SuperPowersActivity>(flags)
```

#### Example: Create an Intent
**Java**
```java
Intent intent = new Intent(this, SuperPowersActivity.class);
```

**Kotlin**
```kotlin
val intent = getIntent<SuperPowersActivity>()
```

Api
-------------------------
**Java**
```java
int sdk = Build.VERSION.SDK_INT;
if (sdk >= Build.VERSION_CODES.LOLLIPOP) {}
if (sdk < Build.VERSION_CODES.LOLLIPOP) {}
if (sdk >= Build.VERSION_CODES.KITKAT) {}
if (sdk < Build.VERSION_CODES.KITKAT) {}
if (sdk >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {}
if (sdk < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {}
if (sdk >= X) {}
if (sdk < X) {}
```

**Kotlin**
```kotlin
val sdk = currentVersion()
if (lollipopOrNewer()) {}
if (beforeLollipop()) {}
if (kitkatOrNewer()) {}
if (beforeKitkat()) {}
if (icsOrNewer()) {}
if (beforeIcs()) {}
if (versionOrNewer(x)) {}
if (beforeVersion(x)) {}
```

Async and main thread
-------------------------
**Kotlin**
```kotlin
async {

}

mainThread {

}
```

Broadcast Receiver
-------------------------
**Java**
```java
new BroadcastReceiver() {
@Override
public void onReceive(Context context,Intent intent) {
}
};
```
**Kotlin**
```kotlin
broadcastReceiver { context,intent -> }
```

Layout Inflation
-------------------------
**Java**
````java
LayoutInflater layoutInflater = LayoutInflater.from(this);
View awesomeView = layoutInflater.inflate(R.layout.my_awesome_layout, null);
````

**Kotlin**
````kotlin
val awesomeView = inflateLayout(R.layout.my_awesome_layout)
````

Logs
-------------------------
#### Example: Logging
**Java**
```java
String tag = this.getClass().getName();
Log.i(tag, "Howdy! Info");
Log.d(tag, "Knock knock! Debug");
Log.e(tag, "Grim, Error");
Log.wtf(tag, "Damn! WTF"); // Nope, not what you think. Refer the docs :P
```

**Kotlin**

```kotlin
i("Howdy! Info")
d("Knock knock! Debug")
e("Grim, Error")
wtf("Damn! WTF")
```

Preferences
-------------------------
#### Example: Single Preference
**Java**
```java
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = preferences.edit();
editor.putBoolean("KEY_WALKTHROUGH_COMPLETE", complete);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
editor.commit();
} else {
editor.apply();
}
```

**Kotlin**
```kotlin
getDefaultSharedPreferences().putBoolean("KEY_WALKTHROUGH_COMPLETE", complete)
```

#### Example: Bulk Preferences
**Kotlin**
```kotlin
getDefaultSharedPreferences()
.bulk()
.putBoolean("KEY_WALKTHROUGH_COMPLETE", complete)
.putString("KEY_LAST_USED", lastUsedIso8601Date)
.applyBulk()
```

Toasts
-------------------------
#### Example: Short Message
**Java**
```java
Toast.makeText(this, R.string.welcome, Toast.LENGTH_SHORT).show();
```

**Kotlin**
```kotlin
toast(R.string.welcome)
```

#### Example: Long Message
**Java**
```java
Toast.makeText(this, R.string.welcome, Toast.LENGTH_LONG).show();
```

**Kotlin**
```kotlin
longToast(R.string.welcome)
```

System Services
-------------------------
#### Example: System service on code

**Java**
```java
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
```

**Kotlin**
```kotlin
val alarmManager = context.alarmManager()
val notificationManager = context.notificationManager()
```

#### Example: System service field (lazy)
**Java**
```java
AlarmManager alarmManager;
NotificationManager notificationManager;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
```

**Kotlin**
```kotlin
val alarmManager: AlarmManager by systemService()
val notificationManager: NotificationManager by systemService()
```

Resources
-------------------------
#### Example: Typeface from Assets
**Java**
```java
AssetManager assetManager = getAssets();
Typeface sourceSansPro = Typeface.createFromAsset(assetManager, "fonts/source-sans-pro.ttf");
```

**Kotlin**
```kotlin
val sourceSansPro = typefaceFromAssets("fonts/source-sans-pro.ttf")
```

#### Example: Get resources
**Java**
```java
String text = getResources().getString(R.string.text);
int number = getResources().getInteger(R.integer.number);
int dimenPixelSize = getResources().getDimensionPixelSize(R.dimen.size);
int color = getResources().getColor(R.color.red);
int[] intArray = getResources().getIntArray(R.array.ids);
String[] stringArray = getResources().getColor(R.array.options);
Drawable drawable = getResources().getDrawable(R.drawable.background);
```

**Kotlin**
```kotlin
val text = stringRes(R.string.text)
val number = intRes(R.integer.number)
val dimenPixelSize = dimenRes(R.dimen.size)
val color = colorRes(R.color.red)
val intArray = intArrayRes(R.array.ids)
val stringArray = stringArrayRes(R.array.options)
val drawable = drawableRes(R.drawable.background)
```

#### Example: Get resources as field (lazy)
**Kotlin**
```kotlin
val text by stringResource(R.string.text)
val number by intResource(R.integer.number)
val dimenPixelSize by dimenResource(R.dimen.size)
val color by colorResource(R.color.red)
val intArray by intArrayResource(R.array.ids)
val stringArray by stringArrayResource(R.array.options)
val drawable by drawableResource(R.drawable.background)
```

More...
-------------------------
More extensions under development.

License
-------------------------
Copyright 2015 Yoav sternberg

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.
Loading

0 comments on commit 2e26ce6

Please sign in to comment.