Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Membangun Modern App dengan Jetpack & Android A...

Membangun Modern App dengan Jetpack & Android Architecture Component - SIGMATECH

Ahmad Arif Faizin

October 28, 2022
Tweet

More Decks by Ahmad Arif Faizin

Other Decks in Programming

Transcript

  1. Membangun Modern App dengan Jetpack & Android Architecture Component Ahmad

    Arif Faizin Curriculum Developer - Dicoding Indonesia
  2. Our Company Mercury is the closest planet to the Sun

    and the smallest one in the Solar System—it’s only a bit larger than the Moon. The planet’s name has nothing to do with the liquid metal, since it was named after the Roman messenger god, Mercury
  3. Table of Contents Modern App 01 Android Jetpack 02 Architecture

    Layer 03 Android Architecture Component 04
  4. Hindari short-term hack! • Mendesain aplikasi hanya untuk perangkat tertentu

    saja • Copy paste kode ke dalam file Anda secara membabi buta • Menuliskan secara hardcode untuk string yang tampil • Menempatkan semua business logic dalam file Activity
  5. Mengapa Perlu Architecture? • Separation of Concern (SoC), memisahkan business

    logic dan ui logic. • Memudahkan developer untuk saling berkolaborasi. • Kode jadi lebih mudah dites. • Menghemat waktu dan mengurangi technical debt saat project berkembang.
  6. Best Practice • Jangan jadikan activity, services, & broadcast sebagai

    sumber data. • Buat batasan yang jelas antar module. • Expose sedikit mungkin data pada setiap module. • Pertimbangkan cara membuat setiap modul dapat diuji secara terpisah. • Jangan menulis kode yang sama berulang-ulang (boilerplate) • Sebisa mungkin simpan data yang relevan dan terbaru. • Tetapkan satu sumber data sebagai single source of truth.
  7. Komponen AAC • ViewModel - Handle data ke UI dan

    mengatasi configuration change • LiveData - data holder yang lifecycle-aware dan bisa di-observe secara real-time untuk mendapatkan data terbaru • Repository - untuk mengatur beberapa sumber data (network, database, cache) • Room - abstraksi di atas SQLite, untuk simpan database dengan lebih simple dan mudah
  8. Contoh Bad Code : All in One Activity class MainActivity

    : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) ... btnCalculate.setOnClickListener { val length = edtLength.text.toString().toDouble() val width = edtWidth.text.toString().toDouble() val height = edtHeight.text.toString().toDouble() //business logic in Activity (DON’T!) val volume = length * width * height tvResult.text = volume.toString() } } }
  9. Membuat Model dan ViewModel class MainRepository { fun getVolume(length:Double, width:Double,

    height:Double): Double { return length * width * height } } MainRepository.kt class MainViewModel : ViewModel() { var result = 0.0 var mainRepo = MainRepository() fun calculate(width: Double, height: Double, length: Double) { result = mainRepo.getVolume(width, height, length) } } MainViewModel.kt
  10. Contoh Good Code : Menerapkan ViewModel class MainActivity : AppCompatActivity()

    { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) ... val viewModel=ViewModelProvider(this).get(MainViewModel::class.java) tvResult.text = viewModel.result.toString() btnCalculate.setOnClickListener { val length = edtLength.text.toString().toDouble() val width = edtWidth.text.toString().toDouble() val height = edtHeight.text.toString().toDouble() viewModel.calculate(width, height, length) //no business logic in Activity (DO!) } } }
  11. —Robert C. Martin ““Good architecture makes the system easy to

    understand, easy to develop, easy to maintain, and easy to deploy.”
  12. CREDITS: This presentation template was created by Slidesgo, including icons

    by Flaticon, and infographics & images by Freepik Thanks Do you have any questions? [email protected] 0857 4048 2440 https://instagram.com/arif_faizin arifaizin.medium.com Please keep this slide for attribution