Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/sample kotlin #155

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ ext {
}

buildscript {
ext.kotlin_version = '1.2.10'
repositories {
google()
jcenter()
Expand All @@ -52,6 +53,7 @@ buildscript {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.novoda:bintray-release:0.8.0'
classpath 'pt.simdea.verifier:verifier:3.6.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

Expand Down
32 changes: 32 additions & 0 deletions docs/adal-accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
```

### Usage
#### Java
```java
public class FragmentAccounts extends BaseFragment {

Expand Down Expand Up @@ -49,6 +50,37 @@ public class FragmentAccounts extends BaseFragment {
}
}
```
#### Kotlin
```kotlin
class FragmentAccounts : BaseFragment() {

override fun doOnCreated() {
AccountHelper.initialize(getActivity())

getAccount()
addAccount()
clearAccount()
}

private fun clearAccount() {
AccountHelper.clearAccounts(context) { }
}

private fun addAccount() {
AccountHelper.addAccount(getContext(), "hardcoded_name", "hardcoded_password", "hardcoded_token")
}

private fun getAccount() {
val account = AccountHelper.getCurrentAccount(context)
if (account != null) {
Toast.makeText(getContext(), "Name: " + account.name + " \nPassword: " + AccountHelper.getAccountPassword(account) + " \ntoken: " + AccountHelper.getCurrentToken(account, getContext()), Toast.LENGTH_LONG).show()
} else {
Toast.makeText(getContext(), "No account available" , Toast.LENGTH_LONG).show()
}
}
}
```

### Contributing
[CONTRIBUTING](../CONTRIBUTING.md)

Expand Down
37 changes: 36 additions & 1 deletion docs/adal-adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies {
}
```
### Usage

#### Java
Create a new adapter:
```java
public class AdapterPhoto extends AbstractLoadMoreBaseAdapter<Photo> {
Expand Down Expand Up @@ -62,6 +62,41 @@ mAdapterPost.setOnLoadMoreListener(new OnLoadMoreListener() {
});
```

#### Kotlin
Create a new adapter:
```kotlin
class AdapterPhoto : AbstractLoadMoreBaseAdapter<Photo>(R.layout.adapter_photo, R.layout.adapter_loading, ArrayList()) {

override fun bindItem(holder: BaseViewHolder, item: Photo) {
val imgThumbnail = holder.getView<ImageView>(R.id.imgThumbnail)

Glide.with(imgThumbnail.context).load(item.mThumbnailUrl).into(imgThumbnail)
holder.setText(R.id.txtTitle, item.mTitle)
holder.setText(R.id.txtDescription, item.mTitle)
}

override fun bindError(holder: BaseViewHolder, loadingError: Boolean) {
val lnlLoading = holder.getView<LinearLayout>(R.id.lnrLoading)
val lnlError = holder.getView<LinearLayout>(R.id.lnrError)

if (loadingError) {
lnlLoading.visibility = View.GONE
lnlError.visibility = View.VISIBLE
}
else {
lnlLoading.visibility = View.VISIBLE
lnlError.visibility = View.GONE
}
}
}
```

Set the adapter:
```kotlin
mAdapterPost = AdapterPost()
mAdapterPost!!.setOnLoadMoreListener({ request() })
```

### Contributing
[CONTRIBUTING](../CONTRIBUTING.md)

Expand Down
31 changes: 30 additions & 1 deletion docs/adal-alarm.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies {
}
```
### Usage

#### Java
```java
/**
* Add a new alarm to the system.
Expand Down Expand Up @@ -44,6 +44,35 @@ private void removeAlarm() {
}
}
```
#### Kotlin
```kotlin
/**
* Add a new alarm to the system.
*/
private fun addAlarm() {
val calendar = Calendar.getInstance()
calendar.time = Date()

if (!AlarmManager.hasAlarm(context, mIntentAlarm, 1001)) {
AlarmManager.addAlarm(context, mIntentAlarm, 1001, calendar)
SnackBuilder.show(mBtnRemoveAlarm, "Alarm added!", R.color.colorAccent)
} else {
SnackBuilder.show(mBtnAddAlarm, "Alarm already added.", R.color.colorAccent)
}
}

/**
* Remove an alarm.
*/
private fun removeAlarm() {
if (AlarmManager.hasAlarm(context, mIntentAlarm, 1001)) {
AlarmManager.cancelAlarm(context, mIntentAlarm, 1001)
SnackBuilder.show(mBtnRemoveAlarm, "Alarm removed!", R.color.colorAccent)
} else {
SnackBuilder.show(mBtnRemoveAlarm, "Please, add an alarm first!", R.color.colorAccent)
}
}
```

### Contributing
[CONTRIBUTING](../CONTRIBUTING.md)
Expand Down
69 changes: 66 additions & 3 deletions docs/adal-analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ dependencies {
// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'
```
### Usage with Google Analytics
### Usage
#### Java
##### Usage with Google Analytics
```java
public class FragmentAnalytics extends BaseFragment {
@Override
Expand All @@ -40,7 +42,7 @@ public class FragmentAnalytics extends BaseFragment {
.setVariant("Black")
.setPosition(1)
.setCustomDimension(1, "Member");
AnalyticsManager.with(getContext()).sendProduct(product, "Search Results", "searchResults");
AnalyticsManager.with(getContext()).sendImpression(product, "Search Results", "searchResults");

// Send product action (product can be null in this case)
ProductAction productAction = new ProductAction(ProductAction.ACTION_CLICK)
Expand All @@ -61,7 +63,7 @@ public class FragmentAnalytics extends BaseFragment {
...
}
```
### Usage with Firebase Analytics
##### Usage with Firebase Analytics
```java
public class FragmentFirebaseAnalytics extends BaseFragment {
@Override
Expand All @@ -82,6 +84,67 @@ public class FragmentFirebaseAnalytics extends BaseFragment {
...
}
```

#### Kotlin
##### Usage with Google Analytics
```kotlin
class FragmentAnalytics : BaseFragment() {
override fun doOnCreated() {
// Send a screen to GA
AnalyticsManager.with(context!!).sendScreen("FragmentAnalytics")

// Send Enhanced Ecommerce actions to GA
// Send impression
val product = Product()
.setId("P12345")
.setName("Android Warhol T-Shirt")
.setCategory("Apparel/T-Shirts")
.setBrand("Google")
.setVariant("Black")
.setPosition(1)
.setCustomDimension(1, "Member")
AnalyticsManager.with(context!!).sendImpression(product, "Search Results", "searchResults")

// Send product action (product can be null in this case)
val productAction = ProductAction(ProductAction.ACTION_CLICK)
.setProductActionList("Search Results")
AnalyticsManager.with(context!!).sendProduct(productAction, product, null, "searchResults")

// Send promotion
val promotion = Promotion()
.setId("PROMO_1234")
.setName("Summer Sale")
.setCreative("summer_banner2")
.setPosition("banner_slot1")
AnalyticsManager.with(context!!).sendPromotion(promotion, "promotions")

// Send promotion action
AnalyticsManager.with(context!!).sendPromotionAction(promotion, Promotion.ACTION_CLICK, "Internal Promotions", "click", "Summer Sale", "promotions")
}
...
}
```
##### Usage with Firebase Analytics
```kotlin
class FragmentFirebaseAnalytics : BaseFragment() {
override fun doOnCreated() {
// Send a screen to FA
FirebaseAnalyticsManager.sendScreen(activity, R.string.analytics_screen_main)

// Send an event to FA
val hashMap = HashMap<String, String>()
hashMap.put("event_category", "Category")
hashMap.put("event_action", "Action")
hashMap.put("event_label", "Label")
FirebaseAnalyticsManager.sendEvent(activity, "EventName", hashMap)

// Send a user property to FA
FirebaseAnalyticsManager.sendUserProperty(activity, "Property", "Value")
}
...
}
```

### Contributing
[CONTRIBUTING](../CONTRIBUTING.md)

Expand Down
42 changes: 42 additions & 0 deletions docs/adal-application-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
```

### Usage
#### Java
```java
public class App extends Application implements ApplicationStateManager.BackAndForegroundListener {

Expand Down Expand Up @@ -64,6 +65,47 @@ application.registerActivityLifecycleCallbacks(new ApplicationStateManager() {
});
```

#### Kotlin
```kotlin
class App : Application(), ApplicationStateManager.BackAndForegroundListener {

var TAG: String = App::class.simpleName!!
private var mApplicationStateManager: ApplicationStateManager? = null

override fun onCreate() {
super.onCreate()
mApplicationStateManager = ApplicationStateManager(this)
registerActivityLifecycleCallbacks(mApplicationStateManager)
}

override fun onBackground() {
Log.d(TAG, "onBackground")
}

override fun onForeground() {
Log.d(TAG, "onForeground")
}

fun isBackground(): Boolean = mApplicationStateManager!!.isBackground

fun isForeground(): Boolean = mApplicationStateManager!!.isForeground
}
```

Another way of using `ApplicationStateManager` with a `context` reference.
```kotlin
val application = context.getApplicationContext() as Application
application.registerActivityLifecycleCallbacks(object : ApplicationStateManager() {
override fun onActivityResumed(activity: Activity) {
doOnActivityResumed(activity)
}

override fun onActivityPaused(activity: Activity) {
doOnActivityPaused(activity)
}
})
```

### Contributing
[CONTRIBUTING](../CONTRIBUTING.md)

Expand Down
49 changes: 49 additions & 0 deletions docs/adal-bus.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
```

### Usage
#### Java
* Subscriber
```java
public static final String BANG_A = "BANG_A";
Expand Down Expand Up @@ -60,6 +61,54 @@ BangBus.with(getContext())
.bang();
```

#### Kotlin
* Subscriber
```kotlin
companion object {
val BANG_A: String = "BANG_A"
}
private var mBangBus: BangBus? = null

// Initialize
mBangBus = BangBus(activity)

// Subscribe with Action
@BangBus.SubscribeBang(action = "BANG_A")
fun bangWithAction(message: String) {
mTxtResult!!.text = message
}

// Subscribe Without Action
@BangBus.SubscribeBang
fun bangWithoutAction(number: Int?) {
mTxtResult!!.text = number.toString()
}

// Unsubscribe
@Override
override fun onDestroy() {
mBangBus!!.unsubscribe()
super.onDestroy()
}
```

* Sender
```kotlin
val BANG_MESSAGE_WITH_ACTION = "received bang with action"
val BANG_NUMBER_WITHOUT_NUMBER = 666

// Send with Action
BangBus.with(context)
.addAction(FragmentA.BANG_A)
.setParameter(BANG_MESSAGE_WITH_ACTION)
.bang()

// Send Bang Without Action
BangBus.with(context)
.setParameter(BANG_NUMBER_WITHOUT_NUMBER)
.bang()
```

### Contributing
[CONTRIBUTING](../CONTRIBUTING.md)

Expand Down
Loading