Skip to content

Commit

Permalink
Merge pull request #5 from icanerdogan/app-development
Browse files Browse the repository at this point in the history
1.1.0
  • Loading branch information
icanerdogan authored Jan 21, 2024
2 parents ba502c7 + 2f3c69f commit ed770d7
Show file tree
Hide file tree
Showing 22 changed files with 511 additions and 160 deletions.
4 changes: 2 additions & 2 deletions .idea/deploymentTargetSelector.xml

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

8 changes: 6 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ plugins {
}

def versionMajor = 1
def versionMinor = 0
def versionPatch = 2
def versionMinor = 1
def versionPatch = 0

android {
namespace 'com.ibrahimcanerdogan.nves'
Expand Down Expand Up @@ -64,6 +64,7 @@ dependencies {
implementation libs.androidx.activity
implementation libs.androidx.constraintlayout
implementation libs.androidx.swiperefreshlayout
implementation libs.androidx.navigation.fragment
testImplementation libs.junit
androidTestImplementation libs.androidx.junit
androidTestImplementation libs.androidx.espresso.core
Expand All @@ -89,6 +90,9 @@ dependencies {
// Lottie
implementation libs.lottie

// Splash Api
implementation libs.androidx.core.splashscreen

// Expandable Layout
implementation libs.expandableLayout
}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/icon_launcher_round"
android:supportsRtl="true"
android:theme="@style/Base.Theme.Nves"
android:theme="@style/Theme.App.Starting"
tools:targetApi="31">
<activity
android:name=".HomeActivity"
android:theme="@style/Theme.App.Starting"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
133 changes: 25 additions & 108 deletions app/src/main/java/com/ibrahimcanerdogan/nves/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.MenuItem
import android.view.View
import android.view.WindowInsets
import android.view.WindowInsetsController
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.card.MaterialCardView
import com.google.android.material.navigation.NavigationView
import com.ibrahimcanerdogan.nves.data.model.News
import com.ibrahimcanerdogan.nves.databinding.ActivityHomeBinding
import com.ibrahimcanerdogan.nves.util.NewsCategory
Expand All @@ -25,131 +32,41 @@ import javax.inject.Inject


@AndroidEntryPoint
class HomeActivity : AppCompatActivity() {
class HomeActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

private lateinit var binding: ActivityHomeBinding

private val viewModel by lazy {
ViewModelProvider(this, factory).get(NewsViewModel::class.java)
}
@Inject
lateinit var factory: NewsViewModelFactory
@Inject
lateinit var newsAdapter: NewsAdapter

private var page = 1
private var totalResults = 0
private var newsCategory: String = NewsCategory.BUSINESS.param

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
installSplashScreen()
binding = ActivityHomeBinding.inflate(layoutInflater)
setContentView(binding.root)

// Categories configuration.
binding.setCategoryCard()
binding.layoutCategory.cardBusiness.setCardBackgroundColor(getColor(R.color.MainLigth))
val toggle = ActionBarDrawerToggle(this, binding.drawerLayout, R.string.open, R.string.close)

viewModel.getNewsHeadLines(context = this, country = "us", category = newsCategory, page = page)
viewModel.headlinesData.observe(this, ::setData)

binding.viewPager.apply {
adapter = newsAdapter
registerOnPageChangeCallback(scrollListener)
binding.apply {
navigationDrawer.setNavigationItemSelectedListener(this@HomeActivity)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
buttonDrawerMenu.setOnClickListener { drawerLayout.open() }
}
}

private fun setData(resource: Resource<News>?) {
when(resource) {
is Resource.Success -> {
resource.data?.let {
newsAdapter.differ.submitList(it.articles?.toList())
totalResults = it.totalResults
showLoadingAnimation()
Log.i(TAG, it.status)
}
}
is Resource.Error -> {
resource.message?.let { error ->
showLoadingAnimation()
viewModel.getNewsHeadLines(context = this, country = "us", category = newsCategory, page = page)
Log.e(TAG, error)
}
}
is Resource.Loading -> {
showLoadingAnimation(true)
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when(item.itemId) {
R.id.itemLocation -> {
Toast.makeText(this, "Coming Soon...", Toast.LENGTH_SHORT).show()
}
else -> Log.d(TAG, "Resource not found!")
}
}

private val scrollListener = object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
if (position == newsAdapter.itemCount - 1) {
page += 1
if (page == (totalResults / 20) + 2 ) page = 1 // If max page ((totalResults / 20) + 1) + 1 make return first
updateNewsHeadlines()
R.id.itemReview -> {
Toast.makeText(this, "Coming Soon...", Toast.LENGTH_SHORT).show()
}
}
}

private fun updateNewsHeadlines() {
showLoadingAnimation(true)
Handler(Looper.getMainLooper()).postDelayed({
viewModel.getNewsHeadLines(
context = this@HomeActivity,
country = "us",
category = newsCategory,
page = page
)
}, 1000)

binding.viewPager.currentItem = 0
}

private fun ActivityHomeBinding.setCategoryCard() {
layoutCategory.apply {
setCategoryInfo(cardBusiness, NewsCategory.BUSINESS.param)
setCategoryInfo(cardEntertainment, NewsCategory.ENTERTAINMENT.param)
setCategoryInfo(cardGeneral, NewsCategory.GENERAL.param)
setCategoryInfo(cardHealth, NewsCategory.HEALTH.param)
setCategoryInfo(cardScience, NewsCategory.SCIENCE.param)
setCategoryInfo(cardSports, NewsCategory.SPORTS.param)
setCategoryInfo(cardTechnology, NewsCategory.TECHNOLOGY.param)
}
}

private fun setCategoryInfo(card: MaterialCardView, categoryParam: String) {
card.setOnClickListener {
if (newsCategory != categoryParam) {
setUnSelectCategoryCard()
page = 1
newsCategory = categoryParam
updateNewsHeadlines()
card.setCardBackgroundColor(getColor(R.color.MainLigth))
R.id.itemShare -> {
Toast.makeText(this, "Coming Soon...", Toast.LENGTH_SHORT).show()
}
}
}

private fun setUnSelectCategoryCard() {
with(binding.layoutCategory) {
cardBusiness.setCardBackgroundColor(getColor(R.color.white))
cardEntertainment.setCardBackgroundColor(getColor(R.color.white))
cardGeneral.setCardBackgroundColor(getColor(R.color.white))
cardHealth.setCardBackgroundColor(getColor(R.color.white))
cardScience.setCardBackgroundColor(getColor(R.color.white))
cardSports.setCardBackgroundColor(getColor(R.color.white))
cardTechnology.setCardBackgroundColor(getColor(R.color.white))
}
}
private fun showLoadingAnimation(isShown: Boolean = false) {
with(binding){
lottieAnimation.visibility = if (isShown) View.VISIBLE else View.INVISIBLE
viewPager.visibility= if (isShown) View.INVISIBLE else View.VISIBLE
}

binding.drawerLayout.close()
return true
}

override fun onStart() {
Expand Down
Loading

0 comments on commit ed770d7

Please sign in to comment.