Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
package com.thanaa.countriesmvvm.view

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.thanaa.countriesmvvm.R
import com.thanaa.countriesmvvm.model.Country
import kotlinx.android.synthetic.main.activity_main.view.*
import kotlinx.android.synthetic.main.item_country.view.*

class CountryListAdapter(var countries:ArrayList<Country>):RecyclerView.Adapter<CountryListAdapter.CountryViewHolder>() {


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CountryViewHolder {
TODO("Not yet implemented")
fun updateCounties (newCountry:List<Country>){
countries.clear()
countries.addAll(newCountry)
notifyDataSetChanged()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
CountryViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_country,parent,false))
override fun getItemCount()= countries.size

override fun onBindViewHolder(holder: CountryViewHolder, position: Int) {
holder.bind(countries[position])
}
class CountryViewHolder(view: View):RecyclerView.ViewHolder(view){

fun bind (country:Country){

class CountryViewHolder(view: View):RecyclerView.ViewHolder(view){
private val countryName = view.name
fun bind (country:Country){
countryName.text = country.countryName
}

}
Expand Down
49 changes: 48 additions & 1 deletion app/src/main/java/com/thanaa/countriesmvvm/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,58 @@ package com.thanaa.countriesmvvm.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ListAdapter
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.thanaa.countriesmvvm.R
import com.thanaa.countriesmvvm.viewmodel.ListViewModel
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
lateinit var viewModel:ListViewModel
var countriesAdapter = CountryListAdapter(arrayListOf())

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProviders.of(this).get(ListViewModel::class.java)
viewModel.refresh()

countriesList.apply {
layoutManager = GridLayoutManager(context,2)
adapter = countriesAdapter
}

observeViewModel()
}

private fun observeViewModel() {

viewModel.countries.observe(this,Observer { countries ->
countries?.let {
countriesAdapter.updateCounties(it)
}
})

viewModel.countryLoadError.observe(this, Observer { isError -> isError?.let {
list_error.visibility = if(it) View.VISIBLE else View.GONE
} })

viewModel.loading.observe(this, Observer { isLoading -> isLoading?.let{
progressBar.visibility = if(it) View.VISIBLE else View.GONE

if(it){
list_error.visibility =View.GONE
countriesList.visibility = View.GONE
}
} })


}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class ListViewModel:ViewModel() {
val countryLoadError = MutableLiveData<Boolean>()
val loading = MutableLiveData<Boolean>()

fun fetch(){
fun refresh(){
fetchCountries()
}

private fun fetchCountries(){
var mockData:List<Country> = listOf(Country("Saudi"),Country("US"))
var mockData:List<Country> = listOf(Country("Saudi"),Country("US"),Country("Germany"))
countryLoadError.value = false
loading.value =false
countries.value = mockData
Expand Down