-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed default app module from java to kotlin
- Loading branch information
1 parent
0963def
commit 08d3d5d
Showing
34 changed files
with
986 additions
and
1,175 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 0 additions & 51 deletions
51
app/src/main/java/com/talentica/androidkotlin/MainActivity.java
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
app/src/main/java/com/talentica/androidkotlin/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2017, The Android Open Source Project | ||
* | ||
* 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. | ||
*/ | ||
package com.talentica.androidkotlin | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.talentica.androidkotlin.model.Product | ||
|
||
class MainActivity : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.main_activity) | ||
|
||
// Add product list fragment if this is first creation | ||
if (savedInstanceState == null) { | ||
val fragment = ProductListFragment() | ||
supportFragmentManager.beginTransaction() | ||
.add(R.id.fragment_container, fragment, ProductListFragment.Companion.TAG).commit() | ||
} | ||
} | ||
|
||
/** Shows the product detail fragment */ | ||
fun show(product: Product) { | ||
val productFragment: ProductFragment = ProductFragment.Companion.forProduct(product.id) | ||
supportFragmentManager | ||
.beginTransaction() | ||
.addToBackStack("product") | ||
.replace( | ||
R.id.fragment_container, | ||
productFragment, null | ||
).commit() | ||
} | ||
} |
114 changes: 0 additions & 114 deletions
114
app/src/main/java/com/talentica/androidkotlin/ProductFragment.java
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
app/src/main/java/com/talentica/androidkotlin/ProductFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2017, The Android Open Source Project | ||
* | ||
* 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. | ||
*/ | ||
package com.talentica.androidkotlin | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.databinding.DataBindingUtil | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.ViewModelProviders | ||
import com.talentica.androidkotlin.databinding.ProductFragmentBinding | ||
import com.talentica.androidkotlin.model.Comment | ||
import com.talentica.androidkotlin.ui.CommentAdapter | ||
import com.talentica.androidkotlin.ui.CommentClickCallback | ||
import com.talentica.androidkotlin.viewmodel.ProductViewModel | ||
|
||
class ProductFragment : Fragment() { | ||
private lateinit var mBinding: ProductFragmentBinding | ||
private var mCommentAdapter: CommentAdapter? = null | ||
override fun onCreateView( | ||
inflater: LayoutInflater, container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
// Inflate this data binding layout | ||
mBinding = DataBindingUtil.inflate(inflater, R.layout.product_fragment, container, false) | ||
|
||
// Create and set the adapter for the RecyclerView. | ||
mCommentAdapter = CommentAdapter(mCommentClickCallback) | ||
mBinding.commentList.adapter = mCommentAdapter | ||
return mBinding.root | ||
} | ||
|
||
private val mCommentClickCallback = object : CommentClickCallback { | ||
override fun onClick(comment: Comment?) { | ||
// no-op | ||
} | ||
} | ||
|
||
override fun onActivityCreated(savedInstanceState: Bundle?) { | ||
super.onActivityCreated(savedInstanceState) | ||
val factory = ProductViewModel.Factory( | ||
requireActivity().application, requireArguments().getInt(KEY_PRODUCT_ID) | ||
) | ||
val model = ViewModelProviders.of(this, factory) | ||
.get(ProductViewModel::class.java) | ||
mBinding.productViewModel = model | ||
subscribeToModel(model) | ||
} | ||
|
||
private fun subscribeToModel(model: ProductViewModel) { | ||
|
||
// Observe product data | ||
model.observableProduct?.observe( | ||
viewLifecycleOwner, | ||
{ productEntity -> model.setProduct(productEntity) }) | ||
|
||
// Observe comments | ||
model.comments?.observe(viewLifecycleOwner, { commentEntities -> | ||
if (commentEntities != null) { | ||
mBinding.isLoading = false | ||
mCommentAdapter!!.setCommentList(commentEntities) | ||
} else { | ||
mBinding.isLoading = true | ||
} | ||
}) | ||
} | ||
|
||
companion object { | ||
private const val KEY_PRODUCT_ID = "product_id" | ||
|
||
/** Creates product fragment for specific product ID */ | ||
fun forProduct(productId: Int): ProductFragment { | ||
val fragment = ProductFragment() | ||
val args = Bundle() | ||
args.putInt(KEY_PRODUCT_ID, productId) | ||
fragment.arguments = args | ||
return fragment | ||
} | ||
} | ||
} |
Oops, something went wrong.