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

Finalize sample convertion to kotlin + closeScopeOnDestroy #421

Open
wants to merge 1 commit into
base: master
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<EditText>(R.id.new_name)
val button = findViewById<Button>(R.id.add_item)
button.setOnClickListener {
val text = editText.text.toString()
if (backpackItemValidator.isValidName(text)) {
val intent = Intent()
.putExtra(NEW_ITEM_NAME_KEY, text)
setResult(RESULT_OK, intent)
finish()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.toothpick.R
import com.example.toothpick.activity.AddNewActivity.NEW_ITEM_NAME_KEY
import com.example.toothpick.activity.AddNewActivity.Companion.NEW_ITEM_NAME_KEY
import com.example.toothpick.adapter.BackpackAdapter
import com.example.toothpick.adapter.IBackpackAdapter
import com.example.toothpick.annotation.ApplicationScope
Expand Down Expand Up @@ -86,7 +86,6 @@ class SimpleBackpackItemsActivity : AppCompatActivity() {
}
}


private fun setupUIComponents() {
setContentView(R.layout.backpack_list)
coordinatorLayout = findViewById(R.id.coordinatorLayout)
Expand Down