-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
392 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
app/src/main/java/com/jjewuz/justweather/FutureWeather.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package com.jjewuz.justweather | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import android.os.Bundle | ||
import android.util.Log | ||
import androidx.fragment.app.Fragment | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.core.content.res.ResourcesCompat | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import androidx.recyclerview.widget.RecyclerView | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import org.json.JSONObject | ||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
import java.util.Locale | ||
|
||
class FutureWeather : Fragment() { | ||
private lateinit var recyclerView: RecyclerView | ||
private lateinit var weatherAdapter: WeatherAdapter | ||
|
||
private val client = OkHttpClient() | ||
private val apiKey = BuildConfig.API_KEY | ||
private lateinit var sharedPreferences: SharedPreferences | ||
private lateinit var measurment: String | ||
private lateinit var measureTxt: String | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
val rootView = inflater.inflate(R.layout.fragment_future_weather, container, false) | ||
|
||
recyclerView = rootView.findViewById(R.id.recyclerView) | ||
weatherAdapter = WeatherAdapter() | ||
|
||
sharedPreferences = requireActivity().getSharedPreferences("prefs", Context.MODE_PRIVATE) | ||
|
||
measurment = sharedPreferences.getString("measure", "metric").toString() | ||
measureTxt = if (measurment == "metric") { | ||
"°С" | ||
} else { | ||
"°F" | ||
} | ||
|
||
recyclerView.apply { | ||
layoutManager = LinearLayoutManager(context) | ||
adapter = weatherAdapter | ||
} | ||
|
||
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.Main) { | ||
val weatherDataList = mutableListOf<WeatherData>() | ||
val request = Request.Builder().url(getUrl()).build() | ||
try { | ||
val response = withContext(Dispatchers.IO) { | ||
client.newCall(request).execute() | ||
} | ||
val responseBody: String? = response.body?.string() | ||
if (response.isSuccessful && !responseBody.isNullOrEmpty()) { | ||
val jsonObject = JSONObject(responseBody) | ||
val jsonArray = jsonObject.getJSONArray("list") | ||
|
||
val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) | ||
|
||
for (i in 0 until jsonArray.length()) { | ||
val item = jsonArray.getJSONObject(i) | ||
val dt = item.getInt("dt") | ||
val main = item.getJSONObject("main") | ||
val temp = main.getInt("temp") | ||
val weatherArray = item.getJSONArray("weather") | ||
val weatherObject = weatherArray.getJSONObject(0) | ||
val icon = weatherObject.getInt("id") | ||
val description = weatherObject.getString("description") | ||
|
||
val data: String = if (measurment == "metric"){ | ||
SimpleDateFormat("dd.MM.yyyy HH:mm", Locale.getDefault()).format(Date(dt.toLong() * 1000)) | ||
} else{ | ||
SimpleDateFormat("yyyy-MM-dd hh:mm a", Locale.getDefault()).format(Date(dt.toLong() * 1000)) | ||
} | ||
|
||
val image: Int | ||
if (icon == 800){ | ||
image = R.drawable.weather | ||
} else if (icon / 100 == 8){ | ||
image = R.drawable.cloud_weather | ||
} else if (icon / 100 == 7){ | ||
image = R.drawable.mist | ||
} else if (icon / 100 == 6){ | ||
image = R.drawable.snow | ||
} else if (icon / 100 == 5){ | ||
image = R.drawable.rainy | ||
} else if (icon == 300){ | ||
image = R.drawable.cloud | ||
} else if (icon / 100 == 3){ | ||
image = R.drawable.drizzle | ||
} else if (icon / 100 == 2){ | ||
image = R.drawable.thunderstorm | ||
} else { | ||
image = R.drawable.buttonc | ||
} | ||
|
||
val weather = WeatherData(data, description, temp.toString() + measureTxt, image) | ||
weatherDataList.add(weather) | ||
} | ||
} | ||
weatherAdapter.setData(weatherDataList) | ||
response.body?.close() | ||
} catch (e: Exception) { | ||
|
||
} | ||
} | ||
|
||
|
||
|
||
return rootView | ||
} | ||
|
||
private fun getLat(): String? { | ||
return sharedPreferences.getString("citylat", "0.0") | ||
} | ||
|
||
private fun getLon(): String? { | ||
return sharedPreferences.getString("citylon", "0.0") | ||
} | ||
|
||
fun getUrl(): String{ | ||
val lang = Locale.getDefault().language | ||
val url = "https://api.openweathermap.org/data/2.5/forecast?lat=${getLat()}&lon=${getLon()}&appid=$apiKey&lang=$lang&units=$measurment&cnt=15" | ||
return url | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/jjewuz/justweather/WeatherAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.jjewuz.justweather | ||
|
||
import android.media.Image | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.ImageView | ||
import android.widget.TextView | ||
import androidx.recyclerview.widget.RecyclerView | ||
|
||
class WeatherAdapter : RecyclerView.Adapter<WeatherAdapter.WeatherViewHolder>() { | ||
|
||
private var weatherDataList: List<WeatherData> = emptyList() | ||
|
||
fun setData(data: List<WeatherData>) { | ||
weatherDataList = data | ||
notifyDataSetChanged() | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WeatherViewHolder { | ||
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_weather, parent, false) | ||
return WeatherViewHolder(view) | ||
} | ||
|
||
override fun onBindViewHolder(holder: WeatherViewHolder, position: Int) { | ||
val weatherData = weatherDataList[position] | ||
holder.bind(weatherData) | ||
} | ||
|
||
override fun getItemCount(): Int = weatherDataList.size | ||
|
||
inner class WeatherViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
private val dayTextView: TextView = itemView.findViewById(R.id.dayTextView) | ||
private val conditionTextView: TextView = itemView.findViewById(R.id.conditionTextView) | ||
private val temperatureTextView: TextView = itemView.findViewById(R.id.temperatureTextView) | ||
private val imageView: ImageView = itemView.findViewById(R.id.image) | ||
|
||
fun bind(weatherData: WeatherData) { | ||
dayTextView.text = weatherData.day | ||
conditionTextView.text = weatherData.condition | ||
temperatureTextView.text = weatherData.temperature | ||
imageView.setImageResource(weatherData.image) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.jjewuz.justweather | ||
|
||
import android.media.Image | ||
import android.widget.ImageView | ||
|
||
data class WeatherData( | ||
val day: String, | ||
val condition: String, | ||
val temperature: String, | ||
val image: Int | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="48dp" | ||
android:height="48dp" | ||
android:viewportWidth="960" | ||
android:viewportHeight="960"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M251,800q-88,0 -149.5,-61.5T40,589q0,-78 50,-137t127,-71q20,-97 94,-158.5T482,161q112,0 189,81.5T748,438v24q72,-2 122,46.5T920,631q0,69 -50,119t-119,50L251,800ZM251,740h500q45,0 77,-32t32,-77q0,-45 -32,-77t-77,-32h-63v-84q0,-91 -61,-154t-149,-63q-88,0 -149.5,63T267,438h-19q-62,0 -105,43.5T100,589q0,63 44,107t107,44ZM480,480Z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="48dp" | ||
android:height="48dp" | ||
android:viewportWidth="960" | ||
android:viewportHeight="960"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M332.98,746.87Q322,753 310,748.5q-12,-4.5 -17,-15.5l-60,-120q-5,-11 -1.5,-23t14.5,-17q11,-5 23.5,-1.5T287,586l60,120q5,11 0.98,22.87 -4.03,11.87 -15,18ZM692.98,746.87Q682,753 670,748.5q-12,-4.5 -17,-15.5L473,373q-5,-11 -1.5,-23t14.5,-17q11,-5 23.5,-1.5T527,346l180,360q5,11 0.98,22.87 -4.03,11.87 -15,18ZM509.83,750Q497,750 488.5,741.33q-8.5,-8.68 -8.5,-21.5 0,-12.82 8.68,-21.33 8.68,-8.5 21.5,-8.5 12.82,0 21.33,8.68 8.5,8.68 8.5,21.5 0,12.82 -8.68,21.33 -8.68,8.5 -21.5,8.5ZM892.98,746.87Q882,753 870,748.5q-12,-4.5 -17,-15.5l-20,-40q-5,-11 -1.5,-23t14.5,-17q11,-5 23.5,-1.5T887,666l20,40q5,11 0.98,22.87 -4.03,11.87 -15,18ZM452.98,626.87Q442,633 430,629q-12,-4 -17,-15L233,254q-5,-11 -1.5,-23.5T246,213q11,-5 23.5,-1.5T287,226l180,360q5,11 0.98,22.87 -4.03,11.87 -15,18ZM799.83,590Q787,590 778.5,581.33q-8.5,-8.68 -8.5,-21.5 0,-12.82 8.68,-21.33 8.68,-8.5 21.5,-8.5 12.82,0 21.33,8.68 8.5,8.68 8.5,21.5 0,12.82 -8.68,21.33 -8.68,8.5 -21.5,8.5ZM199.83,510Q187,510 178.5,501.33q-8.5,-8.68 -8.5,-21.5 0,-12.82 8.68,-21.33 8.68,-8.5 21.5,-8.5 12.82,0 21.33,8.68 8.5,8.68 8.5,21.5 0,12.82 -8.68,21.33 -8.68,8.5 -21.5,8.5ZM752.98,466.87Q742,473 730.5,468.5 719,464 713,453L613,253q-5,-11 -1.5,-23t14.5,-17q11,-5 23.5,-1.5T667,226l100,200q5,11 0.98,22.87 -4.03,11.87 -15,18ZM152.98,386.87Q142,393 130,389q-12,-4 -17,-15L53,254q-5,-11 -1.5,-23.5T66,213q11,-5 23.5,-1.5T107,226l60,120q5,11 0.98,22.87 -4.03,11.87 -15,18ZM449.83,270Q437,270 428.5,261.33q-8.5,-8.68 -8.5,-21.5 0,-12.82 8.68,-21.33 8.68,-8.5 21.5,-8.5 12.82,0 21.33,8.68 8.5,8.68 8.5,21.5 0,12.82 -8.68,21.33 -8.68,8.5 -21.5,8.5Z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="48dp" | ||
android:height="48dp" | ||
android:viewportWidth="960" | ||
android:viewportHeight="960"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M180,880q-24,0 -42,-18t-18,-42v-620q0,-24 18,-42t42,-18h65v-60h65v60h340v-60h65v60h65q24,0 42,18t18,42v620q0,24 -18,42t-42,18L180,880ZM180,820h600v-430L180,390v430ZM180,330h600v-130L180,200v130ZM180,330v-130,130Z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="48dp" | ||
android:height="48dp" | ||
android:viewportWidth="960" | ||
android:viewportHeight="960"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M160,750q-12.75,0 -21.38,-8.68 -8.63,-8.68 -8.63,-21.5 0,-12.82 8.63,-21.33T160,690h400q12.75,0 21.38,8.68 8.63,8.68 8.63,21.5 0,12.82 -8.63,21.33T560,750L160,750ZM720,750q-12.75,0 -21.38,-8.68 -8.63,-8.68 -8.63,-21.5 0,-12.82 8.63,-21.33T720,690h80q12.75,0 21.38,8.68 8.63,8.68 8.63,21.5 0,12.82 -8.63,21.33T800,750h-80ZM160,590q-12.75,0 -21.38,-8.68 -8.63,-8.68 -8.63,-21.5 0,-12.82 8.63,-21.33T160,530h80q12.75,0 21.38,8.68 8.63,8.68 8.63,21.5 0,12.82 -8.63,21.33T240,590h-80ZM400,590q-12.75,0 -21.38,-8.68 -8.63,-8.68 -8.63,-21.5 0,-12.82 8.63,-21.33T400,530h400q12.75,0 21.38,8.68 8.63,8.68 8.63,21.5 0,12.82 -8.63,21.33T800,590L400,590ZM160,430q-12.75,0 -21.38,-8.68 -8.63,-8.68 -8.63,-21.5 0,-12.82 8.63,-21.33T160,370h440q12.75,0 21.38,8.68 8.63,8.68 8.63,21.5 0,12.82 -8.63,21.33T600,430L160,430ZM760,430q-12.75,0 -21.38,-8.68 -8.63,-8.68 -8.63,-21.5 0,-12.82 8.63,-21.33T760,370h40q12.75,0 21.38,8.68 8.63,8.68 8.63,21.5 0,12.82 -8.63,21.33T800,430h-40ZM160,270q-12.75,0 -21.38,-8.68 -8.63,-8.68 -8.63,-21.5 0,-12.82 8.63,-21.33T160,210h200q12.75,0 21.38,8.68 8.63,8.68 8.63,21.5 0,12.82 -8.63,21.33T360,270L160,270ZM520,270q-12.75,0 -21.38,-8.68 -8.63,-8.68 -8.63,-21.5 0,-12.82 8.63,-21.33T520,210h280q12.75,0 21.38,8.68 8.63,8.68 8.63,21.5 0,12.82 -8.63,21.33T800,270L520,270Z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="48dp" | ||
android:height="48dp" | ||
android:viewportWidth="960" | ||
android:viewportHeight="960"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M558,877q-11,5 -23.5,1T517,863l-69,-138q-5,-11 -1.5,-23.5T461,684q11,-5 23.5,-1t17.5,15l69,138q5,11 1.5,23.5T558,877ZM798,876q-11,5 -23.5,1T757,862l-69,-138q-5,-11 -1.5,-23.5T701,683q11,-5 23.5,-1t17.5,15l69,138q5,11 1.5,23.5T798,876ZM318,876q-11,5 -23.5,1.5T277,863l-69,-138q-5,-11 -1,-23.5t15,-17.5q11,-5 23.5,-1.5T263,697l69,139q5,11 1,23t-15,17ZM290,620q-87,0 -148.5,-61.5T80,410q0,-79 56.5,-141T277,201q32,-56 84.5,-88.5T480,80q91,0 152.5,57.5T708,280q79,4 125.5,53.5T880,450q0,70 -49.5,120T710,620L290,620ZM290,560h420q46,0 78,-32.5t32,-77.5q0,-46 -32,-78t-78,-32h-60v-30q0,-71 -49.5,-120.5T480,140q-51,0 -93.5,27.5T324,242l-8,18h-28q-62,2 -105,45.5T140,410q0,62 44,106t106,44ZM480,350Z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="48dp" | ||
android:height="48dp" | ||
android:viewportWidth="960" | ||
android:viewportHeight="960"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M240,760q-14,0 -24.5,-10.5T205,725q0,-14 10.5,-24.5T240,690q14,0 24.5,10.5T275,725q0,14 -10.5,24.5T240,760ZM720,760q-14,0 -24.5,-10.5T685,725q0,-14 10.5,-24.5T720,690q14,0 24.5,10.5T755,725q0,14 -10.5,24.5T720,760ZM360,920q-14,0 -24.5,-10.5T325,885q0,-14 10.5,-24.5T360,850q14,0 24.5,10.5T395,885q0,14 -10.5,24.5T360,920ZM480,760q-14,0 -24.5,-10.5T445,725q0,-14 10.5,-24.5T480,690q14,0 24.5,10.5T515,725q0,14 -10.5,24.5T480,760ZM600,920q-14,0 -24.5,-10.5T565,885q0,-14 10.5,-24.5T600,850q14,0 24.5,10.5T635,885q0,14 -10.5,24.5T600,920ZM290,620q-86.86,0 -148.43,-61.52Q80,496.96 80,410.16 80,331 136.5,269 193,207 277,201q32,-56 84.5,-88.5T480.42,80Q571,80 632.5,137.5T708,280q79,4 125.5,53.5T880,449.62Q880,520 830.42,570 780.83,620 710,620L290,620ZM290,560h420q46.2,0 78.1,-32.5 31.9,-32.5 31.9,-78T788.1,372q-31.9,-32 -78.1,-32h-60v-30q0,-71 -49.5,-120.5T480.21,140q-51.48,0 -93.85,27.5Q344,195 324,242l-8,18h-28q-62,2 -105,45.39t-43,104.46Q140,472 183.93,516 227.86,560 290,560ZM480,350Z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="48dp" | ||
android:height="48dp" | ||
android:viewportWidth="960" | ||
android:viewportHeight="960"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="m475,960 l97,-110 -80,-40 113,-130h80l-97,110 80,40L555,960h-80ZM235,960l97,-110 -80,-40 113,-130h80l-97,110 80,40L315,960h-80ZM290,620q-86.86,0 -148.43,-61.52Q80,496.96 80,410.16 80,331 136.5,269 193,207 277,201q32,-56 84.5,-88.5T480.42,80Q571,80 632.5,137.5T708,280q79,4 125.5,53.5T880,449.62Q880,520 830.42,570 780.83,620 710,620L290,620ZM290,560h420q46.2,0 78.1,-32.5 31.9,-32.5 31.9,-78T788.1,372q-31.9,-32 -78.1,-32h-60v-30q0,-71 -49.5,-120.5T480.21,140q-51.48,0 -93.85,27.5Q344,195 324,242l-8,18h-28q-62,2 -105,45.39t-43,104.46Q140,472 183.93,516 227.86,560 290,560ZM480,350Z"/> | ||
</vector> |
Oops, something went wrong.