Skip to content

Commit

Permalink
changed default app module from java to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
dinkarkumardk committed Jul 9, 2021
1 parent 0963def commit 08d3d5d
Show file tree
Hide file tree
Showing 34 changed files with 986 additions and 1,175 deletions.
2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 30
defaultConfig {
Expand Down Expand Up @@ -38,15 +37,17 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
kapt "com.android.databinding:compiler:3.1.4"
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'

implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.room:room-runtime:2.3.0'
annotationProcessor 'androidx.lifecycle:lifecycle-common-java8:2.3.1'
annotationProcessor 'androidx.room:room-compiler:2.3.0'

implementation "androidx.room:room-ktx:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"
androidTestImplementation "androidx.room:room-testing:2.3.0"
testImplementation 'junit:junit:4.13.2'

// Testing-only dependencies
Expand All @@ -65,6 +66,8 @@ dependencies {
androidTestImplementation 'androidx.legacy:legacy-support-v4:1.0.0'
androidTestImplementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_version"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_version"
}
repositories {
mavenCentral()
Expand Down
51 changes: 0 additions & 51 deletions app/src/main/java/com/talentica/androidkotlin/MainActivity.java

This file was deleted.

46 changes: 46 additions & 0 deletions app/src/main/java/com/talentica/androidkotlin/MainActivity.kt
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 app/src/main/java/com/talentica/androidkotlin/ProductFragment.java

This file was deleted.

94 changes: 94 additions & 0 deletions app/src/main/java/com/talentica/androidkotlin/ProductFragment.kt
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
}
}
}
Loading

0 comments on commit 08d3d5d

Please sign in to comment.