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

Convert Java to Kotlin Shibuya.apk 16

Convert Java to Kotlin Shibuya.apk 16

# Event
https://shibuya-apk.connpass.com/event/59671/

# Links

- 2016年、KotlinでAndroid開発する方へ
- http://qiita.com/satorufujiwara/items/871c5b7b66c7691d82a8

- Kotlin スコープ関数 用途まとめ@Qiita
- http://qiita.com/ngsw_taro/items/d29e3080d9fc8a38691e

- Kotlin + Architecture Component + Dagger2によるAndroidアプリ設計
- http://qiita.com/satorufujiwara/items/0f95ccfc3820d3ee1370

- Kotlin入門までの助走読本
- http://goo.gl/5vUT7o

satorufujiwara

June 22, 2017
Tweet

More Decks by satorufujiwara

Other Decks in Programming

Transcript

  1. Kotlin & Me FRESH! ΛKotlinͰ։ൃ (2015೥3݄ʙ) λοϓϧ஀ੜͷKotlinԽ (2017೥6݄ʙ) Kotlin Advent

    Calendar (2015/2016) -ʮ2016೥ɺKotlinͰAndroid։ൃ͢Δํ΁ʯ @Qiita ( goo.gl/0medax ) Kotlinೖ໳·Ͱͷॿ૸ಡຊ ( goo.gl/5vUT7o )
  2. //com.example.android.persistence.ProductFragment //Java private static final String KEY_PRODUCT_ID = “product_id"; //Kotlin

    (Auto) companion object { private val KEY_PRODUCT_ID = “product_id" } //Kotlin (Manual) companion object { private const val KEY_PRODUCT_ID = “product_id" }
  3. //com.example.android.persistence.ProductFragment //Java public static ProductFragment forProduct(int productId) { ProductFragment fragment

    = new ProductFragment(); Bundle args = new Bundle(); args.putInt(KEY_PRODUCT_ID, productId); fragment.setArguments(args); return fragment; } //Kotlin (Auto) companion object { fun forProduct(productId: Int): ProductFragment { val fragment = ProductFragment() val args = Bundle() args.putInt(KEY_PRODUCT_ID, productId) fragment.arguments = args return fragment } }
  4. //com.example.android.persistence.ProductFragment //Java public static ProductFragment forProduct(int productId) { ProductFragment fragment

    = new ProductFragment(); Bundle args = new Bundle(); args.putInt(KEY_PRODUCT_ID, productId); fragment.setArguments(args); return fragment; } //Kotlin (Manual) companion object { fun forProduct(productId: Int) = ProductFragment().apply { arguments = Bundle().apply { putInt(KEY_PRODUCT_ID, productId) } } }
  5. //com.example.android.persistence.ProductFragment //Java private CommentAdapter mCommentAdapter; public View onCreateView(LayoutInflater inflater, @Nullable

    ViewGroup container, @Nullable Bundle savedInstanceState) { //... mCommentAdapter = new CommentAdapter(mCommentClickCallback); mBinding.commentList.setAdapter(mCommentAdapter); return mBinding.getRoot(); } //Kotlin (Auto) private var mCommentAdapter: CommentAdapter? = null override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { //... mCommentAdapter = CommentAdapter(mCommentClickCallback) mBinding!!.commentList.adapter = mCommentAdapter return mBinding!!.root }
  6. //com.example.android.persistence.ProductFragment //Java private CommentAdapter mCommentAdapter; public View onCreateView(LayoutInflater inflater, @Nullable

    ViewGroup container, @Nullable Bundle savedInstanceState) { //... mCommentAdapter = new CommentAdapter(mCommentClickCallback); mBinding.commentList.setAdapter(mCommentAdapter); return mBinding.getRoot(); } //Kotlin (Manual) private val mCommentAdapter: CommentAdapter = CommentAdapter(mCommentClickCallback) override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { //... mBinding!!.commentList.adapter = mCommentAdapter return mBinding!!.root }
  7. //com.example.android.persistence.ProductFragment //Java private CommentAdapter mCommentAdapter; public View onCreateView(LayoutInflater inflater, @Nullable

    ViewGroup container, @Nullable Bundle savedInstanceState) { //... mCommentAdapter = new CommentAdapter(mCommentClickCallback); mBinding.commentList.setAdapter(mCommentAdapter); return mBinding.getRoot(); } //Kotlin (Manual) private val mCommentAdapter = CommentAdapter(mCommentClickCallback) override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { //... mBinding!!.commentList.adapter = mCommentAdapter return mBinding!!.root }
  8. //Non-Null & val private val mCommentAdapter = CommentAdapter(activity) //runtime error

    private val mCommentAdapter by lazy { CommentAdapter(activity) }
  9. // Nullable & var private var mCommentAdapter :CommentAdapter? = null

    //Non-Null & var private var mCommentAdapter :CommentAdapter by Delegates.notNull()
  10. // Nullable & var private var mCommentAdapter :CommentAdapter? = null

    //Non-Null & var private var mCommentAdapter :CommentAdapter by Delegates.notNull() //Non-Null & var private lateinit var mCommentAdapter :CommentAdapter
  11. propertyͷ༏ઌॱҐ • Non-Null & val • Non-Null & val (lazy)

    • Non-Null & var (lateinit) • Non-Null & var (lazy) • Nullable & var
  12. //com.example.android.persistence.ProductFragment //Java private ProductFragmentBinding mBinding; public View onCreateView(LayoutInflater inflater, @Nullable

    ViewGroup container, @Nullable Bundle savedInstanceState) { mBinding = DataBindingUtil.inflate(inflater, R.layout.product_fragment, container, false); //... mBinding.commentList.setAdapter(mCommentAdapter); return mBinding.getRoot(); } //Kotlin (Auto) private var mBinding: ProductFragmentBinding? = null override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { mBinding = DataBindingUtil.inflate<ProductFragmentBinding>(inflater!!, R.layout.product_fragment, container, false) //... mBinding!!.commentList.adapter = mCommentAdapter return mBinding!!.root }
  13. //com.example.android.persistence.ProductFragment //Java private ProductFragmentBinding mBinding; public View onCreateView(LayoutInflater inflater, @Nullable

    ViewGroup container, @Nullable Bundle savedInstanceState) { mBinding = DataBindingUtil.inflate(inflater, R.layout.product_fragment, container, false); //... mBinding.commentList.setAdapter(mCommentAdapter); return mBinding.getRoot(); } //Kotlin (Manual) private lateinit var mBinding: ProductFragmentBinding override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { mBinding = DataBindingUtil.inflate<ProductFragmentBinding>(inflater!!, R.layout.product_fragment, container, false) //... mBinding.commentList.adapter = mCommentAdapter return mBinding.root }
  14. //com.example.android.persistence.ProductFragment //Java private ProductFragmentBinding mBinding; public View onCreateView(LayoutInflater inflater, @Nullable

    ViewGroup container, @Nullable Bundle savedInstanceState) { mBinding = DataBindingUtil.inflate(inflater, R.layout.product_fragment, container, false); //... mBinding.commentList.setAdapter(mCommentAdapter); return mBinding.getRoot(); } //Kotlin (Manual) private lateinit var mBinding: ProductFragmentBinding override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { mBinding = DataBindingUtil.inflate<ProductFragmentBinding>(inflater, R.layout.product_fragment, container, false) //... mBinding.commentList.adapter = mCommentAdapter return mBinding.root }
  15. //com.example.android.persistence.ProductFragment //Java private ProductFragmentBinding mBinding; public View onCreateView(LayoutInflater inflater, @Nullable

    ViewGroup container, @Nullable Bundle savedInstanceState) { mBinding = DataBindingUtil.inflate(inflater, R.layout.product_fragment, container, false); //... mBinding.commentList.setAdapter(mCommentAdapter); return mBinding.getRoot(); } //Kotlin (Manual) private lateinit var mBinding: ProductFragmentBinding override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { mBinding = DataBindingUtil.inflate<ProductFragmentBinding>(inflater, R.layout.product_fragment, container, false) //... mBinding.commentList.adapter = mCommentAdapter return mBinding.root }
  16. //com.example.android.persistence.ProductFragment //Java private ProductFragmentBinding mBinding; public View onCreateView(LayoutInflater inflater, @Nullable

    ViewGroup container, @Nullable Bundle savedInstanceState) { mBinding = DataBindingUtil.inflate(inflater, R.layout.product_fragment, container, false); //... mBinding.commentList.setAdapter(mCommentAdapter); return mBinding.getRoot(); } //Kotlin (Manual) private lateinit var mBinding: ProductFragmentBinding override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = DataBindingUtil.inflate<ProductFragmentBinding>(inflater, R.layout.product_fragment, container, false) .also { mBinding = it mBinding.commentList.adapter = mCommentAdapter }.root
  17. είʔϓؔ਺ • let / with / run /apply / also

    • Kotlin είʔϓؔ਺ ༻్·ͱΊ@Qiita • http://qiita.com/ngsw_taro/items/d29e3080d9fc8a38691e • inline prefix public inline fun <T, R> T.run(block: T.() -> R): R = block()
  18. ͓·͚ • github.com/googlesamples/android-architecture-components • ʮArchitecture Componentʯͷαϯϓϧ͕3छྨެ։͞Ε͍ͯΔ • BasicSample - Shows

    how to persist data using a SQLite database and Room. Also uses ViewModels and LiveData. • PersistenceContentProviderSample - Shows how to expose data via a Content Provider using Room. • GithubBrowserSample - An advanced sample that uses the Architecture components, Dagger and the Github API. Requires Android Studio 3.0 canary 1
  19. ͓·͚ • ʰKotlin + Architecture Component + Dagger2ʹΑΔAndroidΞϓϦઃܭʱ@Qiita • goo.gl/z1p8zP

    • GitHubSampleΛKotlinԽ͢Δ্ͰͷϋϚΓՕॴΛ·ͱΊ·ͨ͠ • Ұ൪ϋϚͬͨͷ͸Dagger2ͷMultibindingػೳΛ࢖͏͜ͱ
  20. How to start Kotlin • Official Site : kotl.in/ ,

    kotl.in/android • Kotlin is awesome! (kotlin.link/) • Kotlin ελʔτϒοΫ(੺΂͜ຊ) • Kotlin Advent Calendar (2015/2016) • Kotlinೖ໳·Ͱͷॿ૸ಡຊ( goo.gl/5vUT7o )