fragment

mail@pastecode.io avatar
unknown
java
2 years ago
2.1 kB
2
Indexable
Never
package com.ossovita.hesapkimdenative.view.fragments

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
import com.ossovita.hesapkimdenative.R
import com.ossovita.hesapkimdenative.adapter.RestaurantsWithDetailsAdapter
import com.ossovita.hesapkimdenative.db.CustomSharedPreferences
import com.ossovita.hesapkimdenative.view.viewmodels.RestaurantFragmentViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.android.synthetic.main.fragment_restaurant.*
import javax.inject.Inject

@AndroidEntryPoint
class RestaurantFragment : Fragment(R.layout.fragment_restaurant) {

    val TAG = "RestaurantFragment"
    val args: RestaurantFragmentArgs by navArgs()
    lateinit var restaurantsWithDetailsAdapter: RestaurantsWithDetailsAdapter

    @Inject
    lateinit var preferences: CustomSharedPreferences

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setUpRecyclerView()

        var viewModel = ViewModelProvider(this).get(RestaurantFragmentViewModel::class.java)
        Log.d(TAG,
            "onViewCreated: restaurantFragment sharedpreferences:" + preferences.getUserCreds()
                .toString()
        )
        viewModel.getRestaurantsWithDetailsList("Bearer " + args.authResponse.token)

        viewModel.restaurantWithDetailsList.observe(viewLifecycleOwner, Observer { response ->
            restaurantsWithDetailsAdapter.differ.submitList(response?.toList())
        })

        Log.d(TAG, "onViewCreated: " + args.authResponse.toString())
    }

    private fun setUpRecyclerView() {
        restaurantsWithDetailsAdapter = RestaurantsWithDetailsAdapter()
        rvRestaurantWithDetails.apply {
            adapter = restaurantsWithDetailsAdapter
            layoutManager = LinearLayoutManager(activity)

        }
    }
}