droibit/chopsticks


View, resource and preference injection library written by Kotlin.

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.droibit:Chopsticks:1.0.1'
	}
	dependencies {
		implementation("com.github.droibit:Chopsticks:1.0.1")
	}
	<dependency>
	    <groupId>com.github.droibit</groupId>
	    <artifactId>Chopsticks</artifactId>
	    <version>1.0.1</version>
	</dependency>

                            
    libraryDependencies += "com.github.droibit" % "Chopsticks" % "1.0.1"
        
        

                            
    :dependencies [[com.github.droibit/Chopsticks "1.0.1"]]
        
        

Readme


Chopsticks

Build Status jitpack.io Software License

View, resource and preference injection library like Kotter Knife.

View

View injection to support the following classes.

  • Activity
  • Fragment
  • Fragment(androidx)
  • RecyclerView.ViewHolder(androidx)
  • View
class BindViewActivity : AppCompatActivity() {

  private val button: Button by bindView(android.R.id.button1)

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_bind_view)

    button.setOnClickListener {
      Toast.makeText(this@BindViewActivity, "Hello, bindView", Toast.LENGTH_SHORT).show()
    }
  }
}

And, it is also support unbind view.

// e.g. SupportFragment
class BindViewFragment : Fragment(), Binder<Fragment> by SupportFragmentViewBinder() {

  private val textView: TextView by bindView(android.R.id.text1)
  private val button: Button by bindView(android.R.id.button1)

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    retainInstance = true
  }

  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
    return inflater.inflate(R.layout.fragment_bind_view, container, false)
  }

  override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    textView.text = getString(R.string.bind_fragment_text)
    button.setOnClickListener {
      Toast.makeText(context, "Hello, bindView", Toast.LENGTH_SHORT).show()
    }
  }

  override fun onDestroyView() {
    unbindViews()
    super.onDestroyView()
  }
}

Resource

Resource injection to support the following classes.

  • Activity
  • Fragment
  • Fragment(androidx)
  • RecyclerView.ViewHolder(androidx)
  • View
class BindViewActivity : AppCompatActivity() {

  private val button: Button by bindView(android.R.id.button1)
  private val toastMessage: String by bindString(R.string.toast_message)

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_bind_view)

    button.setOnClickListener {
      Toast.makeText(this@BindViewActivity, toastMessage, Toast.LENGTH_SHORT).show()
    }
  }
}

Also support class that does not have context.

class Resources(context: Context) : Binder by ResourceBinder(context) {

  val toastMessage: String by bindString(R.string.toast_message)
}

Preference

Preference injection to support the following classes.

  • PreferenceActivity
  • PreferenceFragment
  • PreferenceFragmentCompat(androidx)
// e.g. PreferenceFragmentCompat
class SettingsFragment: PreferenceFragmentCompat() {

  // String resource is Preference key.
  private val checkboxPref: CheckBoxPreference by bindPreference(R.string.key_checkbox_preference)
  // String literal is Preference key.
  private val switchPref: SwitchPreference by bindPreference("switch_preference")

  override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
    addPreferencesFromResource(R.xml.settings)
  }

  override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    checkboxPref.title = "Edited: ${checkboxPref.title}"
    switchPref.title = "Edited: ${switchPref.title}"
  }
}

Download

Add the following code to build.gradle.

repositories {
  maven { url "https://jitpack.io" }
}

dependencies {
  // If you do not need `androidx.recyclerview:recyclerview`, you can exclude.
  implementation "com.github.droibit.chopsticks:chopstick-view:1.0.0"
  implementation "com.github.droibit.chopsticks:chopstick-resource:1.0.0"

  // If you do not need `androidx.preference:preference`, you can exclude.
  implementation "com.github.droibit.chopsticks:chopstick-preference:1.0.0"
}

License

Copyright (C) 2016 Shinya Kumagai

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.