Skip to content

Instantly share code, notes, and snippets.

View aibolik's full-sized avatar
🇰🇿
Developer from KZ

Aibol Kussain aibolik

🇰🇿
Developer from KZ
View GitHub Profile
@aibolik
aibolik / network.java
Created March 13, 2016 01:14
Sunshine. Code Snippet for network call
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String forecastJsonStr = null;
try {
// Construct the URL for the OpenWeatherMap query
@aibolik
aibolik / uri_builder.java
Created March 13, 2016 01:39
Using Uri Builder to build good URLs
final String FORECAST_BASE_URL =
+ "http://api.openweathermap.org/data/2.5/forecast/daily?";
+ final String QUERY_PARAM = "q";
+ final String FORMAT_PARAM = "mode";
+ final String UNITS_PARAM = "units";
+ final String DAYS_PARAM = "cnt";
+ final String APPID_PARAM = "APPID";
+
+ Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
+ .appendQueryParameter(QUERY_PARAM, params[0])
@aibolik
aibolik / ForecastFragment.java
Created March 13, 2016 01:45
Sunshine. JSON Parser
/* The date/time conversion code is going to be moved outside the asynctask later,
* so for convenience we're breaking it out into its own method now.
*/
private String getReadableDateString(long time){
// Because the API returns a unix timestamp (measured in seconds),
// it must be converted to milliseconds in order to be converted to valid date.
SimpleDateFormat shortenedDateFormat = new SimpleDateFormat("EEE MMM dd");
return shortenedDateFormat.format(time);
}
@aibolik
aibolik / ForecastFragment.java
Created March 14, 2016 02:07
Sunshine. AsyncTask onPostExecute - replacing original data
@Override
protected void onPostExecute(String[] result) {
if(result != null) {
mForecastAdapter.clear();
for (String dayForecastStr : result) {
mForecastAdapter.add(dayForecastStr);
}
}
}
@aibolik
aibolik / WeatherContract.java
Created March 16, 2016 14:39
Sunshine. WeatherContract sample
/*
* Copyright (C) 2014 The Android Open Source Project
*
* 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
@aibolik
aibolik / WeatherDbHelper.java
Created March 16, 2016 14:40
Sunshine. SQLiteOpenHelper for Database
/*
* Copyright (C) 2014 The Android Open Source Project
*
* 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
@aibolik
aibolik / date_format.xml
Created March 25, 2016 05:53
Sunshine. Utility - making friendly date format in xml
<!-- Date format for displaying day of week and date (i.e. Mon Jun 1) [CHAR LIMIT=20] -->
<string name="format_full_friendly_date"><xliff:g id="day">%1$s</xliff:g>, <xliff:g id="date">%2$s</xliff:g></string>
@aibolik
aibolik / Utility.java
Created March 25, 2016 05:56
Sunshine. Converts date to "Friendly date"(i.e. Today, Tomorrow, Wednesday, etc.)
// Format used for storing dates in the database. ALso used for converting those strings
// back into date objects for comparison/processing.
public static final String DATE_FORMAT = "yyyyMMdd";
/**
* Helper method to convert the database representation of the date into something to display
* to users. As classy and polished a user experience as "20140102" is, we can do better.
*
* @param context Context to use for resource localization
* @param dateInMillis The date in milliseconds
@aibolik
aibolik / format_temperature.xml
Created March 25, 2016 06:13
Sunshine. Formatting temperature(best practice)
<string name="format_temperature"><xliff:g id="temp">%1.0f</xliff:g>\u00B0</string>
@aibolik
aibolik / SomePresenter.java
Created June 20, 2016 09:54
Getting list of cities. [for M-Tender project]
public void getRegionsList() {
Log.d("mtender", "ProfilePresenter > getRegionsList");
final LinkedHashMap<Integer, List<SpinnerItem>> regionsWithCities = new LinkedHashMap<>();
final List<SpinnerItem> regionsList = new ArrayList<>();
String USER_REGIONS_URL = "http://46.101.144.46/frontend/web/api/v1/region/list";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()