diff --git a/toothpick-sample/src/main/java/com/example/toothpick/activity/AddNewActivity.java b/toothpick-sample/src/main/java/com/example/toothpick/activity/AddNewActivity.java deleted file mode 100644 index 7a8287e9..00000000 --- a/toothpick-sample/src/main/java/com/example/toothpick/activity/AddNewActivity.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.example.toothpick.activity; - -import android.app.Activity; -import android.content.Intent; -import android.os.Bundle; -import android.widget.Button; -import android.widget.EditText; -import androidx.appcompat.app.AppCompatActivity; -import com.example.toothpick.R; -import com.example.toothpick.annotation.ApplicationScope; -import com.example.toothpick.helper.BackpackItemValidator; -import javax.inject.Inject; -import toothpick.Toothpick; - -public class AddNewActivity extends AppCompatActivity { - - public final static String NEW_ITEM_NAME_KEY = "name"; - - //will be created as a singleton in the root scope - //and is releasable under memory pressure - @Inject BackpackItemValidator backpackItemValidator; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // 1. Open Activity scope as child of Application scope - // 2. Inject dependencies - Toothpick.openScopes(ApplicationScope.class, this) - .inject(this); - - setupUIComponents(); - } - - private void setupUIComponents() { - setContentView(R.layout.backpack_new); - final EditText editText = findViewById(R.id.new_name); - Button button = findViewById(R.id.add_item); - button.setOnClickListener( view -> { - String text = editText.getText().toString(); - if (backpackItemValidator.isValidName(text)) { - Intent intent = new Intent() - .putExtra(NEW_ITEM_NAME_KEY, text); - setResult(Activity.RESULT_OK, intent); - finish(); - } - }); - } -} diff --git a/toothpick-sample/src/main/java/com/example/toothpick/activity/AddNewActivity.kt b/toothpick-sample/src/main/java/com/example/toothpick/activity/AddNewActivity.kt new file mode 100644 index 00000000..e524bd65 --- /dev/null +++ b/toothpick-sample/src/main/java/com/example/toothpick/activity/AddNewActivity.kt @@ -0,0 +1,54 @@ +package com.example.toothpick.activity + +import android.content.Intent +import android.os.Bundle +import android.view.View +import android.widget.Button +import android.widget.EditText +import androidx.appcompat.app.AppCompatActivity +import com.example.toothpick.R +import com.example.toothpick.annotation.ApplicationScope +import com.example.toothpick.helper.BackpackItemValidator +import toothpick.Toothpick +import toothpick.ktp.KTP +import toothpick.smoothie.lifecycle.closeOnDestroy +import javax.inject.Inject + +class AddNewActivity : AppCompatActivity() { + + companion object { + const val NEW_ITEM_NAME_KEY = "name" + } + + //will be created as a singleton in the root scope + //and is releasable under memory pressure + @Inject + lateinit var backpackItemValidator: BackpackItemValidator + + public override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + // 1. Open Activity scope as child of Application scope + // 2. Inject dependencies + KTP.openScope(ApplicationScope::class.java) + .openSubScope(this) + .closeOnDestroy(this) + .inject(this) + setupUIComponents() + } + + private fun setupUIComponents() { + setContentView(R.layout.backpack_new) + val editText = findViewById(R.id.new_name) + val button = findViewById