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

Feature/UI details #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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/.name

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

2 changes: 2 additions & 0 deletions .idea/misc.xml

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

2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ dependencies {
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
implementation 'com.airbnb.android:lottie:5.2.0'
implementation "io.reactivex.rxjava3:rxjava:3.1.5"

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheese.weatherapp.models
package com.cheese.weatherapp.data.models

data class Clouds (
val all: Double
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheese.weatherapp.models
package com.cheese.weatherapp.data.models

data class Coord (
val lon: Double,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheese.weatherapp.models
package com.cheese.weatherapp.data.models

import com.google.gson.annotations.SerializedName

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheese.weatherapp.models
package com.cheese.weatherapp.data.models

import com.google.gson.annotations.SerializedName

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheese.weatherapp.models
package com.cheese.weatherapp.data.models

data class Sys (
val type: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheese.weatherapp.models
package com.cheese.weatherapp.data.models

data class Weather (
// val id: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.example.mikmok.data.models
import com.cheese.weatherapp.models.*
import com.cheese.weatherapp.data.models.*

data class WeatherMain(
val wind: Wind,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheese.weatherapp.models
package com.cheese.weatherapp.data.models

enum class WeatherState {
CLOUDS,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheese.weatherapp.models
package com.cheese.weatherapp.data.models

data class Wind (
val speed: Double,
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/com/cheese/weatherapp/data/request/ApiClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.cheese.weatherapp.data.request

import com.example.mikmok.util.Constants
import okhttp3.Call
import okhttp3.OkHttpClient
import okhttp3.Request

class ApiClient {

private val client = OkHttpClient()
fun makeApiRequest(cityName :String): Call {
val request = Request.Builder()
.url(Constants.API_URL+"${cityName.trim()}")
.get()
.addHeader(Constants.ACCEPT, Constants.TYPE)
.build()

return client.newCall(request)
}

}
62 changes: 25 additions & 37 deletions app/src/main/java/com/cheese/weatherapp/ui/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,75 +1,63 @@
package com.cheese.weatherapp.ui
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.widget.Toast
import androidx.core.widget.doOnTextChanged
import com.cheese.weatherapp.R
import com.cheese.weatherapp.databinding.ActivityMainBinding
import com.cheese.weatherapp.models.WeatherState
import com.cheese.weatherapp.data.models.WeatherState
import com.cheese.weatherapp.data.request.ApiClient
import com.example.mikmok.data.models.WeatherMain
import com.example.mikmok.util.Constants
import com.example.mikmok.util.toCelsius
import com.example.mikmok.util.toPercent
import com.google.gson.Gson
import io.reactivex.rxjava3.core.Observable
import okhttp3.*
import java.io.IOException
import java.util.concurrent.TimeUnit

class MainActivity : BaseActivity<ActivityMainBinding>(){
private val client = OkHttpClient()
var cityName:String =Constants.CITY
val gson =Gson()
private val apiClient by lazy { ApiClient() }
override val LOG_TAG: String =Constants.MAIN_ACTIVITY
override val bindingInflater: (LayoutInflater) -> ActivityMainBinding = ActivityMainBinding::inflate

override fun setUp() {
getWeather(cityName=cityName)
}

override fun addCallbacks() {
onSearchChange()
onClickSearchButton()
}

private fun onClickSearchButton()= binding.buttonSearch.setOnClickListener {
cityName = binding.searchCity.text.toString()
getWeather(cityName=cityName)
onSearchChange()
}

private fun onSearchChange() = binding.searchCity.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable) {}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
visibleButton()
}
})

private fun visibleButton(){
binding.buttonSearch.visibility =android.view.View.VISIBLE
if(binding.searchCity.text.isEmpty()){
binding.buttonSearch.visibility =android.view.View.INVISIBLE
}
else{
binding.buttonSearch.visibility =android.view.View.VISIBLE
}
private fun onSearchChange(){
val observableSearch =Observable.create<String>{ emitter->
binding.searchCity.doOnTextChanged { text, start, before, count ->
if (count !=Constants.INDEXT_COUNT_EMPTY) {
emitter.onNext(text.toString())
}
}
}.debounce(Constants.TIME.toLong(),TimeUnit.SECONDS)
observableSearch.subscribe(
{cityName->getWeather(cityName=cityName)},
{notFound-> showToast(message = Constants.CITY_NOT_FOUND)
}
)
}

private fun getWeather(cityName:String) {
val request = Request.Builder()
.url(Constants.API_URL+"${cityName.trim()}")
.get()
.addHeader(Constants.ACCEPT, Constants.TYPE)
.build()

client.newCall(request).enqueue(object : Callback {
private fun getWeather(cityName:String) {
apiClient.makeApiRequest(cityName=cityName.trim()).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
showToast("${Constants.ON_FAILURE} ${e.message}")
}
override fun onResponse(call: Call, response: Response) {
response.body?.string()?.let { jsonString ->
val result = Gson().fromJson(jsonString, WeatherMain::class.java)
val result = gson.fromJson(jsonString, WeatherMain::class.java)

runOnUiThread {
if (result.cod.toString() == Constants.NOT_FOUND) {
showToast(result.message)
showToast(result.message)
}
else {
attributBinding(result)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/cheese/weatherapp/util/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package com.example.mikmok.util
object Constants {
const val API_URL = "https://api.openweathermap.org/data/2.5/weather?appid=8e17fb89454f5e7a82cf6d96a082c0ad&q="
const val INDEXT_WEATHER =0
const val INDEXT_COUNT_EMPTY =0
const val NOT_FOUND = "404"
const val ON_FAILURE = "Failed to grab the data due to:"
const val ACCEPT ="Accept"
const val TYPE ="application/json"
const val CITY ="Baghdad"
const val MAIN_ACTIVITY ="MAIN_ACTIVITY"
const val CITY_NOT_FOUND ="city not found"
const val TIME = 1
}
5 changes: 3 additions & 2 deletions app/src/main/res/drawable/background.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:endColor="@color/color_up"
android:startColor="@color/color_down"

android:endColor="@color/color_up2"
android:startColor="@color/color_down2"
android:angle="90"
android:type="linear" />
</shape>
Loading