From d342f8e91c7daa0f706a686d0939e8e5146848c6 Mon Sep 17 00:00:00 2001 From: Jelena Petrovic Date: Wed, 9 Nov 2022 23:55:53 +0100 Subject: dodat odabir nacina prikaza objava (u listi ili gridu), dodat refresh povlacenjem i spiner dok se ucitavaju objave #29 --- Client/BrzoDoLokacije/app/build.gradle | 1 + .../brzodolokacije/Fragments/FragmentShowPosts.kt | 84 ++++++++++++++-------- .../src/main/res/layout/fragment_show_posts.xml | 23 +++--- 3 files changed, 70 insertions(+), 38 deletions(-) diff --git a/Client/BrzoDoLokacije/app/build.gradle b/Client/BrzoDoLokacije/app/build.gradle index 72f1f0d..ccc90fc 100644 --- a/Client/BrzoDoLokacije/app/build.gradle +++ b/Client/BrzoDoLokacije/app/build.gradle @@ -46,6 +46,7 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'com.google.android.material:material:1.7.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Fragments/FragmentShowPosts.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Fragments/FragmentShowPosts.kt index 73744d4..83f6429 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Fragments/FragmentShowPosts.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Fragments/FragmentShowPosts.kt @@ -8,9 +8,12 @@ import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ImageButton import android.widget.Toast +import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import com.example.brzodolokacije.Activities.NavigationActivity import com.example.brzodolokacije.Adapters.SampleAdapter import com.example.brzodolokacije.Adapters.ShowPostsAdapter @@ -19,29 +22,53 @@ import com.example.brzodolokacije.R import com.example.brzodolokacije.Services.RetrofitHelper import com.example.brzodolokacije.Services.SharedPreferencesHelper import com.example.brzodolokacije.databinding.FragmentHomeBinding +import com.example.brzodolokacije.databinding.FragmentShowPostsBinding +import kotlinx.android.synthetic.main.fragment_show_posts.* +import kotlinx.android.synthetic.main.fragment_show_posts.view.* import okhttp3.ResponseBody import retrofit2.Call import retrofit2.Response -class FragmentShowPosts : Fragment() { +class FragmentShowPosts : Fragment(), SwipeRefreshLayout.OnRefreshListener { - private lateinit var binding: FragmentShowPosts + private lateinit var binding: FragmentShowPostsBinding private var posts : MutableList = mutableListOf() - private var layoutManagerVar: RecyclerView.LayoutManager? = null + private var linearManagerVar: RecyclerView.LayoutManager? = null private var adapterVar: RecyclerView.Adapter? = null private var recyclerView: RecyclerView?=null + private var gridManagerVar: RecyclerView.LayoutManager?=null + private var swipeRefreshLayout:SwipeRefreshLayout?=null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - //load data for the list - loadData() - Log.d("main","greska") + binding=FragmentShowPostsBinding.inflate(layoutInflater) //instantiate adapter and linearLayout + adapterVar=ShowPostsAdapter(requireActivity(),posts) + linearManagerVar= LinearLayoutManager(activity) + gridManagerVar=GridLayoutManager(activity,2) + } + + fun setUpListeners(rootView: View?){ + rootView?.findViewById(R.id.btnGridLayout)?.setOnClickListener() { + if(recyclerView?.layoutManager!=gridManagerVar){ + recyclerView?.layoutManager=gridManagerVar + } + Log.d("main","klik") + } + + rootView?.findViewById(R.id.btnLinearLayout)?.setOnClickListener() { + if(recyclerView?.layoutManager!=linearManagerVar){ + recyclerView?.layoutManager=linearManagerVar + } + Log.d("main","klik") + } + } + + fun requestToBack(){ val postApi= RetrofitHelper.getInstance() val token=SharedPreferencesHelper.getValue("jwt", requireActivity()) val request=postApi.getPosts("Bearer "+token) - request.enqueue(object : retrofit2.Callback?> { override fun onResponse(call: Call?>, response: Response?>) { if(response.isSuccessful){ @@ -50,6 +77,7 @@ class FragmentShowPosts : Fragment() { Toast.makeText( activity, "prosao zahtev", Toast.LENGTH_LONG ).show() + swipeRefreshLayout?.isRefreshing=false }else{ if(response.errorBody()!=null) Toast.makeText(activity, response.errorBody()!!.string(), Toast.LENGTH_LONG).show(); @@ -64,30 +92,8 @@ class FragmentShowPosts : Fragment() { ).show(); } }) - - adapterVar=ShowPostsAdapter(requireActivity(),posts) - layoutManagerVar= LinearLayoutManager(activity) } - private fun loadData() { - posts.add(PostPreview("123","asdasd", - Location("asd","Ajfelov toranj","Pariz", - "Francuska","idk",1.1,1.1, LocationType.GRAD),"opsiopsaid",13, - 4.3f,mutableListOf(),mutableListOf())) - posts.add(PostPreview("123","asdasd", - Location("asd","Ajfelov toranj","Pariz", - "Francuska","idk",1.1,1.1, LocationType.GRAD),"opsiopsaid",13, - 4.3f,mutableListOf(),mutableListOf())) - posts.add(PostPreview("123","asdasd", - Location("asd","Ajfelov toranj","Pariz", - "Francuska","idk",1.1,1.1, LocationType.GRAD),"opsiopsaid",13, - 4.3f,mutableListOf(),mutableListOf())) - posts.add(PostPreview("123","asdasd", - Location("asd","Ajfelov toranj","Pariz", - "Francuska","idk",1.1,1.1, LocationType.GRAD),"opsiopsaid",13, - 4.3f,mutableListOf(),mutableListOf())) - } - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? @@ -96,9 +102,27 @@ class FragmentShowPosts : Fragment() { recyclerView = rootView?.findViewById(R.id.rvMain) // set recyclerView attributes recyclerView?.setHasFixedSize(true) - recyclerView?.layoutManager = layoutManagerVar + //recyclerView?.layoutManager = linearManagerVar + recyclerView?.layoutManager = linearManagerVar recyclerView?.adapter = adapterVar + setUpListeners(rootView) + swipeRefreshLayout = rootView?.findViewById(R.id.swipeContainer) as SwipeRefreshLayout + swipeRefreshLayout?.setOnRefreshListener(this) + swipeRefreshLayout?.setColorSchemeResources( + R.color.purple_200, + R.color.teal_200, + R.color.dark_blue_transparent, + R.color.purple_700 + ) + swipeRefreshLayout?.post(kotlinx.coroutines.Runnable { + swipeRefreshLayout?.isRefreshing=true + requestToBack() + }) return rootView } + override fun onRefresh() { + requestToBack() + } + } \ No newline at end of file diff --git a/Client/BrzoDoLokacije/app/src/main/res/layout/fragment_show_posts.xml b/Client/BrzoDoLokacije/app/src/main/res/layout/fragment_show_posts.xml index 02512d4..a12801e 100644 --- a/Client/BrzoDoLokacije/app/src/main/res/layout/fragment_show_posts.xml +++ b/Client/BrzoDoLokacije/app/src/main/res/layout/fragment_show_posts.xml @@ -22,7 +22,7 @@ + + + \ No newline at end of file -- cgit v1.2.3