import
android.os.Bundle;
import
android.view.View;
import
android.widget.EditText;
import
android.widget.ImageView;
import
android.widget.ProgressBar;
import
android.widget.Toast;
import
androidx.appcompat.app.AppCompatActivity;
import
androidx.recyclerview.widget.GridLayoutManager;
import
androidx.recyclerview.widget.LinearLayoutManager;
import
androidx.recyclerview.widget.RecyclerView;
import
com.android.volley.Request;
import
com.android.volley.RequestQueue;
import
com.android.volley.Response;
import
com.android.volley.VolleyError;
import
com.android.volley.toolbox.JsonObjectRequest;
import
com.android.volley.toolbox.Volley;
import
org.json.JSONArray;
import
org.json.JSONException;
import
org.json.JSONObject;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.Map;
public
class
MainActivity
extends
AppCompatActivity
implements
CategoryRVAdapter.CategoryClickInterface {
private
RecyclerView categoryRV, wallpaperRV;
private
ProgressBar loadingPB;
private
ArrayList<CategoryRVModal> categoryRVModals;
private
ArrayList<String> wallpaperArrayList;
private
CategoryRVAdapter categoryRVAdapter;
private
WallpaperRVAdapter wallpaperRVAdapter;
private
EditText searchEdt;
private
ImageView searchIV;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
categoryRV = findViewById(R.id.idRVCategories);
wallpaperRV = findViewById(R.id.idRVWallpapers);
searchEdt = findViewById(R.id.idEdtSearch);
searchIV = findViewById(R.id.idIVSearch);
loadingPB = findViewById(R.id.idPBLoading);
wallpaperArrayList =
new
ArrayList<>();
categoryRVModals =
new
ArrayList<>();
LinearLayoutManager manager1 =
new
LinearLayoutManager(MainActivity.
this
, RecyclerView.HORIZONTAL,
false
);
wallpaperRVAdapter =
new
WallpaperRVAdapter(wallpaperArrayList,
this
);
categoryRVAdapter =
new
CategoryRVAdapter(categoryRVModals,
this
,
this
);
categoryRV.setLayoutManager(manager1);
categoryRV.setAdapter(categoryRVAdapter);
GridLayoutManager layoutManager =
new
GridLayoutManager(
this
,
2
);
wallpaperRV.setLayoutManager(layoutManager);
wallpaperRV.setAdapter(wallpaperRVAdapter);
getCategories();
getWallpapers();
searchIV.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
String searchStr = searchEdt.getText().toString();
if
(searchStr.isEmpty()) {
Toast.makeText(MainActivity.
this
,
"Please enter something to search"
, Toast.LENGTH_SHORT).show();
}
else
{
getWallpapersByCategory(searchStr);
}
}
});
}
private
void
getWallpapersByCategory(String category) {
wallpaperArrayList.clear();
loadingPB.setVisibility(View.VISIBLE);
RequestQueue queue = Volley.newRequestQueue(MainActivity.
this
);
JsonObjectRequest jsonObjectRequest =
new
JsonObjectRequest(Request.Method.GET, url,
null
,
new
Response.Listener<JSONObject>() {
@Override
public
void
onResponse(JSONObject response) {
try
{
loadingPB.setVisibility(View.GONE);
JSONArray photos = response.getJSONArray(
"photos"
);
for
(
int
i =
0
; i < photos.length(); i++) {
JSONObject photoObj = photos.getJSONObject(i);
String imgUrl = photoObj.getJSONObject(
"src"
).getString(
"portrait"
);
wallpaperArrayList.add(imgUrl);
}
wallpaperRVAdapter.notifyDataSetChanged();
}
catch
(JSONException e) {
e.printStackTrace();
}
}
},
new
Response.ErrorListener() {
@Override
public
void
onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.
this
,
"Fail to get data.."
, Toast.LENGTH_SHORT).show();
}
}) {
@Override
public
Map<String, String> getHeaders() {
HashMap<String, String> headers =
new
HashMap<>();
headers.put(
"Authorization"
,
"Enter your key"
);
return
headers;
}
};
queue.add(jsonObjectRequest);
}
private
void
getWallpapers() {
wallpaperArrayList.clear();
loadingPB.setVisibility(View.VISIBLE);
RequestQueue queue = Volley.newRequestQueue(MainActivity.
this
);
JsonObjectRequest jsonObjectRequest =
new
JsonObjectRequest(Request.Method.GET, url,
null
,
new
Response.Listener<JSONObject>() {
@Override
public
void
onResponse(JSONObject response) {
loadingPB.setVisibility(View.GONE);
try
{
JSONArray photos = response.getJSONArray(
"photos"
);
for
(
int
i =
0
; i < photos.length(); i++) {
JSONObject photoObj = photos.getJSONObject(i);
String imgUrl = photoObj.getJSONObject(
"src"
).getString(
"portrait"
);
wallpaperArrayList.add(imgUrl);
}
wallpaperRVAdapter.notifyDataSetChanged();
}
catch
(JSONException e) {
e.printStackTrace();
}
}
},
new
Response.ErrorListener() {
@Override
public
void
onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.
this
,
"Fail to get data.."
, Toast.LENGTH_SHORT).show();
}
}) {
@Override
public
Map<String, String> getHeaders() {
HashMap<String, String> headers =
new
HashMap<>();
headers.put(
"Authorization"
,
"Enter your key"
);
return
headers;
}
};
queue.add(jsonObjectRequest);
}
private
void
getCategories() {
}
@Override
public
void
onCategoryClick(
int
position) {
String category = categoryRVModals.get(position).getCategory();
getWallpapersByCategory(category);
}
}