Skip to content

Commit

Permalink
-ViewModelProvider deprecated code migrated to latest for fragment / …
Browse files Browse the repository at this point in the history
…Activity

- Boilerplate code removed.
  • Loading branch information
WaheedNazir committed Sep 29, 2020
1 parent 7c2aac1 commit 3d0895d
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 66 deletions.
59 changes: 34 additions & 25 deletions app/app.iml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,43 @@ package com.kotlin.mvvm.ui.countries
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.kotlin.mvvm.R
import com.kotlin.mvvm.di.base.Injectable
import com.kotlin.mvvm.repository.model.countries.Country
import com.kotlin.mvvm.ui.news.NewsActivity

import dagger.android.support.AndroidSupportInjection
import dagger.android.support.DaggerFragment
import kotlinx.android.synthetic.main.fragment_country_list.view.*
import javax.inject.Inject

/**
* Created by Waheed on 08,November,2019
* Updated to dagger 2.27, 29, September 2020
*/

/**
* A fragment representing a list of Items.
*/
class CountriesFragment : Fragment(), Injectable {
class CountriesFragment : DaggerFragment() {

@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory


private val countriesViewModel: CountriesViewModel by viewModels {
viewModelFactory
}

private var columnCount = 1
private lateinit var countriesAdapter: CountriesAdapter
private lateinit var countriesViewModel: CountriesViewModel
private var listOfCountries = ArrayList<Country>()
private lateinit var thisView: View

Expand Down Expand Up @@ -94,10 +98,6 @@ class CountriesFragment : Fragment(), Injectable {
*
*/
private fun load() {
countriesViewModel =
ViewModelProviders.of(this, viewModelFactory).get(CountriesViewModel::class.java)


thisView.recyclerview_countries.layoutManager =
if (columnCount <= 1) LinearLayoutManager(context) else GridLayoutManager(
context,
Expand Down
10 changes: 4 additions & 6 deletions app/src/main/java/com/kotlin/mvvm/ui/news/NewsActivity.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.kotlin.mvvm.ui.news

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.kotlin.mvvm.R
import com.kotlin.mvvm.ui.BaseActivity
import com.kotlin.mvvm.utils.ToastUtil
import com.kotlin.mvvm.utils.extensions.getViewModel
import com.kotlin.mvvm.utils.extensions.load
import com.kotlin.mvvm.utils.extensions.toast
import kotlinx.android.synthetic.main.activity_news_articles.*
import kotlinx.android.synthetic.main.empty_layout_news_article.*
import kotlinx.android.synthetic.main.progress_layout_news_article.*
Expand All @@ -24,11 +23,10 @@ class NewsActivity : BaseActivity() {
val KEY_COUNTRY_SHORT_KEY: String = "COUNTRY_SHORT_KEY"
}


private lateinit var adapter: NewsAdapter

private val newsArticleViewModel by lazy {
getViewModel<NewsViewModel>()
private val newsArticleViewModel: NewsViewModel by viewModels {
viewModelFactory
}

/**
Expand All @@ -49,7 +47,7 @@ class NewsActivity : BaseActivity() {
news_list.adapter = adapter
news_list.layoutManager = LinearLayoutManager(this)

getNewsOfCountry(intent.getStringExtra(KEY_COUNTRY_SHORT_KEY))
getNewsOfCountry(intent?.getStringExtra(KEY_COUNTRY_SHORT_KEY)!!)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.net.ConnectivityManager

object ConnectivityUtil {

@Suppress("DEPRECATION")
fun isConnected(context: Context): Boolean {
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/kotlin/mvvm/utils/IntentUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.core.app.ShareCompat
* Created by Waheed on 04,November,2019
*/


fun intentShareText(activity: Activity, text: String) {
val shareIntent = ShareCompat.IntentBuilder.from(activity)
.setText(text)
Expand All @@ -22,7 +23,8 @@ fun intentShareText(activity: Activity, text: String) {
addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
} else {
// Else, we will use the old CLEAR_WHEN_TASK_RESET flag
addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
}
}
activity.startActivity(shareIntent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fun Activity.startActivityNewTask(clazz: Class<*>) {
startActivity(intent)
}

@Suppress("DEPRECATION")
fun Activity.isConnectedInternet(): Boolean {
val connectivityManager =
this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fun ViewGroup.inflate(layoutRes: Int): View {
return LayoutInflater.from(context).inflate(layoutRes, this, false)
}

@Suppress("DEPRECATION")
fun Context.getDrawableCompat(@DrawableRes resId: Int, @ColorRes tintColorRes: Int = 0) = when {
tintColorRes != 0 -> AppCompatResources.getDrawable(this, resId)?.apply {
setColorFilter(getColorCompat(tintColorRes), PorterDuff.Mode.SRC_ATOP)
Expand Down

This file was deleted.

0 comments on commit 3d0895d

Please sign in to comment.