Skip to content

Commit

Permalink
added onClick
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzcetinbas committed May 7, 2023
1 parent 9a0d6b7 commit 31b8da4
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import androidx.lifecycle.ViewModelProvider
import com.example.hw13_weatherapp.repo.WeatherPropertyRepository
import com.example.hw13_weatherapp.view.homeFragment.HomeViewModel

class HomeViewModelFactory(private val weatherPropertyRepository: WeatherPropertyRepository) :
ViewModelProvider.Factory {
class HomeViewModelFactory(private val weatherPropertyRepository: WeatherPropertyRepository) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(viewModelClass: Class<T>): T {
if (viewModelClass.isAssignableFrom(HomeViewModel::class.java)) {
return HomeViewModel(weatherPropertyRepository) as T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.example.hw13_weatherapp.databinding.FragmentHomeBinding
import com.example.hw13_weatherapp.model.data.WeatherResponse
import com.example.hw13_weatherapp.repo.WeatherPropertyRepository
Expand All @@ -30,7 +31,6 @@ class HomeFragment : Fragment(){
HomeViewModelFactory(weatherPropertyRepository)
)[HomeViewModel::class.java]


initObserve()
return binding.root
}
Expand All @@ -40,11 +40,14 @@ class HomeFragment : Fragment(){
initRecyclerView(it)
}
}
private fun initRecyclerView(weatherResponse: WeatherResponse?) {
private fun initRecyclerView(weatherResponse : WeatherResponse?) {

val adapter = WeatherDataAdapter(weatherResponse ?: throw IllegalAccessError(""))
//findNavController().navigate(HomeFragmentDirections.actionHomeFragmentToSecondFragment(weatherResponse))
binding.recyclerView.adapter = adapter
val adapters = weatherResponse?.let {weatherRes ->
WeatherDataAdapter(weatherRes) {
findNavController().navigate(HomeFragmentDirections.actionHomeFragmentToSecondFragment(weatherRes,it))
}
}
binding.recyclerView.adapter = adapters
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,35 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.hw13_weatherapp.R
import androidx.navigation.fragment.navArgs
import com.example.hw13_weatherapp.constants.addCelcius
import com.example.hw13_weatherapp.databinding.FragmentSecondBinding

class SecondFragment : Fragment() {

private lateinit var binding: FragmentSecondBinding

private val args : SecondFragmentArgs by navArgs()

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentSecondBinding.inflate(inflater)


val weatherData = args.weatherRes
val position = args.dataaa




binding.apply {
title.text = weatherData?.daily?.time?.get(position)
weatherData?.icons.let {
weatherIcon.setImageResource(it?.get(position) ?: position)
}
tvTemperature.text = weatherData?.daily?.apparentTemperatureMax?.get(position).toString().addCelcius()
}

return binding.root
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ import com.example.hw13_weatherapp.databinding.CurrentDayItemBinding
import com.example.hw13_weatherapp.databinding.NextDaysItemBinding
import com.example.hw13_weatherapp.model.data.WeatherResponse

class WeatherDataAdapter(weatherResponse: WeatherResponse) : Adapter<WeatherDataAdapter.WeatherDataViewHolder>() {
class WeatherDataAdapter(private val weatherResponse: WeatherResponse, val onClick: (Int) -> Unit) :
Adapter<WeatherDataAdapter.WeatherDataViewHolder>() {

private val currentWeather = weatherResponse.currentWeather
private val times = weatherResponse.daily.time
private val maxTemps = weatherResponse.daily.apparentTemperatureMax
private val minTemps = weatherResponse.daily.apparentTemperatureMin
private val icons = weatherResponse.icons

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WeatherDataAdapter.WeatherDataViewHolder {

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): WeatherDataAdapter.WeatherDataViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)

val view = when (viewType) {
Expand All @@ -48,8 +53,7 @@ class WeatherDataAdapter(weatherResponse: WeatherResponse) : Adapter<WeatherData
maxTemp = maxTemps[position],
minTemp = minTemps[position],
icon = icons[position]

)
)
}

override fun getItemCount(): Int {
Expand Down Expand Up @@ -88,6 +92,9 @@ class WeatherDataAdapter(weatherResponse: WeatherResponse) : Adapter<WeatherData
tvMaxTemp.text = maxTemp.toString().addCelcius()
ivWeatherIcon.setImageResource(icon)

cardViewCurrentDay.setOnClickListener {
onClick(adapterPosition)
}
}
}
}
Expand Down
117 changes: 61 additions & 56 deletions app/src/main/res/layout/fragment_second.xml
Original file line number Diff line number Diff line change
@@ -1,70 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"

<androidx.cardview.widget.CardView 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"
android:id="@+id/card_view_current_day"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.homeFragment.SecondFragment">

android:layout_margin="10dp"
android:layout_marginVertical="5dp"
app:cardCornerRadius="20dp"
app:cardElevation="10dp"
app:layout_constraintTop_toTopOf="parent">

<ImageView
android:id="@+id/secondBack"
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint_layout_current_day"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="40dp"
android:layout_height="match_parent"
android:background="@drawable/ic_background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:padding="5dp">

<!-- Başlık -->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Today"
android:textColor="#020202"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="146dp"
android:layout_height="201dp"
android:layout_marginBottom="46dp"
android:background="@drawable/ic_cloud"
app:layout_constraintBottom_toBottomOf="@+id/secondBack"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/tv_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:textColor="#6200EE"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title" />

<TextView
android:id="@+id/tv_date2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:text="Monday"
android:textColor="#090809"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/secondBack" />
<ImageView
android:id="@+id/weather_icon"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:src="@drawable/ic_cloud" />
<TextView
android:id="@+id/tv_temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="20°C"
android:textColor="@color/black"
android:textSize="50sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/weather_icon" />

<TextView
android:id="@+id/tv_min_temp2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15.4 °"
android:textColor="#131313"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.872"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/secondBack" />
</androidx.constraintlayout.widget.ConstraintLayout>

<TextView
android:id="@+id/tv_max_temp2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:text="19.0 °"
android:textColor="#000000"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="@+id/imageView2"
app:layout_constraintTop_toBottomOf="@+id/secondBack" />
</androidx.cardview.widget.CardView>


</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/next_days_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardBackgroundColor="#1E1E1E"
app:cardCornerRadius="10dp"
app:cardCornerRadius="30dp"
app:cardElevation="5dp">

<androidx.constraintlayout.widget.ConstraintLayout
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
tools:layout="@layout/fragment_second">
<argument
app:argType="com.example.hw13_weatherapp.model.data.WeatherResponse"
android:name="data"
android:name="weatherRes"
app:nullable="true"/>
<argument
android:name="dataaa"
app:argType="integer" />
</fragment>
</navigation>

0 comments on commit 31b8da4

Please sign in to comment.