Skip to content

Instantly share code, notes, and snippets.

@prolongservices
Created May 30, 2020 19:33
Show Gist options
  • Select an option

  • Save prolongservices/1fcc07ce1342c33dc8b88effa22e0a4a to your computer and use it in GitHub Desktop.

Select an option

Save prolongservices/1fcc07ce1342c33dc8b88effa22e0a4a to your computer and use it in GitHub Desktop.
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.view.View;
import com.edablogs.tubelite.R;
import com.edablogs.tubelite.adapter.RecyclerAdapter;
import com.edablogs.tubelite.api.APIService;
import com.edablogs.tubelite.api.YoutubeApi;
import com.edablogs.tubelite.model.youtube.Item;
import com.edablogs.tubelite.model.youtube.ItemLatest;
import com.edablogs.tubelite.model.youtube.LatestModel;
import com.edablogs.tubelite.model.youtube.YoutubeResponse;
import com.edablogs.tubelite.utils.CacheManager;
import com.edablogs.tubelite.utils.CacheUtils;
import com.edablogs.tubelite.utils.Constants;
import com.edablogs.tubelite.utils.M;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private RecyclerView latestList;
private RecyclerAdapter latestAdapter;
private CacheManager cacheManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latestList = (RecyclerView) findViewById(R.id.recent_list);
cacheManager = new CacheManager(this);
latestList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
latestAdapter = new RecyclerAdapter(this,new ArrayList<>());
latestList.setAdapter(latestAdapter);
initFromCache(); //load data from cache if exist.
}
private void initFromCache() {
Type type = new TypeToken<ArrayList<ItemLatest>>() {}.getType();
ArrayList<ItemLatest> itemLatest = (ArrayList<ItemLatest>) cacheManager.readJson(type, CacheUtils.LATEST);
if (itemLatest != null)
latestAdapter.replaceWith(itemLatest);
}
@Override
public void onResume() {
getLatest();
super.onResume();
}
private void getLatest() {
if (M.isConnectionAvailable(this)) {
YoutubeApi youtubeApi = APIService.create(YoutubeApi.class);
Call<LatestModel> latestVideos = youtubeApi.getRecentVideos("video", "date", Constants.KEY, Constants.PART, "10","");
latestVideos.enqueue(new Callback<LatestModel>() {
@Override
public void onResponse(@NonNull Call<LatestModel> call, @NonNull Response<LatestModel> response) {
if (response.body() != null) {
//here is your json data from api, save this into json file.
ArrayList<ItemLatest> items = (ArrayList<ItemLatest>) response.body().getItems();
if (items != null){
latestAdapter.replaceWith(items);
Type type = new TypeToken<ArrayList<ItemLatest>>() {
}.getType();
cacheManager.writeJson(response.body().getItems(), type, CacheUtils.LATEST);
}
}
}
@Override
public void onFailure(@NonNull Call<LatestModel> call, @NonNull Throwable t) {
//api failed to return data due to network problem or something else, display data from json file.
M.l(TAG, t);
Type type = new TypeToken<ArrayList<ItemLatest>>() {
}.getType();
ArrayList<ItemLatest> itemLatests = (ArrayList<ItemLatest>) cacheManager.readJson(type, CacheUtils.LATEST);
if (itemLatests != null)
latestAdapter.replaceWith(itemLatests);
}
});
} else {
//Internet not available, display data from json file.
Type type = new TypeToken<ArrayList<ItemLatest>>() {
}.getType();
ArrayList<ItemLatest> itemLatests = (ArrayList<ItemLatest>) cacheManager.readJson(type, CacheUtils.LATEST);
if (itemLatests != null)
latestAdapter.replaceWith(itemLatests);
Snackbar.make(rootLayout, R.string.no_internet_connection, Snackbar.LENGTH_SHORT).setAction(R.string.retry, new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.this.getLatest();
}
}).show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment