Skip to content

Commit

Permalink
OnClick on Top Cards
Browse files Browse the repository at this point in the history
  • Loading branch information
Raj-m01 committed Oct 10, 2022
1 parent 5873871 commit 4880ab3
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 182 deletions.
2 changes: 2 additions & 0 deletions .idea/misc.xml

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

10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ Kotlin-based Android news app helps users stay up-to-date on the latest headline
Note: To run the app you have to add NewsAPI.org key. This API has limited access to daily request for News.
* Generate API key from <a href="https://newsapi.org/">News api</a>
* Paste the key in 'gradle.properties' *(Create the file if not exists)*
- For Linux/Mac: `/home/.gradle/gradle.properties`
- For Windows: `C:\Users\<UserName>\.gradle\gradle.properties`

API_KEY="<YOUR_API_KEY>"
* Rebuild app
Expand All @@ -64,14 +62,6 @@ Note: To run the app you have to add NewsAPI.org key. This API has limited acces
- **If a maintainer reports your pull request as spam, it will not be counted towards your participation in Hacktoberfest.**


### Todos Features:

* Add language support for various languages
* Diversify news feed by adding different country/language options (through API endpoints)
* Improve home page card-slider/Carousel
* And many more.


### 📝 License

Copyright (c) 2022 Raj Manjrekar
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ android {

// Please ensure you have a valid API KEY for https://newsapi.org/
// to use this app
// A valid key will need to be entered
// A valid key will need to be entered in gradle.properties file
buildConfigField("String", "API_KEY", "\"5a3e054de1834138a2fbc4a75ee69053\"")

}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package com.example.newsapp.fragmentClasses

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -16,8 +15,6 @@ import com.example.newsapp.NewsModel
import com.example.newsapp.R
import com.example.newsapp.ReadNewsActivity
import com.example.newsapp.adapters.CustomAdapter
import com.example.newsapp.adapters.CustomAdapterForTopHeadlines
import com.example.newsapp.utils.Constants.DEFAULT_SWIPER_DELAY
import com.example.newsapp.utils.Constants.NEWS_CONTENT
import com.example.newsapp.utils.Constants.NEWS_DESCRIPTION
import com.example.newsapp.utils.Constants.NEWS_IMAGE_URL
Expand All @@ -27,20 +24,15 @@ import com.example.newsapp.utils.Constants.NEWS_TITLE
import com.example.newsapp.utils.Constants.NEWS_URL
import com.example.newsapp.utils.Constants.TOP_HEADLINES_COUNT
import com.example.newsapp.utils.Constants.INITIAL_POSITION
import com.jama.carouselview.CarouselScrollListener
import com.jama.carouselview.CarouselView
import com.jama.carouselview.enums.IndicatorAnimationType
import com.jama.carouselview.enums.OffsetType
import com.squareup.picasso.Picasso

class GeneralFragment : Fragment() {

private lateinit var mainHandler: Handler
private lateinit var swiper: Runnable
private lateinit var recyclerView: RecyclerView
// private lateinit var recyclerViewTop: RecyclerView
private lateinit var carouselView: CarouselView
// private lateinit var topAdapter: CustomAdapterForTopHeadlines
private lateinit var adapter: CustomAdapter
private lateinit var newsDataForTopHeadlines: List<NewsModel>
private lateinit var newsDataForDown: List<NewsModel>
Expand All @@ -67,54 +59,42 @@ class GeneralFragment : Fragment() {

carouselView.apply {
size = newsDataForTopHeadlines.size
// resource = R.layout.carousel_item
autoPlay = true
indicatorAnimationType = IndicatorAnimationType.THIN_WORM
carouselOffset = OffsetType.CENTER
setCarouselViewListener { view, position ->
// Example here is setting up a full image carousel
val imageView = view.findViewById<ImageView>(R.id.img)
// imageView.setImageDrawable()
Picasso.get()
.load(newsDataForTopHeadlines[position].image)
.fit()
.centerCrop()
.error(R.drawable.samplenews)
.into(imageView)
}
// After you finish setting up, show the CarouselView
show()
}

// Top headlines items Slider
mainHandler = Handler(Looper.getMainLooper())
swiper = object : Runnable {
override fun run() {
// recyclerViewTop.smoothScrollToPosition(position)
position++
mainHandler.postDelayed(this, DEFAULT_SWIPER_DELAY)
}
}

val newsTitle = view.findViewById<TextView>(R.id.headline)
newsTitle.text = newsDataForTopHeadlines[position].headLine

carouselView.carouselScrollListener = object : CarouselScrollListener {
override fun onScrollStateChanged(
recyclerView: RecyclerView,
newState: Int,
position: Int
) {
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
mainHandler.removeCallbacks(swiper)
}
}
view.setOnClickListener {

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
val intent = Intent(context, ReadNewsActivity::class.java).apply {
putExtra(NEWS_URL, newsDataForTopHeadlines[position].url)
putExtra(NEWS_TITLE, newsDataForTopHeadlines[position].headLine)
putExtra(NEWS_IMAGE_URL, newsDataForTopHeadlines[position].image)
putExtra(NEWS_DESCRIPTION, newsDataForTopHeadlines[position].description)
putExtra(NEWS_SOURCE, newsDataForTopHeadlines[position].source)
putExtra(NEWS_PUBLICATION_TIME, newsDataForTopHeadlines[position].time)
putExtra(NEWS_CONTENT, newsDataForTopHeadlines[position].content)
}

startActivity(intent)

}
}
// After you finish setting up, show the CarouselView
show()
}



// listitem onClick
adapter.setOnItemClickListener(object : CustomAdapter.OnItemClickListener {
override fun onItemClick(position: Int) {
Expand All @@ -132,26 +112,6 @@ class GeneralFragment : Fragment() {
}
})

//TOPHEADLINES LIST ITEM ONCLICK
// topAdapter.setOnItemClickListener(object :
// CustomAdapterForTopHeadlines.OnItemClickListener {
// override fun onItemClick(position: Int) {
// val pos = position % TOP_HEADLINES_COUNT
//
// val intent = Intent(context, ReadNewsActivity::class.java).apply {
// putExtra(NEWS_URL, newsDataForTopHeadlines[pos].url)
// putExtra(NEWS_TITLE, newsDataForTopHeadlines[pos].headLine)
// putExtra(NEWS_IMAGE_URL, newsDataForTopHeadlines[pos].image)
// putExtra(NEWS_DESCRIPTION, newsDataForTopHeadlines[pos].description)
// putExtra(NEWS_SOURCE, newsDataForTopHeadlines[pos].source)
// putExtra(NEWS_PUBLICATION_TIME, newsDataForTopHeadlines[pos].time)
// putExtra(NEWS_CONTENT, newsDataForTopHeadlines[pos].content)
// }
//
// startActivity(intent)
// }
// })

// Ignore
adapter.setOnItemLongClickListener(object : CustomAdapter.OnItemLongClickListener {
override fun onItemLongClick(position: Int) = Unit
Expand All @@ -160,20 +120,4 @@ class GeneralFragment : Fragment() {
return view
}

private var currentSliderPosition = INITIAL_POSITION

// pause slider when fragment paused
override fun onPause() {
mainHandler.removeCallbacks(swiper)
currentSliderPosition = position
super.onPause()
}

// resume slider when fragment resumed
override fun onResume() {
mainHandler.post(swiper)
position = currentSliderPosition
super.onResume()
}

}
20 changes: 3 additions & 17 deletions app/src/main/res/layout/list_item_for_top_headlines.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,17 @@
android:scaleType="centerCrop"
android:src="@drawable/samplenews" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:layout_margin="10dp"
android:visibility="gone"
android:src="@drawable/ic_baseline_arrow_back_ios_24" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:layout_margin="10dp"
android:visibility="gone"
android:src="@drawable/ic_baseline_arrow_forward_ios_24" />

</com.google.android.material.card.MaterialCardView>

<TextView
android:id="@+id/headline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="4"
android:minHeight="70dp"
android:maxLines="3"
android:padding="5dp"
android:text="Hello , this is sample news....."
android:text="Hello , ypesetter in the book. It usually begins with"
android:textStyle="bold" />

</LinearLayout>
Expand Down

0 comments on commit 4880ab3

Please sign in to comment.