Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DetailScreen Complete #8

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ dependencies {
// WorkManager
implementation 'androidx.work:work-runtime-ktx:2.9.0'

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.sun.weather.data.model.entity.WeatherEntity
import com.sun.weather.data.repository.source.local.converter.WeatherBasicConverter
import com.sun.weather.data.repository.source.local.converter.WeatherBasicListConverter
import com.sun.weather.data.repository.source.local.dao.WeatherDao

@Database(
entities = [Weather::class],
version = 1,
exportSchema = false,
)
@Database(entities = [WeatherEntity::class], version = 1, exportSchema = false)
@TypeConverters(WeatherBasicConverter::class, WeatherBasicListConverter::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun weatherDao(): WeatherDao
Expand All @@ -24,7 +21,7 @@ abstract class AppDatabase : RoomDatabase() {
@Volatile
private var instance: AppDatabase? = null

fun getInstance(context: Context): AppDatabase =
fun getInstance(context: Context) =
instance ?: synchronized(this) {
instance ?: buildDatabase(context).also { instance = it }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package com.sun.weather.data.repository.source.local

import com.sun.weather.data.model.entity.WeatherEntity
import com.sun.weather.data.repository.source.WeatherDataSource
import com.sun.weather.data.repository.source.local.dao.WeatherDao

class WeatherLocalDataSource(
private val weatherDao: WeatherDao,
) : WeatherDataSource.Local {
override suspend fun insertCurrentWeather(
current: Weather,
hourly: Weather,
daily: Weather,
current: WeatherEntity,
hourly: WeatherEntity,
daily: WeatherEntity,
) {
// TODO Implement later
}

override suspend fun insertCurrentWeather(weather: Weather) {
// TODO Implement later
override suspend fun insertCurrentWeather(weather: WeatherEntity) {
}

override suspend fun getAllLocalWeathers(): List<Weather> {
override suspend fun getAllLocalWeathers(): List<WeatherEntity> {
return weatherDao.getAllData()
}

override suspend fun getLocalWeather(id: String): Weather? {
override suspend fun getLocalWeather(id: String): WeatherEntity? {
return weatherDao.getWeather(id)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import com.sun.weather.data.repository.source.WeatherDataSource
import com.sun.weather.data.repository.source.remote.api.ApiService
import com.sun.weather.utils.Constant

class WeatherRemoteDataSource(private val apiService: ApiService) : WeatherDataSource.Remote {
class WeatherRemoteDataSource(
private val baseApiService: ApiService,
private val proApiService: ApiService,
) : WeatherDataSource.Remote {
override suspend fun getCurrentWeather(
city: String,
language: String,
): CurrentWeather {
return apiService.getCurrentWeather(
return baseApiService.getCurrentWeather(
city,
Constant.BASE_API_KEY,
Constant.UNITS_VALUE,
Expand All @@ -25,7 +28,7 @@ class WeatherRemoteDataSource(private val apiService: ApiService) : WeatherDataS
lon: Double,
language: String,
): CurrentWeather {
return apiService.getCurrentLocationWeather(
return baseApiService.getCurrentLocationWeather(
lat,
lon,
Constant.BASE_API_KEY,
Expand All @@ -38,7 +41,7 @@ class WeatherRemoteDataSource(private val apiService: ApiService) : WeatherDataS
city: String,
language: String,
): HourlyForecast {
return apiService.getHourlyForecast(
return proApiService.getHourlyForecast(
city,
Constant.UNITS_VALUE,
Constant.FORECAST_HOUR,
Expand All @@ -51,7 +54,7 @@ class WeatherRemoteDataSource(private val apiService: ApiService) : WeatherDataS
city: String,
language: String,
): WeeklyForecast {
return apiService.getWeeklyForecast(
return proApiService.getWeeklyForecast(
city,
Constant.UNITS_VALUE,
Constant.FORECAST_DAY,
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/sun/weather/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sun.weather.di

import android.app.Application
import android.content.Context
import android.content.res.Resources
import com.example.weather.utils.dispatchers.BaseDispatcherProvider
import com.example.weather.utils.dispatchers.DispatcherProvider
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/sun/weather/di/DataSourceModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ package com.sun.weather.di
import com.sun.weather.data.repository.source.WeatherDataSource
import com.sun.weather.data.repository.source.local.WeatherLocalDataSource
import com.sun.weather.data.repository.source.remote.WeatherRemoteDataSource
import org.koin.core.qualifier.named
import org.koin.dsl.module

val DataSourceModule =
module {
single<WeatherDataSource.Remote> { WeatherRemoteDataSource(get()) }
single<WeatherDataSource.Remote> {
WeatherRemoteDataSource(
baseApiService = get(named("baseApiService")),
proApiService = get(named("proApiService")),
)
}
single<WeatherDataSource.Local> { WeatherLocalDataSource(get()) }
}
25 changes: 16 additions & 9 deletions app/src/main/java/com/sun/weather/di/NetworkModule.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@

package com.sun.weather.di

import android.app.Application
import com.google.gson.Gson
import com.sun.weather.data.repository.source.remote.api.ApiService
import com.sun.weather.utils.Constant
import com.sun.weather.utils.Constant.BASE_API_SERVICE
import com.sun.weather.utils.Constant.BASE_RETROFIT
import com.sun.weather.utils.Constant.PRO_API_SERVICE
import com.sun.weather.utils.Constant.PRO_RETROFIT
import okhttp3.Cache
import okhttp3.OkHttpClient
import org.koin.core.qualifier.named
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
Expand All @@ -14,12 +19,11 @@ import java.util.concurrent.TimeUnit
val NetworkModule =
module {
single { provideOkHttpCache(get()) }

single { provideOkHttpClient(get()) }

single { provideRetrofit(get(), get()) }

single { provideApiService(get()) }
single<Retrofit>(named(BASE_RETROFIT)) { provideRetrofit(Constant.BASE_URL, get(), get()) }
single<Retrofit>(named(PRO_RETROFIT)) { provideRetrofit(Constant.PRO_URL, get(), get()) }
single<ApiService>(named(BASE_API_SERVICE)) { provideApiService(get(named(BASE_RETROFIT))) }
single<ApiService>(named(PRO_API_SERVICE)) { provideApiService(get(named(PRO_RETROFIT))) }
}

fun provideOkHttpCache(app: Application): Cache {
Expand All @@ -43,16 +47,19 @@ fun provideOkHttpClient(cache: Cache): OkHttpClient {
NetWorkInstant.CONNECT_TIMEOUT,
TimeUnit.SECONDS,
)

return httpClientBuilder.build()
}

fun provideRetrofit(
baseUrl: String,
gson: Gson,
okHttpClient: OkHttpClient,
): Retrofit {
return Retrofit.Builder().baseUrl(Constant.BASE_URL).addConverterFactory(GsonConverterFactory.create(gson))
.client(okHttpClient).build()
return Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okHttpClient)
.build()
}

fun provideApiService(retrofit: Retrofit): ApiService {
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/sun/weather/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import com.sun.weather.data.repository.WeatherRepositoryImpl
import com.sun.weather.data.repository.source.WeatherDataSource
import com.sun.weather.data.repository.source.local.WeatherLocalDataSource
import com.sun.weather.data.repository.source.remote.WeatherRemoteDataSource
import com.sun.weather.utils.Constant.BASE_API_SERVICE
import com.sun.weather.utils.Constant.PRO_API_SERVICE
import org.koin.core.qualifier.named
import org.koin.dsl.module

val RepositoryModule =
module {
single {
provideWeatherRepository(
WeatherLocalDataSource(get()),
WeatherRemoteDataSource(get()),
WeatherRemoteDataSource(
baseApiService = get(named(BASE_API_SERVICE)),
proApiService = get(named(PRO_API_SERVICE)),
),
)
}
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/sun/weather/di/ViewModelModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.sun.weather.di

import com.sun.weather.ui.MainViewModel
import com.sun.weather.ui.SharedViewModel
import com.sun.weather.ui.detail.DetailViewModel
import com.sun.weather.ui.home.HomeViewModel
import com.sun.weather.ui.search.SearchViewModel
import org.koin.androidx.viewmodel.dsl.viewModel
Expand All @@ -14,4 +15,5 @@ val ViewModelModule: Module =
viewModel { SharedViewModel() }
viewModel { HomeViewModel(get()) }
viewModel { SearchViewModel(get()) }
viewModel { DetailViewModel(get()) }
}
6 changes: 2 additions & 4 deletions app/src/main/java/com/sun/weather/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::infl
this,
Observer { location ->
val (latitude, longitude) = location
// Chuyển dữ liệu tọa độ sang HomeFragment
val homeFragment = HomeFragment.newInstance(latitude, longitude)
sharedViewModel.setLocation(latitude, longitude)
val homeFragment = HomeFragment.newInstance()
setNextFragment(homeFragment)
},
)

// Gọi hàm yêu cầu vị trí người dùng
viewModel.requestLocationAndFetchWeather(this)
}

Expand Down
57 changes: 57 additions & 0 deletions app/src/main/java/com/sun/weather/ui/detail/DailyAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.sun.weather.ui.detail

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.sun.weather.data.model.WeatherBasic
import com.sun.weather.databinding.ForecastDailyBinding
import com.sun.weather.utils.Constant
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class DailyAdapter(private var forecastList: List<WeatherBasic>) : RecyclerView.Adapter<DailyAdapter.ViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return ViewHolder(ForecastDailyBinding.inflate(inflater, parent, false))
}

override fun getItemCount(): Int {
return forecastList.size
}

override fun onBindViewHolder(
holder: ViewHolder,
position: Int,
) {
holder.bindData(forecastList[position])
}

inner class ViewHolder(private val binding: ForecastDailyBinding) : RecyclerView.ViewHolder(binding.root) {
fun bindData(item: WeatherBasic) {
val dayFormat = SimpleDateFormat(DAY_ONLY_PATTERN, Locale.getDefault())
val day = dayFormat.format(Date(item.dateTime!! * SECOND_TO_MILLIS))
binding.tvDay.text = day
binding.tvStatus.text = item.weatherDescription
binding.tvMaxTemp.text = String.format("%.1f", item.maxTemperature) + "°C"
binding.tvMinTemp.text = String.format("%.1f", item.minTemperature) + "°C"
val iconWeatherUrl = "${Constant.BASE_ICON_URL}${item.iconWeather}@2x.png"
Glide.with(itemView.context.applicationContext)
.load(iconWeatherUrl)
.into(binding.imgStatus)
}
}

fun updateData(list: List<WeatherBasic>) {
forecastList = list
notifyDataSetChanged()
}
companion object {
const val DAY_ONLY_PATTERN = "dd-MM-yyyy"
const val SECOND_TO_MILLIS = 1000
}
}
Loading
Loading