Skip to content
Open
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
22 changes: 19 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ android {
targetSdkVersion 30
versionCode 1
versionName "1.0"
apply plugin: 'kotlin-kapt'

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}



androidExtensions {
experimental = true
}

buildFeatures {
viewBinding true
}

buildTypes {
release {
minifyEnabled false
Expand Down Expand Up @@ -55,6 +62,18 @@ dependencies {



def room_version = "2.2.6"

implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"

// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"

// optional - Test helpers
testImplementation "androidx.room:room-testing:$room_version"


// region Retrofit
implementation "com.squareup.retrofit2:retrofit:${retrofit}"
implementation "com.squareup.retrofit2:converter-gson:${retrofit}"
Expand All @@ -76,7 +95,4 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'




}
5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
android:name=".application.WeatherApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ui.MainActivity">
<activity android:name=".presentation.MainActivity">

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ui.DetailCityActivity" />
<activity android:name=".presentation.DetailCityActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.androidpractice2020.application

import android.app.Application
import com.example.androidpractice2020.data.api.ApiFactory
import com.example.androidpractice2020.data.database.WeatherRoomDatabase
import com.example.androidpractice2020.data.database.repository.WeatherRepositoryImpl

class WeatherApplication : Application() {

val database by lazy {
WeatherRoomDatabase.getDataBase(this)
}

val repository by lazy {
WeatherRepositoryImpl(
ApiFactory.weatherApi,
database.weatherDao()
)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.androidpractice2020.factory
package com.example.androidpractice2020.data.api

import com.example.androidpractice2020.BuildConfig
import okhttp3.Interceptor
Expand Down Expand Up @@ -53,7 +53,9 @@ object ApiFactory {
}

val weatherApi by lazy {
retrofit.create(WeatherApi::class.java)
retrofit.create(
WeatherApi::class.java
)
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.androidpractice2020.factory
package com.example.androidpractice2020.data.api

import com.example.androidpractice2020.BuildConfig
import okhttp3.logging.HttpLoggingInterceptor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.androidpractice2020.factory
package com.example.androidpractice2020.data.api

import com.example.androidpractice2020.data.api.response.CitiesWeatherResponse
import com.example.androidpractice2020.data.api.response.WeatherResponse
import retrofit2.http.GET
import retrofit2.http.Query

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.androidpractice2020.factory
package com.example.androidpractice2020.data.api.response

data class CitiesWeatherResponse(
val cod: String,
val count: Int,
val list: List<InfoCity>,
val list: ArrayList<InfoCity>,
val message: String
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.androidpractice2020.factory
package com.example.androidpractice2020.data.api.response


data class WeatherResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.androidpractice2020.data.database

import android.content.Context
import androidx.room.*
import com.example.androidpractice2020.data.database.dao.WeatherDao
import com.example.androidpractice2020.data.database.entity.City

@Database(entities = arrayOf(City::class), version = 2, exportSchema = false)
abstract class WeatherRoomDatabase : RoomDatabase() {

abstract fun weatherDao(): WeatherDao

companion object {

@Volatile
private var INSTANCE: WeatherRoomDatabase? = null

fun getDataBase(context: Context): WeatherRoomDatabase {
return INSTANCE
?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
WeatherRoomDatabase::class.java,
"city_database"
).fallbackToDestructiveMigration()
.build()
INSTANCE = instance

instance
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.androidpractice2020.data.database.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.example.androidpractice2020.data.database.entity.City

@Dao
interface WeatherDao {


suspend fun insert(list: List<City>) {
insrt(*list.toTypedArray())
}

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insrt(vararg list: City)

@Query("SELECT * FROM city")
suspend fun getAllCities(): List<City>

@Query("SELECT * FROM city WHERE id = :idCity")
suspend fun getCityById(idCity: Int): City

@Query("SELECT * FROM city WHERE name = :nameCity")
suspend fun getCityByName(nameCity: String): City

@Query("DELETE FROM city")
suspend fun deleteAllCity()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.androidpractice2020.data.database.entity

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.example.androidpractice2020.data.api.response.InfoCity

@Entity(tableName = "city")
data class City(
@PrimaryKey
@ColumnInfo(name = "id")
var id: Int,
@ColumnInfo(name = "name")
var name: String,
@ColumnInfo(name = "temperature")
var temp: Double,
@ColumnInfo(name = "description")
var description: String,
@ColumnInfo(name = "numb_humidity")
var numbHumidity: Int,
@ColumnInfo(name = "deg")
var deg: Int
) {
companion object {
fun mapResponsetoEntity(infoCity: InfoCity): City {
with(infoCity) {
return City(
id, name, main.temp, weather[0].description, main.humidity, wind.deg
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.example.androidpractice2020.data.database.repository

import com.example.androidpractice2020.data.api.WeatherApi
import com.example.androidpractice2020.data.database.dao.WeatherDao
import com.example.androidpractice2020.data.database.entity.City
import com.example.androidpractice2020.domain.WeatherRepository
import java.lang.Exception
import java.net.UnknownHostException

class WeatherRepositoryImpl(
private val weatherApi: WeatherApi,
private val weatherDao: WeatherDao
) : WeatherRepository {


override suspend fun getWeatherByName(cityName: String) =
try {
val weatherResponse = weatherApi.getWeather(cityName)
City(
weatherResponse.id,
weatherResponse.name,
weatherResponse.main.temp,
weatherResponse.weather[0].description,
weatherResponse.main.humidity,
weatherResponse.wind.deg
)

} catch (e: Exception) {
when (e) {
is UnknownHostException -> weatherDao.getCityByName(cityName)
else -> City(
-1,
"",
-1.0,
"",
-1,
1
)
}
}

override suspend fun getCityById(cityId: Int): City =
try {
val weatherResponse = weatherApi.getCityById(cityId)
City(
weatherResponse.id,
weatherResponse.name,
weatherResponse.main.temp,
weatherResponse.weather[0].description,
weatherResponse.main.humidity,
weatherResponse.wind.deg
)
} catch (e: UnknownHostException) {
weatherDao.getCityById(cityId)
}

override suspend fun getCitiesWeather(
latitudeUser: Double,
longitude: Double,
countCities: Int
): ArrayList<City> {
var result: ArrayList<City> = arrayListOf()
try {
var weatherResponse =
weatherApi.getCitiesWeather(latitudeUser, longitude, countCities).list
var i = 0
while (i < weatherResponse.size) {
result.add(City.mapResponsetoEntity(weatherResponse[i]))
i++
}
weatherDao.deleteAllCity()
weatherDao.insert(result)
} catch (e: UnknownHostException) {
result = weatherDao.getAllCities() as ArrayList<City>
}
return result
}

// @SuppressLint("MissingPermission")
// override suspend fun getGeoPositionUser(
// applicationContext: Context,
// boolean: Boolean
// ): DoubleArray {
// var fusedLocationProviderClient =
// LocationServices.getFusedLocationProviderClient(applicationContext)
// var longitude = 37.6156
// var latitude = 55.7522
// fusedLocationProviderClient.lastLocation.addOnCompleteListener { task ->
// var location: Location? = task.result
// if (location != null) {
// longitude = location.longitude
// latitude = location.latitude
// Log.e("qweqw", "$latitude")
// Log.e("qweqwe", "$longitude")
// }
// }
// return doubleArrayOf(latitude, longitude)
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.androidpractice2020.domain

import com.example.androidpractice2020.data.database.entity.City
import com.example.androidpractice2020.data.database.repository.WeatherRepositoryImpl
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext

class FindCityUseCase(
private val weatherRepositoryImpl: WeatherRepositoryImpl,
private val context: CoroutineContext
) {

suspend fun findWeatherCityByName(cityName: String): City =
withContext(context) {
weatherRepositoryImpl.getWeatherByName(cityName)
}

suspend fun findWeatherCityById(cityId: Int): City =
withContext(context) {
weatherRepositoryImpl.getCityById(cityId)
}

suspend fun findWeatherCitiesByCoordinates(
latitudeUser: Double,
longitudeUser: Double,
countCities: Int
): ArrayList<City> =
withContext(context) {
weatherRepositoryImpl.getCitiesWeather(latitudeUser, longitudeUser, countCities)
}

// suspend fun getCoordinatesByUser(
// applicationContext: Context, boolean: Boolean
// ): DoubleArray =
// withContext(context) {
// weatherRepositoryImpl.getGeoPositionUser(applicationContext, boolean)
// }
}
Loading