-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from MuhammedEdrees/search_frag
implemented search behaviour
- Loading branch information
Showing
14 changed files
with
248 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
app/src/main/java/com/edrees/newsapp/ui/search/SearchAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.edrees.newsapp.ui.search | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.View.OnClickListener | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.bumptech.glide.Glide | ||
import com.bumptech.glide.request.RequestOptions | ||
import com.edrees.newsapp.databinding.SearchListItemBinding | ||
import com.edrees.newsapp.model.Article | ||
import com.edrees.newsapp.ui.home.DetailsCallback | ||
import jp.wasabeef.glide.transformations.BlurTransformation | ||
|
||
class SearchAdapter(private val callback: DetailsCallback): RecyclerView.Adapter<SearchAdapter.ViewHolder>() { | ||
private val data = mutableListOf<Article>() | ||
inner class ViewHolder(val binding: SearchListItemBinding): RecyclerView.ViewHolder(binding.root) { | ||
init{ | ||
binding.root.setOnClickListener { | ||
this@SearchAdapter.callback.navigateToDetails(data[layoutPosition]) | ||
} | ||
} | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
val binding = SearchListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
return ViewHolder(binding) | ||
} | ||
|
||
override fun getItemCount() = data.size | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
with(holder){ | ||
with(data[position]){ | ||
binding.titleTextView.text = title | ||
binding.descriptionTextView.text = description | ||
binding.sourceTextView.text = source?.name?:"Unknown Source" | ||
Glide.with(binding.root) | ||
.load(urlToImage) | ||
.apply(RequestOptions.bitmapTransform(BlurTransformation(5, 1))) | ||
.into(binding.itemThumbnail) | ||
} | ||
} | ||
} | ||
fun setData(newData: List<Article>){ | ||
data.clear() | ||
data.addAll(newData) | ||
notifyDataSetChanged() | ||
} | ||
} |
74 changes: 62 additions & 12 deletions
74
app/src/main/java/com/edrees/newsapp/ui/search/SearchFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,92 @@ | ||
package com.edrees.newsapp.ui.search | ||
|
||
import android.os.Bundle | ||
import android.text.Editable | ||
import android.text.TextWatcher | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.get | ||
import androidx.navigation.fragment.findNavController | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.edrees.newsapp.databinding.FragmentSearchBinding | ||
import com.edrees.newsapp.local.LocalSourceImpl | ||
import com.edrees.newsapp.model.Article | ||
import com.edrees.newsapp.network.APIClient | ||
import com.edrees.newsapp.repo.ArticleRepositoryImpl | ||
import com.edrees.newsapp.ui.ViewModelFactory | ||
import com.edrees.newsapp.ui.home.DetailsCallback | ||
import com.google.android.material.textfield.TextInputEditText | ||
|
||
class SearchFragment : Fragment() { | ||
class SearchFragment : Fragment(), DetailsCallback { | ||
|
||
private var _binding: FragmentSearchBinding? = null | ||
|
||
// This property is only valid between onCreateView and | ||
// onDestroyView. | ||
private val binding get() = _binding!! | ||
private lateinit var viewModel: SearchViewModel | ||
private lateinit var recyclerView: RecyclerView | ||
private lateinit var layoutManager: LinearLayoutManager | ||
private lateinit var searchEditText: TextInputEditText | ||
private lateinit var adapter: SearchAdapter | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
val slideshowViewModel = | ||
ViewModelProvider(this).get(SearchViewModel::class.java) | ||
|
||
_binding = FragmentSearchBinding.inflate(inflater, container, false) | ||
val root: View = binding.root | ||
return binding.root | ||
} | ||
|
||
val textView: TextView = binding.textSlideshow | ||
slideshowViewModel.text.observe(viewLifecycleOwner) { | ||
textView.text = it | ||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
prepareViewModel() | ||
searchEditText = binding.searchTextInputLayout.editText as TextInputEditText | ||
viewModel.listOfArticles.observe(viewLifecycleOwner){articles -> | ||
if(searchEditText.text.isNullOrBlank()){ | ||
adapter.setData(listOf()) | ||
} else { | ||
adapter.setData(articles) | ||
} | ||
} | ||
return root | ||
recyclerView = binding.searchRecyclerView | ||
layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false) | ||
adapter = SearchAdapter(this) | ||
recyclerView.layoutManager = layoutManager | ||
recyclerView.adapter = adapter | ||
searchEditText.addTextChangedListener(object: TextWatcher{ | ||
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { | ||
} | ||
|
||
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { | ||
if(p0.toString().isBlank()){ | ||
adapter.setData(listOf<Article>()) | ||
} else { | ||
viewModel.search(p0.toString(), "en", 1) | ||
} | ||
} | ||
|
||
override fun afterTextChanged(p0: Editable?) { | ||
} | ||
}) | ||
} | ||
|
||
private fun prepareViewModel() { | ||
val factory = ViewModelFactory(ArticleRepositoryImpl(APIClient, LocalSourceImpl(requireContext()))) | ||
viewModel = ViewModelProvider(this, factory).get(SearchViewModel::class.java) | ||
} | ||
|
||
override fun onDestroyView() { | ||
adapter.setData(listOf()) | ||
super.onDestroyView() | ||
_binding = null | ||
} | ||
|
||
override fun navigateToDetails(article: Article) { | ||
val action = SearchFragmentDirections.actionNavSearchToDetailsFragment(article) | ||
findNavController().navigate(action) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:shape="rectangle"> | ||
<gradient | ||
android:angle="90" | ||
android:endColor="#88000000" | ||
android:startColor="#88000000" | ||
android:centerColor="#88000000" /> | ||
|
||
<corners android:radius="0dp" /> | ||
</shape> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<com.google.android.material.card.MaterialCardView | ||
xmlns:tools="http://schemas.android.com/tools" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_margin="8dp" | ||
app:cardCornerRadius="12dp" | ||
android:elevation="12dp"> | ||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
<ImageView | ||
android:layout_width="match_parent" | ||
android:layout_height="0dp" | ||
app:layout_constraintTop_toTopOf="@id/text_views_layout" | ||
app:layout_constraintBottom_toBottomOf="@id/text_views_layout" | ||
android:scaleType="centerCrop" | ||
android:id="@+id/item_thumbnail" | ||
android:foreground="@drawable/image_shade" | ||
android:src="@drawable/ic_splash"/> | ||
<LinearLayout | ||
android:id="@+id/text_views_layout" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintTop_toTopOf="parent" | ||
android:orientation="vertical" | ||
android:padding="16dp"> | ||
<TextView | ||
android:id="@+id/titleTextView" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
tools:text="Title Placeholder" | ||
android:textAppearance="@style/HeadlineText" /> | ||
<View | ||
android:layout_width="match_parent" | ||
android:layout_height="2dp" | ||
android:background="#aaeeeeee"/> | ||
<TextView | ||
android:id="@+id/descriptionTextView" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="4dp" | ||
android:textColor="@color/white" | ||
android:textSize="14sp" /> | ||
<TextView | ||
android:id="@+id/sourceTextView" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="8dp" | ||
android:gravity="end" | ||
android:layout_gravity="bottom" | ||
tools:text="Source" | ||
android:textAppearance="@style/SubText"/> | ||
</LinearLayout> | ||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
</com.google.android.material.card.MaterialCardView> |
Oops, something went wrong.