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

Inside Jetpack

Inside Jetpack

kobito-kaba

November 28, 2018
Tweet

More Decks by kobito-kaba

Other Decks in Programming

Transcript

  1. • Started as a Venture between Softbank and Yahoo •

    One of biggest search in Japan • Not a part of big
  2. 2018 -JGFDZDMFT -JWF%BUB 7JFX.PEFM 3PPN 1BHJOH %BUB#JOEJOH /BWJHBUJPO 8PSL.BOBHFS %PXOMPBE.BOBHFS

    .FEJB1MBZCBDL 1FSNJTTJPOT /PUJDBUJPOT 4IBSJOH 4MJDFT "OJNBUJPO5SBOTJUJPOT "VUP 578FBS &NPKJ 'SBHNFOU -BZPVU 1BMFUUF "QQ$PNQBU "OESPJE,59 .VMUJEFY 5FTU
  3. class NetworkReceiver(val listener : (NetworkInfo) -> Unit) : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) { val con = context.getSystemService(ConnectivityManager::class.java) listener(con.activeNetworkInfo) } }
  4. class NetworkReceiver(val listener : (NetworkInfo) -> Unit) : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) { val con = context.getSystemService(ConnectivityManager::class.java) listener(con.activeNetworkInfo) } }
  5. class NetworkReceiver(val listener : (NetworkInfo) -> Unit) : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) { val con = context.getSystemService(ConnectivityManager::class.java) listener(con.activeNetworkInfo) } }
  6. class MainActivity : AppCompatActivity() { private lateinit var networkReceiver :

    NetworkReceiver override fun onCreate(...) { networkReceiver = NetworkReceiver { // do something } val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION) registerReceiver(networkReceiver, filter) } override fun onDestroy() { unregisterReceiver(networkReceiver) } }
  7. class MainActivity : AppCompatActivity() { private lateinit var networkReceiver :

    NetworkReceiver override fun onCreate(...) { networkReceiver = NetworkReceiver { // do something } val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION) registerReceiver(networkReceiver, filter) } override fun onDestroy() { unregisterReceiver(networkReceiver) } }
  8. class MainActivity : AppCompatActivity() { private lateinit var networkReceiver :

    NetworkReceiver override fun onCreate(...) { networkReceiver = NetworkReceiver { // do something } val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION) registerReceiver(networkReceiver, filter) } override fun onDestroy() { unregisterReceiver(networkReceiver) } }
  9. class NetworkObserver(val owner : LifecycleOwner, private val context: Context, private

    val listener: (NetworkInfo) -> Unit) : LifecycleObserver { private val receiver = object : BroadcastReceiver() { ... } }
  10. class NetworkObserver(val owner : LifecycleOwner, private val context: Context, private

    val listener: (NetworkInfo) -> Unit) : LifecycleObserver { init { owner.lifecycle.addObserver(this) } private val receiver = object : BroadcastReceiver() { ... } }
  11. ... @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) fun register() { val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION) context.registerReceiver(receiver,

    filter) } @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) fun unregister() { context.unregisterReceiver(receiver) } }
  12. object CatRepository { private val _cat = MutableLiveData<Cat>() fun loadCat(catId

    : Int) : LiveData<Cat> { fetch(catId) { newCat -> _cat.postValue(newCat) } return _cat } }
  13. class CatActivity : AppCompatActivity() { override fun onCreate(...) { val

    catId = intent.getIntExtra("cat_id") CatRepository.loadCat(catId).observe(this, Observer { cat -> showCat(cat)}) } }
  14. class CatActivity : AppCompatActivity() { override fun onCreate(...) { ...

    val relatedCatsModule = RelatedCatsModuleAdapter { clicked -> val intent = Intent(this, CatActivity::class.java) intent.putExtra("cat_id", clicked.id) startActivity(intent) } } }
  15. Activity#0 created onCreate onStart onResume onPause onStop onDestroy Device rotated

    Activity#1 created Call Web API onCreate Call Web API
  16. public class FragmentActivity extends ComponentActivity ... { @Override public final

    Object onRetainNonConfigurationInstance() { ... NonConfigurationInstances nci = new NonConfigurationInstances(); nci.custom = custom; nci.viewModelStore = mViewModelStore; nci.fragments = fragments; return nci; }
  17. public class FragmentActivity extends ComponentActivity ... { @Override protected void

    onCreate(...) { ... NonConfigurationInstances nc = (NonConfigurationInstances) getLastNonConfigurationInstance(); if (nc != null && nc.viewModelStore != null && mViewModelStore == null) { mViewModelStore = nc.viewModelStore; }
  18. private val _cat = MutableLiveData<List<Cat>>() fun updateCat(new: Cat) { val

    currentCats = _cat.value ?: return val updatedCats = currentCats.toMutableList() .map { old -> if(old.id == new.id) new else old } _cat.postValue(updatedCats) // update storage and web service ... }
  19. private val _cat = MutableLiveData<List<Cat>>() fun updateCat(new: Cat) { val

    currentCats = _cat.value ?: return val updatedCats = currentCats.toMutableList() .map { old -> if(old.id == new.id) new else old } _cat.postValue(updatedCats) // update storage and web service ... }
  20. private val _cat = MutableLiveData<List<Cat>>() fun updateCat(new: Cat) { val

    currentCats = _cat.value ?: return val updatedCats = currentCats.toMutableList() .map { old -> if(old.id == new.id) new else old } _cat.postValue(updatedCats) // update storage and web service ... }
  21. @Entity data class Cat( @PrimaryKey(autoGenerate = true) val id: Int

    = 0, val name: String, val age: Int, val description: String, val loved: Boolean )
  22. @Dao interface CatDao { @Query("SELECT * from Cat") fun loadCats()

    : LiveData<List<Cat>> @Update fun update(cat: Cat) }
  23. @Database(entities = [Cat::class], version = 1) abstract class CatDatabase :

    RoomDatabase() { abstract fun catDao() : CatDao }
  24. Cat Database Cat table room_master_table Id, identity_hash room_table_modification_log InvalidationTracker Version,

    table_id Observer Cat Dao ComputeLiveDat a addObserver query onInvalidated
  25. Repository ViewModel Activity / Fragment What do I display? I

    need cats Give us ALL Cats OK, 1,000,000 cats here. Here you are Show this 1,000,000 cats ¯\_(π)_/¯
  26. public abstract class PagedListAdapter<T, VH> ... { final AsyncPagedListDiffer<T> mDiffer;

    @Nullable protected T getItem(int position) { return mDiffer.getItem(position); } }
  27. public class AsyncPagedListDiffer<T> { ... public T getItem(int index) {

    ... mPagedList.loadAround(index); return mPagedList.get(index); } }
  28. public abstract class PagedList<T> extends AbstractList<T> { ... public void

    loadAround(int index) { ... loadAroundInternal(index); ... } }
  29. class ContiguousPagedList<K, V> extends PagedList<V> ... { protected void loadAroundInternal(int

    index) { int appendItems = getAppendItemsRequested(...); ... mAppendItemsRequested = Math.max(...); if (mAppendItemsRequested > 0) { scheduleAppend(); } }