8 лаба
This commit is contained in:
parent
340b4193de
commit
98c89f7b36
@ -21,7 +21,7 @@ public interface CityDao {
|
||||
List<City> getAllCity();
|
||||
|
||||
@Query("SELECT * FROM city_table WHERE id = :cityId")
|
||||
City getCityById(int cityId);
|
||||
City getCityById(long cityId);
|
||||
|
||||
@Query("SELECT COUNT(*) FROM city_table")
|
||||
int countCity();
|
||||
|
@ -24,4 +24,7 @@ public interface FavoriteCityDao {
|
||||
|
||||
@Query("SELECT * FROM favorite_cities_table WHERE owner_id = :userId AND city_id = :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 java.util.ArrayList;
|
||||
|
||||
public class MainActivity2 extends AppCompatActivity {
|
||||
|
||||
private AppBarConfiguration mAppBarConfiguration;
|
||||
private ActivityMain2Binding binding;
|
||||
private WeatherData CurrentCity = new WeatherData("Город ещё не выбран","", "", "", "");
|
||||
|
||||
public static ArrayList<WeatherData> weatherDataList = new ArrayList<>();
|
||||
|
||||
private String userName = "";
|
||||
|
||||
public String getUserName() {
|
||||
|
@ -4,6 +4,7 @@ import android.app.AlertDialog;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Toast;
|
||||
@ -11,6 +12,9 @@ import android.widget.Toast;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.Fragment;
|
||||
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.RecyclerView;
|
||||
|
||||
@ -27,6 +31,7 @@ import com.example.myapplication.R;
|
||||
import com.example.myapplication.RecyclerItemClickListener;
|
||||
import com.example.myapplication.DTO.WeatherData;
|
||||
import com.example.myapplication.databinding.FragmentHomeBinding;
|
||||
import com.google.android.material.navigation.NavigationView;
|
||||
import com.google.gson.JsonArray;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -44,9 +49,7 @@ public class HomeFragment extends Fragment {
|
||||
|
||||
private FragmentHomeBinding binding;
|
||||
private WeatherService service;
|
||||
private ArrayList<WeatherData> weatherDataList = new ArrayList<>();
|
||||
private List<City> cityList = new ArrayList<City>();
|
||||
|
||||
private RecyclerView recyclerView;
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
@ -60,15 +63,15 @@ public class HomeFragment extends Fragment {
|
||||
recyclerView = root.findViewById(R.id.suslik);
|
||||
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(root.getContext(), recyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
|
||||
@Override public void onItemClick(View view, int position) {
|
||||
Log.e("HomeFragment", "onClick: " + weatherDataList.get(position).City);
|
||||
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
|
||||
public void onLongItemClick(View view, int position) {
|
||||
String selectedCity = weatherDataList.get(position).City;
|
||||
String selectedCity = MainActivity2.weatherDataList.get(position).City;
|
||||
showDialog(selectedCity);
|
||||
}
|
||||
}));
|
||||
@ -143,7 +146,6 @@ public class HomeFragment extends Fragment {
|
||||
binding = null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
@ -153,9 +155,9 @@ public class HomeFragment extends Fragment {
|
||||
if (response.isSuccessful()) {
|
||||
JsonArray weatherData = response.body();
|
||||
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++) {
|
||||
weatherDataList.add(new WeatherData(
|
||||
MainActivity2.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(),
|
||||
@ -163,7 +165,7 @@ public class HomeFragment extends Fragment {
|
||||
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();
|
||||
// Log.i("HomeFragment", "City: " + item.get("City").getAsString());
|
||||
@ -178,7 +180,7 @@ public class HomeFragment extends Fragment {
|
||||
CityDao cityDao = db.cityDao();
|
||||
Executors.newSingleThreadExecutor().execute(() -> {
|
||||
if (cityDao.countCity() == 0) {
|
||||
for (WeatherData wd : weatherDataList) {
|
||||
for (WeatherData wd : MainActivity2.weatherDataList) {
|
||||
Log.i("HomeFragment", "Inserting city: " + wd.City);
|
||||
cityDao.insert(new City(wd.City));
|
||||
}
|
||||
|
@ -1,37 +1,148 @@
|
||||
package com.example.myapplication.ui.slideshow;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.Fragment;
|
||||
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 java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class SlideshowFragment extends Fragment {
|
||||
|
||||
private FragmentSlideshowBinding binding;
|
||||
private RecyclerView recyclerView;
|
||||
private List<City> cityList = new ArrayList<City>();
|
||||
|
||||
private AppDatabase db;
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
ViewGroup container, Bundle savedInstanceState) {
|
||||
SlideshowViewModel slideshowViewModel =
|
||||
new ViewModelProvider(this).get(SlideshowViewModel.class);
|
||||
|
||||
binding = FragmentSlideshowBinding.inflate(inflater, container, false);
|
||||
View root = binding.getRoot();
|
||||
|
||||
final TextView textView = binding.textSlideshow;
|
||||
slideshowViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
|
||||
db = AppDatabase.getDatabase(getContext());
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
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"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ui.slideshow.SlideshowFragment">
|
||||
tools:context=".ui.home.HomeFragment">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text_slideshow"
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
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" />
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/fav_cyclik">
|
||||
|
||||
</androidx.recyclerview.widget.RecyclerView>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
x
Reference in New Issue
Block a user