Skip to content

Commit

Permalink
Improve Java support
Browse files Browse the repository at this point in the history
  • Loading branch information
gouline committed Apr 2, 2017
1 parent 2dfe308 commit c8c6945
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import kotlin.reflect.KProperty
*/
sealed class Delegate<in M, T>(internal val initializer: M.() -> T?) {

internal var value: T? = null
var value: T? = null
internal set

/**
* Initializes value from the injection module.
Expand Down
3 changes: 3 additions & 0 deletions samples/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".view.CounterActivity"
android:label="@string/title_counter"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2017 Traversal Space
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package space.traversal.kapsule.demo.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import kotlin.jvm.functions.Function1;
import space.traversal.kapsule.Delegate;
import space.traversal.kapsule.Kapsule;
import space.traversal.kapsule.demo.App;
import space.traversal.kapsule.demo.R;
import space.traversal.kapsule.demo.data.Dao;
import space.traversal.kapsule.demo.di.Module;

/**
* Counter activity showcasing that Kapsule still works with Java, even if it's not pretty.
*/
public class CounterActivity extends AppCompatActivity {

private final Kapsule<Module> mKap = new Kapsule<>();
private final Delegate<Module, Dao> mDao = mKap.required(new Function1<Module, Dao>() {
@Override
public Dao invoke(Module module) {
return module.getDao();
}
});

private TextView mCounterTextView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_counter);
mKap.inject(App.Companion.module(this));

mCounterTextView = (TextView) findViewById(R.id.txt_counter);
}

@Override
protected void onStart() {
super.onStart();

updateCount();
}

/**
* Fetches count from the DAO and applies it to the text view.
*/
private void updateCount() {
if (mDao.getValue() != null) {
String text = Integer.toString(mDao.getValue().fetchCount());
mCounterTextView.setText(text);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

package space.traversal.kapsule.demo.view

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import kotlinx.android.synthetic.main.activity_main.*
import space.traversal.kapsule.Injects
import space.traversal.kapsule.Kapsule
Expand Down Expand Up @@ -43,6 +46,19 @@ class HomeActivity : AppCompatActivity(), HomeView, Injects<Module> {
presenter.load()
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem?) = when (item?.itemId) {
R.id.menu_counter -> {
startActivity(Intent(this, CounterActivity::class.java))
true
}
else -> super.onOptionsItemSelected(item)
}

override fun onDestroy() {
presenter.detach()
super.onDestroy()
Expand Down
20 changes: 20 additions & 0 deletions samples/android/src/main/res/layout/activity_counter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2017 Traversal Space
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/txt_counter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp"
tools:text="10"/>
20 changes: 20 additions & 0 deletions samples/android/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2017 Traversal Space
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/menu_counter"
android:title="@string/title_counter"
app:showAsAction="ifRoom"/>

</menu>
3 changes: 3 additions & 0 deletions samples/android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<resources>

<string name="app_name">Kapsule Demo</string>

<string name="title_counter">Counter</string>

<string name="button_add">Add</string>
<string name="button_remove">Remove</string>

Expand Down

0 comments on commit c8c6945

Please sign in to comment.