8 лаба
This commit is contained in:
parent
340b4193de
commit
98c89f7b36
@ -21,7 +21,7 @@ public interface CityDao {
|
|||||||
List<City> getAllCity();
|
List<City> getAllCity();
|
||||||
|
|
||||||
@Query("SELECT * FROM city_table WHERE id = :cityId")
|
@Query("SELECT * FROM city_table WHERE id = :cityId")
|
||||||
City getCityById(int cityId);
|
City getCityById(long cityId);
|
||||||
|
|
||||||
@Query("SELECT COUNT(*) FROM city_table")
|
@Query("SELECT COUNT(*) FROM city_table")
|
||||||
int countCity();
|
int countCity();
|
||||||
|
@ -24,4 +24,7 @@ public interface FavoriteCityDao {
|
|||||||
|
|
||||||
@Query("SELECT * FROM favorite_cities_table WHERE owner_id = :userId AND city_id = :cityId")
|
@Query("SELECT * FROM favorite_cities_table WHERE owner_id = :userId AND city_id = :cityId")
|
||||||
int isFavorite(long userId, long cityId);
|
int isFavorite(long userId, long cityId);
|
||||||
|
|
||||||
|
@Query("DELETE FROM favorite_cities_table WHERE owner_id = :userId AND city_id = :cityId")
|
||||||
|
void removeFavorite(long userId, long cityId);
|
||||||
}
|
}
|
||||||
|
@ -18,12 +18,16 @@ import androidx.appcompat.app.AppCompatActivity;
|
|||||||
|
|
||||||
import com.example.myapplication.databinding.ActivityMain2Binding;
|
import com.example.myapplication.databinding.ActivityMain2Binding;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class MainActivity2 extends AppCompatActivity {
|
public class MainActivity2 extends AppCompatActivity {
|
||||||
|
|
||||||
private AppBarConfiguration mAppBarConfiguration;
|
private AppBarConfiguration mAppBarConfiguration;
|
||||||
private ActivityMain2Binding binding;
|
private ActivityMain2Binding binding;
|
||||||
private WeatherData CurrentCity = new WeatherData("Город ещё не выбран","", "", "", "");
|
private WeatherData CurrentCity = new WeatherData("Город ещё не выбран","", "", "", "");
|
||||||
|
|
||||||
|
public static ArrayList<WeatherData> weatherDataList = new ArrayList<>();
|
||||||
|
|
||||||
private String userName = "";
|
private String userName = "";
|
||||||
|
|
||||||
public String getUserName() {
|
public String getUserName() {
|
||||||
|
@ -4,6 +4,7 @@ import android.app.AlertDialog;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.Menu;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
@ -11,6 +12,9 @@ import android.widget.Toast;
|
|||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
import androidx.navigation.NavController;
|
||||||
|
import androidx.navigation.Navigation;
|
||||||
|
import androidx.navigation.ui.NavigationUI;
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
@ -27,6 +31,7 @@ import com.example.myapplication.R;
|
|||||||
import com.example.myapplication.RecyclerItemClickListener;
|
import com.example.myapplication.RecyclerItemClickListener;
|
||||||
import com.example.myapplication.DTO.WeatherData;
|
import com.example.myapplication.DTO.WeatherData;
|
||||||
import com.example.myapplication.databinding.FragmentHomeBinding;
|
import com.example.myapplication.databinding.FragmentHomeBinding;
|
||||||
|
import com.google.android.material.navigation.NavigationView;
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -44,9 +49,7 @@ public class HomeFragment extends Fragment {
|
|||||||
|
|
||||||
private FragmentHomeBinding binding;
|
private FragmentHomeBinding binding;
|
||||||
private WeatherService service;
|
private WeatherService service;
|
||||||
private ArrayList<WeatherData> weatherDataList = new ArrayList<>();
|
|
||||||
private List<City> cityList = new ArrayList<City>();
|
private List<City> cityList = new ArrayList<City>();
|
||||||
|
|
||||||
private RecyclerView recyclerView;
|
private RecyclerView recyclerView;
|
||||||
|
|
||||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||||
@ -60,15 +63,15 @@ public class HomeFragment extends Fragment {
|
|||||||
recyclerView = root.findViewById(R.id.suslik);
|
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: " + weatherDataList.get(position).City);
|
|
||||||
if (getActivity() instanceof MainActivity2) {
|
if (getActivity() instanceof MainActivity2) {
|
||||||
((MainActivity2) getActivity()).setCurrentCity(weatherDataList.get(position));
|
Log.e("HomeFragment", "onClick: " + MainActivity2.weatherDataList.get(position).City);
|
||||||
|
((MainActivity2) getActivity()).setCurrentCity(MainActivity2.weatherDataList.get(position));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLongItemClick(View view, int position) {
|
public void onLongItemClick(View view, int position) {
|
||||||
String selectedCity = weatherDataList.get(position).City;
|
String selectedCity = MainActivity2.weatherDataList.get(position).City;
|
||||||
showDialog(selectedCity);
|
showDialog(selectedCity);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
@ -143,7 +146,6 @@ public class HomeFragment extends Fragment {
|
|||||||
binding = null;
|
binding = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
@ -153,9 +155,9 @@ public class HomeFragment extends Fragment {
|
|||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
JsonArray weatherData = response.body();
|
JsonArray weatherData = response.body();
|
||||||
Log.d("HomeFragment", "Weather data: " + weatherData);
|
Log.d("HomeFragment", "Weather data: " + weatherData);
|
||||||
weatherDataList = new ArrayList<>(weatherData.size());
|
MainActivity2.weatherDataList = new ArrayList<>(weatherData.size());
|
||||||
for (int i = 0; i < weatherData.size(); i++) {
|
for (int i = 0; i < weatherData.size(); i++) {
|
||||||
weatherDataList.add(new WeatherData(
|
MainActivity2.weatherDataList.add(new WeatherData(
|
||||||
weatherData.get(i).getAsJsonObject().get("City").getAsString(),
|
weatherData.get(i).getAsJsonObject().get("City").getAsString(),
|
||||||
weatherData.get(i).getAsJsonObject().get("Temperature").getAsString(),
|
weatherData.get(i).getAsJsonObject().get("Temperature").getAsString(),
|
||||||
weatherData.get(i).getAsJsonObject().get("WeatherCondition").getAsString(),
|
weatherData.get(i).getAsJsonObject().get("WeatherCondition").getAsString(),
|
||||||
@ -163,7 +165,7 @@ public class HomeFragment extends Fragment {
|
|||||||
weatherData.get(i).getAsJsonObject().get("Wind").getAsString()
|
weatherData.get(i).getAsJsonObject().get("Wind").getAsString()
|
||||||
));
|
));
|
||||||
|
|
||||||
weatherDataList.sort((o1, o2) -> o1.City.compareTo(o2.City));
|
MainActivity2.weatherDataList.sort((o1, o2) -> o1.City.compareTo(o2.City));
|
||||||
|
|
||||||
// JsonObject item = weatherData.get(i).getAsJsonObject();
|
// JsonObject item = weatherData.get(i).getAsJsonObject();
|
||||||
// Log.i("HomeFragment", "City: " + item.get("City").getAsString());
|
// Log.i("HomeFragment", "City: " + item.get("City").getAsString());
|
||||||
@ -178,7 +180,7 @@ public class HomeFragment extends Fragment {
|
|||||||
CityDao cityDao = db.cityDao();
|
CityDao cityDao = db.cityDao();
|
||||||
Executors.newSingleThreadExecutor().execute(() -> {
|
Executors.newSingleThreadExecutor().execute(() -> {
|
||||||
if (cityDao.countCity() == 0) {
|
if (cityDao.countCity() == 0) {
|
||||||
for (WeatherData wd : weatherDataList) {
|
for (WeatherData wd : MainActivity2.weatherDataList) {
|
||||||
Log.i("HomeFragment", "Inserting city: " + wd.City);
|
Log.i("HomeFragment", "Inserting city: " + wd.City);
|
||||||
cityDao.insert(new City(wd.City));
|
cityDao.insert(new City(wd.City));
|
||||||
}
|
}
|
||||||
|
@ -1,37 +1,148 @@
|
|||||||
package com.example.myapplication.ui.slideshow;
|
package com.example.myapplication.ui.slideshow;
|
||||||
|
|
||||||
|
import android.app.AlertDialog;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
|
import com.example.myapplication.AppDatabase;
|
||||||
|
import com.example.myapplication.CustomAdapter;
|
||||||
|
import com.example.myapplication.DAO.CityDao;
|
||||||
|
import com.example.myapplication.DAO.FavoriteCityDao;
|
||||||
|
import com.example.myapplication.DAO.UserDao;
|
||||||
|
import com.example.myapplication.DTO.City;
|
||||||
|
import com.example.myapplication.DTO.FavoriteCity;
|
||||||
|
import com.example.myapplication.DTO.User;
|
||||||
|
import com.example.myapplication.MainActivity2;
|
||||||
|
import com.example.myapplication.R;
|
||||||
|
import com.example.myapplication.RecyclerItemClickListener;
|
||||||
import com.example.myapplication.databinding.FragmentSlideshowBinding;
|
import com.example.myapplication.databinding.FragmentSlideshowBinding;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
public class SlideshowFragment extends Fragment {
|
public class SlideshowFragment extends Fragment {
|
||||||
|
|
||||||
private FragmentSlideshowBinding binding;
|
private FragmentSlideshowBinding binding;
|
||||||
|
private RecyclerView recyclerView;
|
||||||
|
private List<City> cityList = new ArrayList<City>();
|
||||||
|
|
||||||
|
private AppDatabase db;
|
||||||
|
|
||||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||||
ViewGroup container, Bundle savedInstanceState) {
|
ViewGroup container, Bundle savedInstanceState) {
|
||||||
SlideshowViewModel slideshowViewModel =
|
|
||||||
new ViewModelProvider(this).get(SlideshowViewModel.class);
|
|
||||||
|
|
||||||
binding = FragmentSlideshowBinding.inflate(inflater, container, false);
|
binding = FragmentSlideshowBinding.inflate(inflater, container, false);
|
||||||
View root = binding.getRoot();
|
View root = binding.getRoot();
|
||||||
|
|
||||||
final TextView textView = binding.textSlideshow;
|
db = AppDatabase.getDatabase(getContext());
|
||||||
slideshowViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
|
|
||||||
|
recyclerView = root.findViewById(R.id.fav_cyclik);
|
||||||
|
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(root.getContext(), recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onItemClick(View view, int position) {
|
||||||
|
Log.e("SlideshowFragment", "onClick: " + cityList.get(position).CityName);
|
||||||
|
if (getActivity() instanceof MainActivity2) {
|
||||||
|
((MainActivity2) getActivity()).setCurrentCity(MainActivity2.weatherDataList.get(position));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLongItemClick(View view, int position) {
|
||||||
|
String selectedCity = cityList.get(position).CityName;
|
||||||
|
showDialog(selectedCity);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
recyclerView.setLayoutManager(new LinearLayoutManager(root.getContext()));
|
||||||
|
CustomAdapter adapter = new CustomAdapter(cityList);
|
||||||
|
recyclerView.setAdapter(adapter);
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void showDialog(String city) {
|
||||||
|
new AlertDialog.Builder(getContext())
|
||||||
|
.setTitle("Выбор города")
|
||||||
|
.setMessage("Удалить " + city + " из избранного?")
|
||||||
|
.setPositiveButton("Удалить", (dialog, which) -> {
|
||||||
|
if (getActivity() instanceof MainActivity2) {
|
||||||
|
String username = ((MainActivity2) getActivity()).getUserName();
|
||||||
|
|
||||||
|
Executors.newSingleThreadExecutor().execute(() -> {
|
||||||
|
CityDao cityDao = db.cityDao();
|
||||||
|
UserDao userDao = db.userDao();
|
||||||
|
FavoriteCityDao favoriteCityDao = db.favoriteCityDao();
|
||||||
|
City cityObj = cityDao.getCityByName(city);
|
||||||
|
User userObj = userDao.getUserByName(username);
|
||||||
|
|
||||||
|
// check if city is already in favorites
|
||||||
|
if (userObj != null && cityObj != null) {
|
||||||
|
if (favoriteCityDao.isFavorite(userObj.getId(), cityObj.getId()) > 0) {
|
||||||
|
favoriteCityDao.removeFavorite(userObj.getId(), cityObj.getId());
|
||||||
|
getActivity().runOnUiThread(() -> {
|
||||||
|
Toast.makeText(getContext(), city + " удалён из избранного", Toast.LENGTH_SHORT).show();
|
||||||
|
updateData();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.e("HomeFragment", "User or city not found");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Log.e("SlideshowFragment", "Ошибка получения контекста");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.setNegativeButton("Отмена", null)
|
||||||
|
.show();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroyView() {
|
public void onDestroyView() {
|
||||||
super.onDestroyView();
|
super.onDestroyView();
|
||||||
binding = null;
|
binding = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
updateData();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateData() {
|
||||||
|
Executors.newSingleThreadExecutor().execute(() -> {
|
||||||
|
if (getActivity() instanceof MainActivity2) {
|
||||||
|
String username = ((MainActivity2) getActivity()).getUserName();
|
||||||
|
|
||||||
|
CityDao cityDao = db.cityDao();
|
||||||
|
UserDao userDao = db.userDao();
|
||||||
|
FavoriteCityDao favoriteCityDao = db.favoriteCityDao();
|
||||||
|
|
||||||
|
User userObj = userDao.getUserByName(username);
|
||||||
|
|
||||||
|
List<FavoriteCity> favCity = favoriteCityDao.getFavoriteCityByOwnerId(userObj.getId());
|
||||||
|
cityList.clear();
|
||||||
|
|
||||||
|
for (FavoriteCity favoriteCity : favCity) {
|
||||||
|
City city = cityDao.getCityById(favoriteCity.getCityId());
|
||||||
|
cityList.add(city);
|
||||||
|
}
|
||||||
|
getActivity().runOnUiThread(() -> {
|
||||||
|
recyclerView.getAdapter().notifyDataSetChanged();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
@ -4,19 +4,12 @@
|
|||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context=".ui.slideshow.SlideshowFragment">
|
tools:context=".ui.home.HomeFragment">
|
||||||
|
|
||||||
<TextView
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
android:id="@+id/text_slideshow"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="match_parent"
|
||||||
android:layout_marginStart="8dp"
|
android:id="@+id/fav_cyclik">
|
||||||
android:layout_marginTop="8dp"
|
|
||||||
android:layout_marginEnd="8dp"
|
</androidx.recyclerview.widget.RecyclerView>
|
||||||
android:textAlignment="center"
|
|
||||||
android:textSize="20sp"
|
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
x
Reference in New Issue
Block a user