Working with KTX Libraries

Hello, Habr. In anticipation of the start of the courses " Android Developer. Basic " and " Android Developer. Professional " we have prepared a translation of the material.






Android Java API Kotlin, , Kotlin, . , API, Jetpack KTX. 20 KTX , Java API, API Android ViewModels, SQLite Play Core. API , , .





, :





ktx



, -ktx



, . -ktx



-ktx , . , viewmodel



2 : viewmodel



viewmodel-ktx



. -ktx



Kotlin:





// Java language implementation
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
// Kotlin implementation
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
      
      



-ktx 

API Android core-ktx



.





implementation "androidx.core:core-ktx:$corektx_version"
      
      



ktx



, Android Studio.





, , , Color, KTX.





API β€” core-ktx

core-ktx



Kotlin API Android.





, SharedPreferences



, , :





/* Copyright 2020 Google LLC.	
   SPDX-License-Identifier: Apache-2.0 */
   
- val editor = sharedPreferences.edit()
- editor.putBoolean(SHOW_DELETED_WORDS_KEY, enable)
- editor.apply()
+ sharedPreferences.edit {
+ putBoolean(SHOW_DELETED_WORDS_KEY, enable)
+ }
      
      



SharedPrefs.kt.diff GitHub





edit ktx , commit β€” apply()



. apply ()



, commit()



, :





// From https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:core/core-ktx/src/main/java/androidx/core/content/SharedPreferences.kt;l=39?q=SharedPreferences.kt

public inline fun SharedPreferences.edit(
    commit: Boolean = false,
    action: SharedPreferences.Editor.() -> Unit
) {
    val editor = edit()
    action(editor)
    if (commit) {
        editor.commit()
    } else {
        editor.apply()
    }
}
      
      



SharedPreferences.kt GitHub





core-ktx



. , EditText



, TextWatcher



, onTextChanged()



. core-ktx



TextWatcher



: doOnTextChanged, doAfterTextChanged doBeforeTextChanged, Kotlin , :





/* Copyright 2020 Google LLC.	
   SPDX-License-Identifier: Apache-2.0 */
   
- editWordView.addTextChangedListener(object : TextWatcher {
-     override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
-         handleTextChanged(s)
-     }
-
-     override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
-     }
-
-     override fun afterTextChanged(s: Editable) {
-     }
- })
+ editWordView.doOnTextChanged { text, start, count, after -> handleTextChanged(text) }
      
      



Edit.kt.diff GitHub





: , , nullability



.





API AnimatorListener TransitionListener.





doOnTextChanged TextView



β€” , addTextChangedListener



. doOnTextChanged



TextWatcher



.





API Jetpack

API Jetpack. , .





LiveData

LiveData : map, switchMap distinctUntilChanged ().





, map liveData-ktx



Transformations.map(liveata) { /* map function */ }



liveData.map



Kotlin.





LiveData



, Observer. observe



lifecycle-ktx



. , androidx.lifecycle.observe



, .





/* Copyright 2020 Google LLC.	
   SPDX-License-Identifier: Apache-2.0 */
   
- wordViewModel.allWords.observe(
-     this,
-     Observer { words ->
-         // Update the cached copy of the words in the adapter.
-         words?.let { adapter.submitList(it) }
-     }
- )
+ wordViewModel.allWords.observe(owner = this) { words ->
+     // Update the cached copy of the words in the adapter.
+     words.let { adapter.submitList(it) }
+ }
      
      



Observe.kt.diff GitHub





LiveData



, , Flow LiveData



LiveData



Flow



, lifecycle-livedata-ktx



: Flow.asLiveData() LiveData.asFlow().





Activity / Fragment ViewModel

ViewModel



, ViewModel ViewModelProvider.Factory, ViewModel



. , viewModels ( ): by viewModels(factory)



:





/* Copyright 2020 Google LLC.	
   SPDX-License-Identifier: Apache-2.0 */
   
+ private val wordViewModel: WordViewModel by viewModels {
+     WordViewModelFactory(repository)
+ }
override fun onCreate(savedInstanceState: Bundle?) {
…
-     val viewModelFactory = WordViewModelFactory(repository)
-     val viewModel = ViewModelProvider(this, viewModelFactory).get(WordViewModel::class.java)
}
      
      



MyActivity.kt.diff GitHub





viewModels



-ktx activity



fragment



.





ViewModel



. , , ViewModel



. CoroutineScope



, viewModelScope. ViewModel.onCleared(). viewModelScope



.





Room WorkManager

Room, WorkManager -ktx



. , , MAD ​​Skills, !





KTX

AndroidX β€” , KTX :





  • Firebase Kotlin





  • Google Maps Maps Places ktx





  • Play Core core-ktx,





, Kotlin- β€” , , -ktx



. , Kotlin Jetpack !





: "Android Developer. Basic" / "Android Developer. Professional".





:

β€’
Β«- Β» β€” Android 2 .





β€’ Β« AndroidΒ»:

-

- Canvas, Path, Paint

-








All Articles