Skip to content

Commit

Permalink
HomeScreen Completete
Browse files Browse the repository at this point in the history
  • Loading branch information
Lê Chí Dũng committed Sep 6, 2024
1 parent 55869ec commit 828801f
Show file tree
Hide file tree
Showing 22 changed files with 506 additions and 134 deletions.
21 changes: 17 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
buildscript {
apply from: "ktlint.gradle"

}

plugins {
Expand Down Expand Up @@ -43,11 +44,15 @@ android {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '1.8'
jvmTarget = '17'
}
buildFeatures {
viewBinding = true
buildConfig=true
}
buildFeatures {
viewBinding = true
Expand All @@ -70,22 +75,30 @@ dependencies {
implementation 'com.github.bumptech.glide:glide:4.16.0'
implementation 'com.airbnb.android:lottie:3.4.0'
// Room
implementation "androidx.room:room-ktx:2.6.1"
implementation "androidx.room:room-runtime:2.6.1"
implementation "androidx.legacy:legacy-support-v4:1.0.0"
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0'
annotationProcessor "androidx.room:room-compiler:2.6.1"
kapt "androidx.room:room-compiler:2.6.1"

// retrofit
implementation "com.squareup.retrofit2:retrofit:2.11.0"
implementation "com.squareup.retrofit2:converter-gson:2.11.0"
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.11'
// ViewModel
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0'
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.7.0"
// Koin
implementation "io.insert-koin:koin-android:3.5.0"
// coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3"
// Rx
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
implementation 'androidx.room:room-rxjava2:2.0.0'
// WorkManager
implementation 'androidx.work:work-runtime-ktx:2.9.0'

}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name=".AndroidApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/com/sun/weather/AndroidApplication.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package com.sun.weather

import android.app.Application
import com.sun.weather.di.AppModule
import com.sun.weather.di.DataSourceModule
import com.sun.weather.di.NetworkModule
import com.sun.weather.di.RepositoryModule
import com.sun.weather.di.ViewModelModule
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidFileProperties
import org.koin.core.context.startKoin

class AndroidApplication : Application() {
private val rootModule = listOf(AppModule, NetworkModule, DataSourceModule, RepositoryModule, ViewModelModule)

override fun onCreate() {
super.onCreate()

startKoin {
androidContext(this@AndroidApplication)
androidFileProperties()
modules(rootModule)
}
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/sun/weather/base/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import androidx.viewbinding.ViewBinding
import com.sun.weather.ui.SharedViewModel

typealias ActivityInflate<T> = (LayoutInflater) -> T

Expand All @@ -14,6 +15,7 @@ abstract class BaseActivity<VB : ViewBinding>(private val inflate: ActivityInfla

val binding get() = _binding!!
abstract val viewModel: ViewModel
abstract val sharedViewModel: SharedViewModel

protected abstract fun initialize()

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/sun/weather/base/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.viewbinding.ViewBinding
import com.sun.weather.ui.SharedViewModel

typealias FragmentInflate<T> = (LayoutInflater, ViewGroup?, Boolean) -> T

Expand All @@ -15,6 +16,7 @@ abstract class BaseFragment<VB : ViewBinding>(private val inflate: FragmentInfla
private var _binding: VB? = null
val binding get() = _binding!!
abstract val viewModel: ViewModel
abstract val sharedViewModel: SharedViewModel

protected abstract fun initView()

Expand Down
108 changes: 68 additions & 40 deletions app/src/main/java/com/sun/weather/data/model/CurrentWeather.kt
Original file line number Diff line number Diff line change
@@ -1,77 +1,101 @@
package com.sun.weather.data.model

import com.example.weather.utils.ext.combineWithCountry
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
import com.sun.weather.data.model.entity.WeatherEntity
import com.sun.weather.utils.ext.combineWithCountry

data class CurrentWeather(
@SerializedName("main")
var main: Main,
@Expose
var main: Main? = null,
@SerializedName("weather")
var weathers: List<Weather>,
@Expose
var weathers: List<Weather>? = null,
@SerializedName("wind")
var wind: Wind,
@Expose
var wind: Wind? = null,
@SerializedName("clouds")
var clouds: Clouds,
@Expose
var clouds: Clouds? = null,
@SerializedName("coord")
var coord: Coord,
@Expose
var coord: Coord? = Coord(0.0, 0.0),
@SerializedName("dt")
var dt: Long,
@Expose
var dt: Long? = null,
@SerializedName("name")
var nameCity: String,
@Expose
var nameCity: String? = null,
@SerializedName("sys")
var sys: Sys,
var day: String = "",
var iconWeather: String = "",
@Expose
var sys: Sys? = null,
var day: String? = "",
var iconWeather: String? = "",
)

data class Main(
@SerializedName("temp")
var currentTemperature: Double,
@Expose
var currentTemperature: Double? = null,
@SerializedName("humidity")
var humidity: Int,
@SerializedName("temp_min") var tempMin: Double,
@SerializedName("temp_max") var tempMax: Double,
@Expose
var humidity: Int? = null,
@SerializedName("temp_min")
@Expose
var tempMin: Double? = null,
@SerializedName("temp_max")
@Expose
var tempMax: Double? = null,
)

data class Weather(
@SerializedName("icon")
var iconWeather: String,
@Expose
var iconWeather: String? = null,
@SerializedName("main")
var main: String,
@Expose
var main: String? = null,
@SerializedName("description")
var description: String,
@Expose
var description: String? = null,
)

data class Wind(
@SerializedName("speed")
var windSpeed: Double,
@Expose
var windSpeed: Double? = null,
)

data class Clouds(
@SerializedName("all")
var percentCloud: Int,
@Expose
var percentCloud: Int? = null,
)

data class Coord(
@SerializedName("lon")
var lon: Double,
@Expose
var lon: Double? = null,
@SerializedName("lat")
var lat: Double,
@Expose
var lat: Double? = null,
)

data class Sys(
@SerializedName("country")
var country: String,
@Expose
var country: String? = null,
)

fun CurrentWeather.toWeatherEntity(): WeatherEntity {
val country: String? = sys.country
val id = nameCity.combineWithCountry(country)
val latitude = coord.lat
val longitude = coord.lon
val timeZone = dt
val city = nameCity
val country: String = sys?.country ?: "Unknown"
val id = nameCity?.combineWithCountry(country) ?: "Unknown"
val latitude = coord?.lat ?: 0.0
val longitude = coord?.lon ?: 0.0
val timeZone = dt ?: 0
val city = nameCity ?: "Unknown"
val weatherCurrent: WeatherBasic? = this.toWeatherBasic()
val weatherHourlyList: List<WeatherBasic>? = null
val weatherDailyList: List<WeatherBasic>? = null
Expand All @@ -89,16 +113,20 @@ fun CurrentWeather.toWeatherEntity(): WeatherEntity {
)
}

fun CurrentWeather.toWeatherBasic(): WeatherBasic {
return WeatherBasic(
dateTime = dt,
currentTemperature = main.currentTemperature,
maxTemperature = main.tempMax,
minTemperature = main.tempMin,
iconWeather = weathers.firstOrNull()?.iconWeather,
weatherDescription = weathers.firstOrNull()?.description,
humidity = main.humidity,
percentCloud = clouds.percentCloud,
windSpeed = wind.windSpeed,
)
fun CurrentWeather.toWeatherBasic(): WeatherBasic? {
return if (main != null && weathers?.isNotEmpty() == true && wind != null && clouds != null) {
WeatherBasic(
dateTime = dt ?: 0,
currentTemperature = main?.currentTemperature ?: 0.0,
maxTemperature = main?.tempMax ?: 0.0,
minTemperature = main?.tempMin ?: 0.0,
iconWeather = weathers?.firstOrNull()?.iconWeather,
weatherDescription = weathers?.firstOrNull()?.description,
humidity = main?.humidity ?: 0,
percentCloud = clouds?.percentCloud ?: 0,
windSpeed = wind?.windSpeed ?: 0.0,
)
} else {
null
}
}
Loading

0 comments on commit 828801f

Please sign in to comment.