Java tutorial
/* * Copyright 2016 Denis Timakov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ru.timakden.yacer2; import android.app.Fragment; import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import ru.timakden.yacer2.util.ExchangeRateAdapter; import ru.timakden.yacer2.util.db.dao.DaoSession; import ru.timakden.yacer2.util.db.dao.ExchangeRateDao; import ru.timakden.yacer2.util.db.model.ExchangeRate; import ru.timakden.yacer2.util.update.Updater; import java.text.DateFormat; import java.util.Date; import java.util.List; import java.util.TimeZone; import java.util.concurrent.TimeUnit; public class MainFragment extends Fragment { private static final String LOG_TAG = MainFragment.class.getSimpleName(); private SwipeRefreshLayout swipeRefreshLayout; @Override public void onStart() { super.onStart(); // ?? ?? ? populateList(); // ? SharedPreferences ? ? ? ?. ? ?, ? // ?; ? , ? RecyclerView . SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); boolean updated = sharedPreferences.getBoolean("updated", false); boolean updateAtStartup = sharedPreferences.getBoolean(getString(R.string.update_at_startup_key), true); if (updateAtStartup && !updated) { sharedPreferences.edit().putBoolean("updated", true).apply(); swipeRefreshLayout.postDelayed(new Runnable() { @Override public void run() { swipeRefreshLayout.setRefreshing(true); new UpdateTask().execute(); } }, 1000); } else { populateList(); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout); swipeRefreshLayout.setColorSchemeResources(R.color.red, R.color.green, R.color.blue); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { new UpdateTask().execute(); } }); return rootView; } /** * ListView ? . */ public void populateList() { DaoSession daoSession = ((YacerApplication) getActivity().getApplicationContext()).getDaoSession(); ExchangeRateDao exchangeRateDao = daoSession.getExchangeRateDao(); // ? ? ? long date = 0; try { date = exchangeRateDao.queryBuilder().limit(1).distinct().orderDesc(ExchangeRateDao.Properties.Date) .unique().getDate(); } catch (Exception e) { Log.e(LOG_TAG, "Error Message: " + e.getMessage(), e); } // ?? ? String rawSql = ", CURRENCY C WHERE T.DATE = ? AND C._ID = T.CURRENCY_ID ORDER BY C.SORT_ORDER ASC"; List<ExchangeRate> exchangeRates = exchangeRateDao.queryRawCreate(rawSql, date).list(); if (exchangeRates.size() == 0) { Toast.makeText(getActivity(), R.string.populate_list_failed, Toast.LENGTH_SHORT).show(); } else { AppCompatActivity appCompatActivity = (AppCompatActivity) getActivity(); ActionBar supportActionBar = appCompatActivity.getSupportActionBar(); if (supportActionBar != null) { // ?? ? UTC, UTC long dateInMilliseconds = TimeUnit.SECONDS.toMillis(exchangeRates.get(0).getDate()); DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); String dateString = dateFormat.format(new Date(dateInMilliseconds)); supportActionBar.setSubtitle(dateString); } RecyclerView recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view); recyclerView.setHasFixedSize(true); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()); recyclerView.setLayoutManager(linearLayoutManager); ExchangeRateAdapter exchangeRateAdapter = new ExchangeRateAdapter(exchangeRates, getActivity()); recyclerView.setAdapter(exchangeRateAdapter); } if (swipeRefreshLayout.isRefreshing()) { swipeRefreshLayout.setRefreshing(false); } } /** * ? ?? ? ? ? ? {@code AsyncTask}. */ private class UpdateTask extends AsyncTask<Void, Void, Integer> { @Override protected void onPostExecute(Integer integer) { switch (integer) { case Updater.NO_NETWORK: Toast.makeText(getActivity(), R.string.no_connection, Toast.LENGTH_SHORT).show(); break; case Updater.EXCEPTION_DURING_UPDATE: Toast.makeText(getActivity(), R.string.update_failed, Toast.LENGTH_SHORT).show(); } populateList(); super.onPostExecute(integer); } @Override protected Integer doInBackground(Void... params) { return Updater.update(getActivity()); } } }