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
1 change: 1 addition & 0 deletions .idea/gradle.xml

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

2 changes: 1 addition & 1 deletion .idea/misc.xml

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

45 changes: 45 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,67 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

androidExtensions {
experimental = true
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

buildTypes.each {
it.buildConfigField 'String', 'API_ENDPOINT', baseUrl
it.buildConfigField 'String', 'API_KEY', apiKey
}

compileOptions {
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
}
}

def retrofit = "2.9.0"

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0'
implementation 'com.google.android.material:material:1.1.0'



// region Retrofit
implementation "com.squareup.retrofit2:retrofit:${retrofit}"
implementation "com.squareup.retrofit2:converter-gson:${retrofit}"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.9.0"
// endregion

//RecylerView
implementation 'androidx.recyclerview:recyclerview:1.1.0'

//CircleImage
implementation 'de.hdodenhof:circleimageview:3.1.0'

//Google Service Location
implementation 'com.google.android.gms:play-services-location:15.0.1'

implementation 'com.squareup.okhttp3:logging-interceptor:3.12.8' // 3.13.+ require minSDK 21

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'




}
8 changes: 7 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidpractice2020">

<uses-permission android:name="android.permission.INTERNET"/>
<!-- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
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=".MainActivity">
<activity android:name=".ui.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" />
</application>

</manifest>
Binary file added app/src/main/ic_geo-playstore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 0 additions & 11 deletions app/src/main/java/com/example/androidpractice2020/MainActivity.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.example.androidpractice2020.factory

import com.example.androidpractice2020.BuildConfig
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object ApiFactory {

private val QUERY_API_KEY = "appid"
private val QUERY_UNIT = "units"


private val apiKeyInterceptor = Interceptor { chain ->
val original = chain.request()
original.url().newBuilder()
.addQueryParameter(QUERY_API_KEY, BuildConfig.API_KEY)
.build().let {
chain.proceed(
original.newBuilder().url(it).build()
)
}
}

private val unitsInterceptor = Interceptor { chain ->
val original = chain.request()
original.url().newBuilder()
.addQueryParameter(QUERY_UNIT, "metric")
.build().let {
chain.proceed(
original.newBuilder().url(it).build()
)
}
}

private val client by lazy {
OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.addInterceptor(apiKeyInterceptor)
.addInterceptor(unitsInterceptor)
.addInterceptor(LoggingInterceptor())
.build()
}

private val retrofit by lazy {
Retrofit.Builder()
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BuildConfig.API_ENDPOINT)
.build()
}

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


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.androidpractice2020.factory

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

data class InfoCity(
val clouds: Clouds,
val coord: Coord,
val dt: Int,
val id: Int,
val main: Main,
val name: String,
val rain: Any,
val snow: Any,
val sys: Sys,
val weather: List<Weather>,
val wind: Wind
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.androidpractice2020.factory

import com.example.androidpractice2020.BuildConfig
import okhttp3.logging.HttpLoggingInterceptor
import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException

class LoggingInterceptor(
private val loggingInterceptor: HttpLoggingInterceptor =
HttpLoggingInterceptor().setLevel(
if (BuildConfig.DEBUG)
HttpLoggingInterceptor.Level.BODY
else
HttpLoggingInterceptor.Level.NONE
)
) : Interceptor {

@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response =
loggingInterceptor.intercept(chain)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.androidpractice2020.factory

import retrofit2.http.GET
import retrofit2.http.Query

interface WeatherApi {

@GET("weather?lang=ru")
suspend fun getWeather(
@Query("q") cityName: String
): WeatherResponse

@GET("find?lang=ru")
suspend fun getCitiesWeather(
@Query("lat") latitudeUser: Double,
@Query("lon") longitude: Double,
@Query("cnt") countCities: Int
): CitiesWeatherResponse

@GET("weather?lang=ru")
suspend fun getCityById(
@Query("id") cityId: Int
): WeatherResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.example.androidpractice2020.factory


data class WeatherResponse(
val base: String,
val clouds: Clouds,
val cod: Int,
val coord: Coord,
val dt: Int,
val id: Int,
val main: Main,
val name: String,
val sys: Sys,
val timezone: Int,
val visibility: Int,
val weather: List<Weather>,
val wind: Wind
)

data class Clouds(
val all: Int
)

data class Coord(
val lat: Double,
val lon: Double
)

data class Main(
val feels_like: Double,
val humidity: Int,
val pressure: Int,
val temp: Double,
val temp_max: Double,
val temp_min: Double
)

data class Sys(
val country: String,
val id: Int,
val sunrise: Int,
val sunset: Int,
val type: Int
)

data class Weather(
val description: String,
val icon: String,
val id: Int,
val main: String
)

data class Wind(
val deg: Int,
val speed: Double
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.androidpractice2020.recyclerview

data class City(
var id: Int,
var name: String,
var temp: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.androidpractice2020.recyclerview

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.androidpractice2020.factory.InfoCity

class CityAdapter(var list: ArrayList<InfoCity>, var itemClick: (Int) -> Unit): RecyclerView.Adapter<CityHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
CityHolder =
CityHolder.create(
parent,
itemClick
)

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

override fun onBindViewHolder(holder: CityHolder, position: Int) = holder.bind(list[position])


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.androidpractice2020.recyclerview

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.androidpractice2020.factory.InfoCity
import kotlinx.android.extensions.LayoutContainer
import com.example.androidpractice2020.R
import kotlinx.android.synthetic.main.city_item.*

class CityHolder(override val containerView: View, var itemClick: (Int) -> Unit) :
RecyclerView.ViewHolder(containerView), LayoutContainer {

fun bind(city: InfoCity) {

text_name_list.text = city.name
text_temp_list.text = "${city.main.temp} \u2103"

when(city.main.temp) {
in -30.0..-18.0 -> text_temp_list.setTextColor(-65536)
in -18.0..-6.0 -> text_temp_list.setTextColor(-256)
in -6.0..6.0 -> text_temp_list.setTextColor(-16711681)
in 6.0..18.0 -> text_temp_list.setTextColor(-16711936)
in 18.0..30.0 -> text_temp_list.setTextColor(-16777216)
}

itemView.setOnClickListener {
itemClick(city.id)
}
}

companion object {
fun create(parent: ViewGroup, itemClick: (Int) -> Unit): CityHolder =
CityHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.city_item,
parent,
false
),
itemClick
)
}

}
Loading