Skip to content

Commit

Permalink
[LH-112] Add ViewPage fragment for different category #16
Browse files Browse the repository at this point in the history
  • Loading branch information
flash159483 committed Aug 4, 2023
1 parent eaa365c commit bfe5bcb
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 8 deletions.
63 changes: 59 additions & 4 deletions feature/board/src/main/java/com/lighthouse/board/BoardFragment.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,73 @@
package com.lighthouse.board

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.lighthouse.board.databinding.FragmentBoardBinding
import com.lighthouse.board.view.TabContentFragment
import com.lighthouse.board.viewmodel.BoardViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class BoardFragment : Fragment() {
private val viewModel: BoardViewModel by viewModels()
private lateinit var binding: FragmentBoardBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_board, container, false)
): View {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_board, container, false)

initSpinner()
initTab()

return binding.root
}

private fun initSpinner() {
val arrayList = arrayListOf(
resources.getString(com.lighthouse.android.common_ui.R.string.sort_latest),
resources.getString(com.lighthouse.android.common_ui.R.string.sort_top_rated)
)
val arrayAdapter = ArrayAdapter(
requireContext(),
R.layout.spinner_item,
arrayList
).apply {
setDropDownViewResource(com.google.android.material.R.layout.support_simple_spinner_dropdown_item)
}

binding.spinnerSort.adapter = arrayAdapter
}

private fun initTab() {
binding.tabBoard
binding.vpBoard.adapter = TabViewPagerAdapter(this)
TabLayoutMediator(binding.tabBoard, binding.vpBoard) { tab, position ->
tab.text = resources.getStringArray(R.array.tab_name)[position]
}.attach()
}

inner class TabViewPagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
override fun getItemCount(): Int = resources.getStringArray(R.array.tab_name).size

override fun createFragment(position: Int): Fragment {
val fragment = TabContentFragment()

fragment.arguments = Bundle().apply {
putInt("tab_pos", position)
putString("order", binding.spinnerSort.selectedItem.toString())
}
return fragment
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.lighthouse.board.view

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.lighthouse.board.R
import com.lighthouse.board.databinding.FragmentTabContentBinding

class TabContentFragment : Fragment() {
private lateinit var binding: FragmentTabContentBinding

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_tab_content, container, false)

val content = arguments?.getInt("tab_pos") ?: "Default Content"

binding.srBoard.setOnRefreshListener {
Toast.makeText(context, "Hello!", Toast.LENGTH_SHORT).show()
binding.srBoard.isRefreshing = false
}

return binding.root
}
}
31 changes: 31 additions & 0 deletions feature/board/src/main/res/layout/fragment_tab_content.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.TabContentFragment">

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/sr_board"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_question"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:itemCount="3"
tools:listitem="@layout/question_tile" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class HomeFragment @Inject constructor() : Fragment() {

viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.CREATED) {
viewModel.fetchNextPage(1, null).collect {
viewModel.fetchNextPage(1, 200).collect {
render(it)
}
}
Expand Down Expand Up @@ -131,7 +131,7 @@ class HomeFragment @Inject constructor() : Fragment() {
private fun loadMoreProfiles() {
viewModel.loading.value = true
lifecycleScope.launch {
viewModel.fetchNextPage(1, null).collect {
viewModel.fetchNextPage(1, 200).collect {
render(it)
viewModel.loading.value = false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.lighthouse.profile

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.lighthouse.domain.repository.DrivenRepository
Expand All @@ -20,7 +19,6 @@ class ProfileViewModel @Inject constructor(
val drivenData: Flow<UiState> = drivenRepository
.getDriven()
.map {
Log.d("MYTAG", it.toString())
UiState.Success(it) as UiState
}
.onCompletion {
Expand Down

0 comments on commit bfe5bcb

Please sign in to comment.