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

Loginov Version 1 #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
21 changes: 14 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion 28
//buildToolsVersion "23.0.3"

defaultConfig {
applicationId "ru.android_2019.citycam"
minSdkVersion 14
targetSdkVersion 22
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
Expand All @@ -17,11 +17,18 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
}
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.android_2019.citycam" >

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/ru/android_2019/citycam/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.android_2019.citycam;

import android.app.Application;
import android.arch.persistence.room.Room;

import ru.android_2019.citycam.database.AppDataBase;

public class App extends Application {
private static App instance;
private AppDataBase dataBase;
@Override
public void onCreate() {
super.onCreate();
instance = this;
dataBase = Room.databaseBuilder(this, AppDataBase.class, "database")
.build();
}

public static App getInstance() {
return instance;
}

public AppDataBase getDataBase() {
return dataBase;
}
}
156 changes: 135 additions & 21 deletions app/src/main/java/ru/android_2019/citycam/CityCamActivity.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,165 @@
package ru.android_2019.citycam;

import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.JsonReader;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;


import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;

import ru.android_2019.citycam.database.WebcamDAO;
import ru.android_2019.citycam.lists.RecylcerDividersDecorator;
import ru.android_2019.citycam.lists.webcam_list.WebcamAdapter;
import ru.android_2019.citycam.model.City;
import ru.android_2019.citycam.model.Webcam;
import ru.android_2019.citycam.webcams.WebCamParser;
import ru.android_2019.citycam.webcams.Webcams;

/**
* Экран, показывающий веб-камеру одного выбранного города.
* Выбранный город передается в extra параметрах.
*/
public class CityCamActivity extends AppCompatActivity {

/**
* Обязательный extra параметр - объект City, камеру которого надо показать.
*/
public static final String EXTRA_CITY = "city";
private static final String LOG_TAG = "CityCam";

private City city;
private PictureDownloadTask downloadTask;

private ImageView camImageView;
private ProgressBar progressView;
private WebcamAdapter adapter;
private List<Webcam> webcamList;
private RecyclerView recyclerView;
private WebcamDAO webcamDAO;
private ImageView imageViewNotFound;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

city = getIntent().getParcelableExtra(EXTRA_CITY);
if (city == null) {
Log.w(TAG, "City object not provided in extra parameter: " + EXTRA_CITY);
Log.w(LOG_TAG, "City object not provided in extra parameter: " + EXTRA_CITY);
finish();
}

setContentView(R.layout.activity_city_cam);
camImageView = (ImageView) findViewById(R.id.cam_image);
progressView = (ProgressBar) findViewById(R.id.progress);
recyclerView = findViewById(R.id.activity_city_cam__list);
imageViewNotFound = findViewById(R.id.activity_city_cam__image);

recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
recyclerView.addItemDecoration(new RecylcerDividersDecorator(Color.DKGRAY));

webcamList = new ArrayList<>();
adapter = new WebcamAdapter(this, webcamList);
recyclerView.setAdapter(adapter);

if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(city.getName());
}

getSupportActionBar().setTitle(city.name);
webcamDAO = App.getInstance().getDataBase().webcamDAO();

progressView.setVisibility(View.VISIBLE);
if (savedInstanceState != null) {
downloadTask = (PictureDownloadTask) getLastCustomNonConfigurationInstance();
}
if (downloadTask == null) {
downloadTask = new PictureDownloadTask(this);
downloadTask.execute(city);
} else {
downloadTask.attachActivity(this);
}
}

// Здесь должен быть код, инициирующий асинхронную загрузку изображения с веб-камеры
// в выбранном городе.
@Override
public Object onRetainCustomNonConfigurationInstance() {
downloadTask.attachActivity(null);
return downloadTask;
}

private static final String TAG = "CityCam";
}
static class PictureDownloadTask extends AsyncTask<City, Void, Void> {
boolean haveResultFromNetWork;
List<Webcam> list;
private WeakReference<CityCamActivity> weakReference;

PictureDownloadTask(CityCamActivity activity) {
this.weakReference = new WeakReference<>(activity);
}

void attachActivity(CityCamActivity activity) {
this.weakReference = new WeakReference<>(activity);
updateView();
}

void updateView() {
CityCamActivity activity = weakReference.get();
if (activity != null) {
if (!list.isEmpty()) {
activity.imageViewNotFound.setVisibility(View.GONE);
activity.recyclerView.setVisibility(View.VISIBLE);
activity.webcamList.clear();
activity.webcamList.addAll(list);
activity.adapter.notifyDataSetChanged();
} else {
activity.recyclerView.setVisibility(View.GONE);
activity.imageViewNotFound.setVisibility(View.VISIBLE);
activity.imageViewNotFound.setImageResource(haveResultFromNetWork ? R.drawable.nothing : R.drawable.notfound);
}
}
}

@Override
protected void onPreExecute() {
super.onPreExecute();
list = new ArrayList<>();
haveResultFromNetWork = false;
}

@Override
protected Void doInBackground(City... cities) {
City city = cities[0];
try {
HttpURLConnection httpURLConnection = (HttpURLConnection) Webcams.createNearbyUrl(city.getLatitude(), city.getLongitude()).openConnection();
httpURLConnection.setRequestProperty("X-RapidAPI-Key", "bb43131250mshf0d6ed9887777a2p1fada8jsnb76edde66d91");
httpURLConnection.connect();
Log.d(LOG_TAG, "Begin connect " + httpURLConnection.getURL());
Log.d(LOG_TAG, httpURLConnection.getResponseCode() + " " + httpURLConnection.getResponseMessage());

JsonReader reader = new JsonReader(new InputStreamReader(httpURLConnection.getInputStream()));
list = new WebCamParser(city.name).parse(reader);
insertToDatabase(list);

reader.close();
haveResultFromNetWork = true;
} catch (IOException e) {
e.printStackTrace();
getDataFromDatabase(city.name);
}
return null;
}

void getDataFromDatabase(String cityName) {
CityCamActivity activity = weakReference.get();
list = activity.webcamDAO.getWebcamsByCity(cityName);
l0gark marked this conversation as resolved.
Show resolved Hide resolved
}

void insertToDatabase(List<Webcam> webcamList) {
CityCamActivity cityCamActivity = weakReference.get();
if (cityCamActivity != null) {
cityCamActivity.webcamDAO.insertOrUpdateList(webcamList);
}
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
updateView();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import ru.android_2019.citycam.list.CitiesRecyclerAdapter;
import ru.android_2019.citycam.list.CitySelectedListener;
import ru.android_2019.citycam.list.RecylcerDividersDecorator;
import ru.android_2019.citycam.lists.city_list.CitiesRecyclerAdapter;
import ru.android_2019.citycam.lists.city_list.CitySelectedListener;
import ru.android_2019.citycam.lists.RecylcerDividersDecorator;
import ru.android_2019.citycam.model.City;

public class SelectCityActivity extends AppCompatActivity
implements CitySelectedListener {
public class SelectCityActivity extends AppCompatActivity implements CitySelectedListener {

// Прокручивающийся список городов
private RecyclerView recyclerView;
Expand All @@ -23,7 +22,7 @@ public class SelectCityActivity extends AppCompatActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_city);
recyclerView = (RecyclerView) findViewById(R.id.list);
recyclerView = findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new RecylcerDividersDecorator(Color.DKGRAY));
CitiesRecyclerAdapter adapter = new CitiesRecyclerAdapter(this);
Expand All @@ -41,5 +40,4 @@ public void onCitySelected(City city) {
}

private static final String TAG = "SelectCity";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.android_2019.citycam.database;

import android.arch.persistence.room.Database;
import android.arch.persistence.room.RoomDatabase;

import ru.android_2019.citycam.model.Webcam;

@Database(entities = {Webcam.class}, version = 1, exportSchema = false)
public abstract class AppDataBase extends RoomDatabase {
public abstract WebcamDAO webcamDAO();
}
33 changes: 33 additions & 0 deletions app/src/main/java/ru/android_2019/citycam/database/WebcamDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ru.android_2019.citycam.database;

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.Transaction;
import android.arch.persistence.room.Update;

import java.util.List;

import ru.android_2019.citycam.model.Webcam;

@Dao
public abstract class WebcamDAO {
@Query("SELECT * FROM webcam where city = :city")
public abstract List<Webcam> getWebcamsByCity(String city);

@Insert
public abstract void insert(Webcam webcam);

@Update
public abstract int update(Webcam webcam);

@Transaction
public void insertOrUpdateList(List<Webcam> webcamList) {
for (Webcam webcam : webcamList) {
int isUpdated = update(webcam);
if (isUpdated == 0) {
insert(webcam);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package ru.android_2019.citycam.list;
package ru.android_2019.citycam.lists;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;

Expand All @@ -22,10 +23,10 @@ public RecylcerDividersDecorator(int color) {
}

@Override
public void getItemOffsets(Rect outRect,
View view,
RecyclerView recyclerView,
RecyclerView.State state) {
public void getItemOffsets(@NonNull Rect outRect,
@NonNull View view,
@NonNull RecyclerView recyclerView,
@NonNull RecyclerView.State state) {
int position = recyclerView.getChildLayoutPosition(view);
RecyclerView.Adapter adapter = recyclerView.getAdapter();
int itemCount = adapter == null ? 0 : adapter.getItemCount();
Expand All @@ -38,7 +39,7 @@ public void getItemOffsets(Rect outRect,
}

@Override
public void onDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.State state) {
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.State state) {
RecyclerView.Adapter adapter = recyclerView.getAdapter();
int itemCount = adapter == null ? 0 : adapter.getItemCount();

Expand Down
Loading