Лаба 4

This commit is contained in:
Sergey Karmanov 2024-11-07 17:39:44 +03:00
parent c549bee4e7
commit cc39ffdbe0
10 changed files with 190 additions and 17 deletions

9
.gitattributes vendored Normal file
View File

@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# These are explicitly windows files and should use crlf
* text=auto eol=lf
*.bat text eol=crlf
*.nsi text eol=crlf

View File

@ -40,6 +40,8 @@ dependencies {
implementation libs.lifecycle.viewmodel.ktx implementation libs.lifecycle.viewmodel.ktx
implementation libs.navigation.fragment implementation libs.navigation.fragment
implementation libs.navigation.ui implementation libs.navigation.ui
implementation libs.retrofit2.retrofit
implementation libs.converter.gson
testImplementation libs.junit testImplementation libs.junit
androidTestImplementation libs.ext.junit androidTestImplementation libs.ext.junit
androidTestImplementation libs.espresso.core androidTestImplementation libs.espresso.core

View File

@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"

View File

@ -8,9 +8,12 @@ import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private String[] localDataSet; private List<WeatherData> localDataSet;
public static class ViewHolder extends RecyclerView.ViewHolder { public static class ViewHolder extends RecyclerView.ViewHolder {
@ -29,7 +32,7 @@ public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder
} }
public CustomAdapter(String[] dataSet) { public CustomAdapter(List<WeatherData> dataSet) {
localDataSet = dataSet; localDataSet = dataSet;
} }
@ -49,12 +52,12 @@ public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder
// Get element from your dataset at this position and replace the // Get element from your dataset at this position and replace the
// contents of the view with that element // contents of the view with that element
viewHolder.getTextView().setText(localDataSet[position]); viewHolder.getTextView().setText(localDataSet.get(position).City);
} }
// Return the size of your dataset (invoked by the layout manager) // Return the size of your dataset (invoked by the layout manager)
@Override @Override
public int getItemCount() { public int getItemCount() {
return localDataSet.length; return localDataSet.size();
} }
} }

View File

@ -4,6 +4,7 @@ import android.os.Bundle;
import android.view.View; import android.view.View;
import android.view.Menu; import android.view.Menu;
import com.example.myapplication.ui.home.HomeFragment;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView; import com.google.android.material.navigation.NavigationView;
@ -20,9 +21,9 @@ public class MainActivity2 extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration; private AppBarConfiguration mAppBarConfiguration;
private ActivityMain2Binding binding; private ActivityMain2Binding binding;
private String CurrentCity = "Город ещё не выбран"; private WeatherData CurrentCity = new WeatherData("Город ещё не выбран","", "", "", "");
public String getCurrentCity() { public WeatherData getCurrentCity() {
return CurrentCity; return CurrentCity;
} }
@ -69,7 +70,7 @@ public class MainActivity2 extends AppCompatActivity {
|| super.onSupportNavigateUp(); || super.onSupportNavigateUp();
} }
public void setCurrentCity(String city) { public void setCurrentCity(WeatherData city) {
CurrentCity = city; CurrentCity = city;
NavController nc = Navigation.findNavController(this, R.id.nav_host_fragment_content_main); NavController nc = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
NavigationView nv = findViewById(R.id.nav_view); NavigationView nv = findViewById(R.id.nav_view);

View File

@ -0,0 +1,17 @@
package com.example.myapplication;
public class WeatherData {
public String City;
public String Temperature;
public String WeatherCondition;
public String AtmosphericPressure;
public String Wind;
public WeatherData(String City, String Temperature, String WeatherCondition, String AtmosphericPressure, String Wind) {
this.City = City;
this.Temperature = Temperature;
this.WeatherCondition = WeatherCondition;
this.AtmosphericPressure = AtmosphericPressure;
this.Wind = Wind;
}
}

View File

@ -12,13 +12,13 @@ import androidx.lifecycle.ViewModelProvider;
import com.example.myapplication.MainActivity2; import com.example.myapplication.MainActivity2;
import com.example.myapplication.R; import com.example.myapplication.R;
import com.example.myapplication.WeatherData;
import com.example.myapplication.databinding.FragmentGalleryBinding; import com.example.myapplication.databinding.FragmentGalleryBinding;
public class GalleryFragment extends Fragment { public class GalleryFragment extends Fragment {
private FragmentGalleryBinding binding; private FragmentGalleryBinding binding;
private TextView currentCityView; private TextView currentCityView, currentTemperature, currentPressure, currentWind, currentWeatherStatus;
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) { ViewGroup container, Bundle savedInstanceState) {
@ -30,6 +30,10 @@ public class GalleryFragment extends Fragment {
// Initialize the TextView // Initialize the TextView
currentCityView = root.findViewById(R.id.current_city); currentCityView = root.findViewById(R.id.current_city);
currentPressure = root.findViewById(R.id.pressure_status);
currentTemperature = root.findViewById(R.id.temperature);
currentWind = root.findViewById(R.id.wind);
currentWeatherStatus = root.findViewById(R.id.weather_status);
return root; return root;
} }
@ -39,8 +43,12 @@ public class GalleryFragment extends Fragment {
super.onResume(); super.onResume();
// Update the TextView with the current recipe // Update the TextView with the current recipe
if (getActivity() instanceof MainActivity2) { if (getActivity() instanceof MainActivity2) {
String currentRecipe = ((MainActivity2) getActivity()).getCurrentCity(); WeatherData currentCity = ((MainActivity2) getActivity()).getCurrentCity();
currentCityView.setText(currentRecipe); currentCityView.setText(currentCity.City);
currentTemperature.setText("Температура: " + currentCity.Temperature);
currentPressure.setText("Атмосферное давление: " + currentCity.AtmosphericPressure);
currentWind.setText("Ветер: " + currentCity.Wind);
currentWeatherStatus.setText("Погодные условия: " + currentCity.WeatherCondition );
} }
} }

View File

@ -14,21 +14,36 @@ import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SortedList;
import com.example.myapplication.CustomAdapter; import com.example.myapplication.CustomAdapter;
import com.example.myapplication.MainActivity2; import com.example.myapplication.MainActivity2;
import com.example.myapplication.R; import com.example.myapplication.R;
import com.example.myapplication.RecyclerItemClickListener; import com.example.myapplication.RecyclerItemClickListener;
import com.example.myapplication.WeatherData;
import com.example.myapplication.databinding.FragmentHomeBinding; import com.example.myapplication.databinding.FragmentHomeBinding;
import com.example.myapplication.ui.gallery.GalleryFragment; import com.example.myapplication.ui.gallery.GalleryFragment;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.io.Console; import java.io.Console;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
public class HomeFragment extends Fragment { public class HomeFragment extends Fragment {
private FragmentHomeBinding binding; private FragmentHomeBinding binding;
private WeatherService service;
private ArrayList<WeatherData> weatherDataList = new ArrayList<>();
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) { ViewGroup container, Bundle savedInstanceState) {
@ -38,15 +53,21 @@ public class HomeFragment extends Fragment {
binding = FragmentHomeBinding.inflate(inflater, container, false); binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot(); View root = binding.getRoot();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://raw.githubusercontent.com/Lpirskaya/JsonLab/refs/heads/master/")
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(WeatherService.class);
// String[] city_list = getResources().getStringArray(R.array.city_list);
// Arrays.sort(city_list);
String[] city_list = getResources().getStringArray(R.array.city_list);
Arrays.sort(city_list);
RecyclerView recyclerView = root.findViewById(R.id.suslik); RecyclerView recyclerView = root.findViewById(R.id.suslik);
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(root.getContext(), recyclerView ,new RecyclerItemClickListener.OnItemClickListener() { recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(root.getContext(), recyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) { @Override public void onItemClick(View view, int position) {
Log.e("HomeFragment", "onClick: " + city_list[position]); Log.e("HomeFragment", "onClick: " + weatherDataList.get(position).City);
if (getActivity() instanceof MainActivity2) { if (getActivity() instanceof MainActivity2) {
((MainActivity2) getActivity()).setCurrentCity(city_list[position]); ((MainActivity2) getActivity()).setCurrentCity(weatherDataList.get(position));
} }
} }
@ -56,15 +77,60 @@ public class HomeFragment extends Fragment {
} }
})); }));
recyclerView.setLayoutManager(new LinearLayoutManager(root.getContext())); recyclerView.setLayoutManager(new LinearLayoutManager(root.getContext()));
CustomAdapter adapter = new CustomAdapter(city_list); CustomAdapter adapter = new CustomAdapter(weatherDataList);
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
return root; return root;
} }
public interface WeatherService {
@GET("Weather2022.json")
Call<JsonArray> getWeather2022();
}
@Override @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
binding = null; binding = null;
} }
@Override
public void onResume() {
super.onResume();
service.getWeather2022().enqueue(new Callback<JsonArray>() {
@Override
public void onResponse(Call<JsonArray> call, Response<JsonArray> response) {
if (response.isSuccessful()) {
JsonArray weatherData = response.body();
Log.d("HomeFragment", "Weather data: " + weatherData);
weatherDataList = new ArrayList<>(weatherData.size());
for (int i = 0; i < weatherData.size(); i++) {
weatherDataList.add(new WeatherData(
weatherData.get(i).getAsJsonObject().get("City").getAsString(),
weatherData.get(i).getAsJsonObject().get("Temperature").getAsString(),
weatherData.get(i).getAsJsonObject().get("WeatherCondition").getAsString(),
weatherData.get(i).getAsJsonObject().get("AtmosphericPressure").getAsString(),
weatherData.get(i).getAsJsonObject().get("Wind").getAsString()
));
weatherDataList.sort((o1, o2) -> o1.City.compareTo(o2.City));
// JsonObject item = weatherData.get(i).getAsJsonObject();
// Log.i("HomeFragment", "City: " + item.get("City").getAsString());
// Log.i("HomeFragment", "Temperature: " + item.get("Temperature").getAsInt());
// Log.i("HomeFragment", "WeatherCondition: " + item.get("WeatherCondition").getAsString());
// Log.i("HomeFragment", "AtmosphericPressure: " + item.get("AtmosphericPressure").getAsString());
// Log.i("HomeFragment", "Wind: " + item.get("Wind").getAsString());
// Log.i("HomeFragment", "-------------------");
}
} else
Log.e("HomeFragment", "Request failed: " + response.message());
}
@Override
public void onFailure(Call<JsonArray> call, Throwable t) {
Log.e("HomeFragment", "Network request failed", t);
}
});
}
} }

View File

@ -6,6 +6,7 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".ui.gallery.GalleryFragment"> tools:context=".ui.gallery.GalleryFragment">
<TextView <TextView
android:id="@+id/current_city" android:id="@+id/current_city"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -13,10 +14,72 @@
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:layout_marginEnd="8dp" android:layout_marginEnd="8dp"
android:text="Город"
android:textAlignment="center" android:textAlignment="center"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.175" />
<TextView
android:id="@+id/temperature"
android:layout_width="348dp"
android:layout_height="28dp"
android:layout_marginTop="8dp"
android:text="Температура:"
android:textSize="20sp" android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.492"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.528" />
<TextView
android:id="@+id/wind"
android:layout_width="348dp"
android:layout_height="28dp"
android:layout_marginTop="8dp"
android:text="Ветер:"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.492"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.6" />
<TextView
android:id="@+id/weather_status"
android:layout_width="348dp"
android:layout_height="28dp"
android:layout_marginTop="8dp"
android:text="Погодные условия:"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.492"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.672" />
<TextView
android:id="@+id/pressure_status"
android:layout_width="348dp"
android:layout_height="28dp"
android:layout_marginTop="8dp"
android:text="Атмосферное давление:"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.492"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.739" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -11,8 +11,10 @@ lifecycleLivedataKtx = "2.8.5"
lifecycleViewmodelKtx = "2.8.5" lifecycleViewmodelKtx = "2.8.5"
navigationFragment = "2.6.0" navigationFragment = "2.6.0"
navigationUi = "2.6.0" navigationUi = "2.6.0"
retrofitVersion = "2.9.0"
[libraries] [libraries]
converter-gson = { module = "com.squareup.retrofit2:converter-gson", version.ref = "retrofitVersion" }
junit = { group = "junit", name = "junit", version.ref = "junit" } junit = { group = "junit", name = "junit", version.ref = "junit" }
ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" } ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" } espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
@ -24,6 +26,7 @@ lifecycle-livedata-ktx = { group = "androidx.lifecycle", name = "lifecycle-lived
lifecycle-viewmodel-ktx = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-ktx", version.ref = "lifecycleViewmodelKtx" } lifecycle-viewmodel-ktx = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-ktx", version.ref = "lifecycleViewmodelKtx" }
navigation-fragment = { group = "androidx.navigation", name = "navigation-fragment", version.ref = "navigationFragment" } navigation-fragment = { group = "androidx.navigation", name = "navigation-fragment", version.ref = "navigationFragment" }
navigation-ui = { group = "androidx.navigation", name = "navigation-ui", version.ref = "navigationUi" } navigation-ui = { group = "androidx.navigation", name = "navigation-ui", version.ref = "navigationUi" }
retrofit2-retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofitVersion" }
[plugins] [plugins]
android-application = { id = "com.android.application", version.ref = "agp" } android-application = { id = "com.android.application", version.ref = "agp" }