Created
July 10, 2014 11:52
-
-
Save vikewoods/1489433c77ccaf125767 to your computer and use it in GitHub Desktop.
Navigation catalog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ua.in.vedev.vipcable.Fragment; | |
import android.app.Activity; | |
import android.app.Fragment; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.support.v4.widget.SwipeRefreshLayout; | |
import android.util.Log; | |
import android.view.ContextMenu; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.AdapterView; | |
import android.widget.ListView; | |
import android.widget.SimpleAdapter; | |
import android.widget.Toast; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import ua.in.vedev.myapplication.R; | |
import ua.in.vedev.vipcable.Activity.Landing; | |
import ua.in.vedev.vipcable.Data.JsonWorker.JSONService; | |
public class Catalog extends Fragment { | |
// URL to get contacts JSON | |
private static String url = "ht" | |
// JSON Node names | |
private static final String TAG_API_STATUS = "success"; | |
private static final String TAG_CATALOG = "categories"; | |
private static final String TAG_CATALOG_NAME = "category_name"; | |
private static final String TAG_CATALOG_ID = ""; | |
private static final String TAG_CHILDREN = "children"; | |
private static final String TAG_CHILDREN_ID = "id"; | |
private static final String TAG_CHILDREN_NAME = "name"; | |
private static final String TAG_CHILDREN_TOTAL = "total"; | |
private static final String TAG_CHILDREN_URI = "href"; | |
// contacts JSONArray | |
JSONArray catalog_list = null; | |
// Hashmap for ListView | |
ArrayList<HashMap<String, String>> catalogList; | |
// Swipe refresh | |
SwipeRefreshLayout mSwipeRefreshLayout; | |
public Catalog() { | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if(getArguments() != null){ | |
Log.d(TAG_CATALOG, "Arg is: " + getArguments().size()); | |
} | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) { | |
Log.d(TAG_API_STATUS, "Get activity actions " + getActivity().getPackageName()); | |
// Creating root view | |
View rootView = inflater.inflate(R.layout.fragment_catalog, container, false); | |
// Creating array for catalog list | |
catalogList = new ArrayList<HashMap<String, String>>(); | |
// Getting data from server async | |
new GetCatalogs().execute(); | |
// Initialize Swipe on Refresh actions | |
mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.pull_to_refresh); | |
// Setting colors | |
mSwipeRefreshLayout.setColorSchemeResources( | |
R.color.brand_green, | |
R.color.brand_green, | |
R.color.brand_blue, | |
R.color.brand_green | |
); | |
// Set on refresh action | |
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { | |
@Override | |
public void onRefresh() { | |
getActivity().getActionBar().setTitle("Refreshing data..."); | |
getActivity().getActionBar().setDisplayHomeAsUpEnabled(false); | |
getActivity().getActionBar().setDisplayShowHomeEnabled(false); | |
//actionRightMenuSection.findItem(R.id.action_settings).setVisible(false); | |
mSwipeRefreshLayout.setRefreshing(true); | |
mSwipeRefreshLayout.postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
// Clean up list | |
catalogList.clear(); | |
// Get catalogs async | |
new GetCatalogs().execute(); | |
} | |
}, 2000); | |
} | |
}); | |
// Handle user click: long click and just regular click | |
ListView catalogViewClick = (ListView)rootView.findViewById(R.id.catalog_list_view); | |
registerForContextMenu(catalogViewClick); | |
// Passing data to parent catalog fragment | |
catalogViewClick.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { | |
Toast.makeText(getActivity(), "int: "+i+" long: "+l, Toast.LENGTH_LONG).show(); | |
} | |
}); | |
// Reurn view | |
return rootView; | |
} | |
@Override | |
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { | |
super.onCreateContextMenu(menu, v, menuInfo); | |
menu.setHeaderTitle("Context menu:"); | |
menu.add(0, v.getId(), 0, "Edit"); | |
menu.add(1, v.getId(), 1, "Copy"); | |
} | |
@Override | |
public boolean onContextItemSelected(MenuItem item) { | |
return super.onContextItemSelected(item); | |
} | |
private class GetCatalogs extends AsyncTask<Void, Void, Void> { | |
@Override | |
protected Void doInBackground(Void... voids) { | |
// Making a request to url and getting response | |
JSONService sh = new JSONService(); | |
String jsonStr = sh.makeJsonServiceCall(url, JSONService.GET); | |
if(jsonStr != null){ | |
try{ | |
// Convert to object | |
JSONObject jsonObj = new JSONObject(jsonStr); | |
Boolean authorized = jsonObj.getBoolean(TAG_API_STATUS); | |
if(authorized){ | |
// Get catalog list as an array | |
catalog_list = jsonObj.getJSONArray(TAG_CATALOG); | |
// Loop array to parse data | |
for (int i = 0; i < catalog_list.length(); i++) { | |
// Convert to object catalog list | |
JSONObject catalog_listJSONObject = catalog_list.getJSONObject(i); | |
// Saving catalog name to variable as string | |
String catalogName = catalog_listJSONObject.getString(TAG_CATALOG_NAME); | |
// Convert children catalog list to an JSON array | |
JSONArray childrenCatalog = catalog_listJSONObject.getJSONArray(TAG_CHILDREN); | |
// Loop them to cath every of them | |
for(int cc = 0; cc < childrenCatalog.length(); cc++){ | |
// Creating object of catalog childrens | |
JSONObject childrenCatalogListObject = childrenCatalog.getJSONObject(cc); | |
// Finding childer catalog values and assign them as strings | |
String childrenCatalogId = childrenCatalogListObject.getString(TAG_CHILDREN_ID); | |
Integer childrenCatalogTotalIn = childrenCatalogListObject.getInt(TAG_CHILDREN_TOTAL); | |
String childrenCatalogName = childrenCatalogListObject.getString(TAG_CHILDREN_NAME); | |
String childrenCatalogUri = childrenCatalogListObject.getString(TAG_CHILDREN_URI); | |
// Temp for single children data | |
HashMap<String, String> childrenCatalogItem = new HashMap<String, String>(); | |
// Adding each child to HASH | |
childrenCatalogItem.put(TAG_CATALOG_NAME, catalogName); | |
childrenCatalogItem.put(TAG_CHILDREN_ID, childrenCatalogId); | |
childrenCatalogItem.put(TAG_CHILDREN_TOTAL, childrenCatalogTotalIn.toString()); | |
childrenCatalogItem.put(TAG_CHILDREN_NAME, childrenCatalogName); | |
childrenCatalogItem.put(TAG_CHILDREN_URI, childrenCatalogUri); | |
// Add all to catalog list | |
catalogList.add(childrenCatalogItem); | |
} | |
} | |
}else{ | |
} | |
}catch (JSONException e){ | |
e.printStackTrace(); | |
} | |
}else{ | |
} | |
return null; | |
} | |
@Override | |
protected void onPreExecute() { | |
super.onPreExecute(); | |
} | |
@Override | |
protected void onProgressUpdate(Void... values) { | |
super.onProgressUpdate(values); | |
} | |
@Override | |
protected void onPostExecute(Void aVoid) { | |
super.onPostExecute(aVoid); | |
ListView catalogListView = (ListView)getActivity().findViewById(R.id.catalog_list_view); | |
SimpleAdapter adapter = new SimpleAdapter( | |
getActivity(), | |
catalogList, | |
R.layout.fragment_catalog_item, | |
new String[] { | |
TAG_CHILDREN_NAME | |
}, | |
new int[] { | |
R.id.catalog_item_name | |
} | |
); | |
catalogListView.setAdapter(adapter); | |
mSwipeRefreshLayout.setRefreshing(false); | |
getActivity().getActionBar().setTitle("Catalog"); | |
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); | |
getActivity().getActionBar().setDisplayShowHomeEnabled(true); | |
} | |
@Override | |
protected void onCancelled() { | |
super.onCancelled(); | |
catalogList.clear(); | |
mSwipeRefreshLayout.setRefreshing(false); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014. | |
* Developed by Vik Ewoods in VE Development Team in 2014. | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
package ua.in.vedev.vipcable.Activity; | |
import android.animation.ValueAnimator; | |
import android.app.Activity; | |
import android.app.Fragment; | |
import android.app.FragmentManager; | |
import android.content.Intent; | |
import android.content.res.Configuration; | |
import android.os.Bundle; | |
import android.support.v4.app.ActionBarDrawerToggle; | |
import android.support.v4.widget.DrawerLayout; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ListView; | |
import java.util.ArrayList; | |
import java.util.List; | |
import ua.in.vedev.myapplication.R; | |
import ua.in.vedev.vipcable.Fragment.Catalog; | |
import ua.in.vedev.vipcable.Fragment.Home; | |
import ua.in.vedev.vipcable.Fragment.Information; | |
import ua.in.vedev.vipcable.UI.Drawer.CustomNavigationDrawerAdapter; | |
import ua.in.vedev.vipcable.UI.Drawer.NavigationDrawerItem; | |
public class Landing extends Activity { | |
private DrawerLayout mDrawerLayout; | |
private ListView mDrawerList; | |
private ActionBarDrawerToggle mDrawerToggle; | |
private CharSequence mDrawerTitle; | |
private CharSequence mTitle; | |
CustomNavigationDrawerAdapter adapter; | |
List<NavigationDrawerItem> dataList; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_landing); | |
//Fragment catalog = new Catalog(); | |
// Initializing navigation drawer | |
dataList = new ArrayList<NavigationDrawerItem>(); | |
mTitle = mDrawerTitle = getTitle(); | |
mDrawerLayout = (DrawerLayout) findViewById(R.id.landing_drawer); | |
mDrawerList = (ListView) findViewById(R.id.left_drawer); | |
//mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); | |
dataList.add(new NavigationDrawerItem("Home", android.R.drawable.ic_input_delete)); | |
dataList.add(new NavigationDrawerItem("Catalog", android.R.drawable.ic_menu_agenda)); | |
dataList.add(new NavigationDrawerItem("Information", android.R.drawable.ic_menu_close_clear_cancel)); | |
adapter = new CustomNavigationDrawerAdapter(this, R.layout.drawer_item, dataList); | |
mDrawerList.setAdapter(adapter); | |
mDrawerList.setOnItemClickListener(new NavigationDrawerItemClickListener()); | |
getActionBar().setDisplayHomeAsUpEnabled(true); | |
getActionBar().setHomeButtonEnabled(true); | |
getActionBar().setIcon(R.drawable.ic_launcher); | |
getActionBar().setDisplayShowHomeEnabled(true); | |
mDrawerToggle = new ActionBarDrawerToggle( | |
this, | |
mDrawerLayout, | |
R.drawable.action_bar_left_side, | |
R.string.drawer_open, | |
R.string.drawer_close) { | |
public void onDrawerClosed(View view) { | |
getActionBar().setTitle(mTitle); | |
invalidateOptionsMenu(); // creates call to | |
// onPrepareOptionsMenu() | |
} | |
public void onDrawerOpened(View drawerView) { | |
mDrawerTitle = "Menu"; | |
getActionBar().setTitle(mDrawerTitle); | |
invalidateOptionsMenu(); // creates call to | |
// onPrepareOptionsMenu() | |
} | |
}; | |
mDrawerLayout.setDrawerListener(mDrawerToggle); | |
if (savedInstanceState == null) { | |
SelectItem(0); | |
} | |
} | |
public void SelectItem(int possition) { | |
Fragment fragment = null; | |
Bundle args = new Bundle(); | |
switch (possition) { | |
case 0: | |
fragment = new Home(); | |
args.putString(Home.ITEM_NAME, dataList.get(possition).getItemName()); | |
args.putInt(Home.IMAGE_RESOURCE_ID, dataList.get(possition).getImgResID()); | |
break; | |
case 1: | |
fragment = new Catalog(); | |
//startActivity(intent); | |
break; | |
case 2: | |
fragment = new Information(); | |
args.putString(Information.ITEM_NAME, dataList.get(possition).getItemName()); | |
args.putInt(Information.IMAGE_RESOURCE_ID, dataList.get(possition).getImgResID()); | |
break; | |
default: | |
break; | |
} | |
fragment.setArguments(args); | |
FragmentManager frgManager = getFragmentManager(); | |
frgManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); | |
mDrawerList.setItemChecked(possition, true); | |
setTitle(dataList.get(possition).getItemName()); | |
mDrawerLayout.closeDrawer(mDrawerList); | |
} | |
@Override | |
public void setTitle(CharSequence title) { | |
mTitle = title; | |
getActionBar().setTitle(mTitle); | |
} | |
@Override | |
protected void onPostCreate(Bundle savedInstanceState) { | |
super.onPostCreate(savedInstanceState); | |
// Sync the toggle state after onRestoreInstanceState has occurred. | |
mDrawerToggle.syncState(); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// The action bar home/up action should open or close the drawer. | |
// ActionBarDrawerToggle will take care of this. | |
if (mDrawerToggle.onOptionsItemSelected(item)) { | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public boolean onPrepareOptionsMenu(Menu menu) { | |
/* | |
Hide all action in right section of drawwer | |
*/ | |
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); | |
menu.findItem(R.id.action_settings).setVisible(!drawerOpen); | |
return super.onPrepareOptionsMenu(menu); | |
} | |
@Override | |
public void onConfigurationChanged(Configuration newConfig) { | |
super.onConfigurationChanged(newConfig); | |
// Pass any configuration change to the drawer toggles | |
mDrawerToggle.onConfigurationChanged(newConfig); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.landing, menu); | |
return true; | |
} | |
// Todo: Separate this in other file | |
private class NavigationDrawerItemClickListener implements ListView.OnItemClickListener { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
SelectItem(position); | |
} | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014. | |
* Developed by Vik Ewoods in VE Development Team in 2014. | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
package ua.in.vedev.vipcable.Fragment; | |
import android.app.Fragment; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.support.v4.widget.SwipeRefreshLayout; | |
import android.view.ContextMenu; | |
import android.view.LayoutInflater; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ListView; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import ua.in.vedev.myapplication.R; | |
import ua.in.vedev.vipcable.Data.JsonWorker.JSONService; | |
//todo: Thinking about removing TAG_API_URI_PARENT_CATALOG, api should work from an id | |
public class ParentCatalog extends Fragment { | |
// Api url | |
public String TAG_API_URI_PARENT_CATALOG; | |
public String TAG_CATALOG_ID; | |
// Tagging json tags | |
private static final String TAG_PARENT_CATALOG = ""; | |
private static final String TAG_API_STATUS = "success"; | |
private static final String TAG_PARENT_CATALOG_NAME = "parent_catalogs"; | |
private static final String TAG_PARENT_CATALOG_ID = "id"; | |
private static final String TAG_PARENT_CATALOG_TOTAL = "total"; | |
private static final String TAG_PARENT_CATALOG_URI = "href"; | |
private static final String TAG_PARENT_CATALOG_FOLLOW_LINK = "follow"; | |
// Parent catalog JSON Array | |
JSONArray parent_catalog_list; | |
// Hasmap for listview | |
ArrayList<HashMap<String, String>> parentCatalogList; | |
// Swipe to refresh | |
SwipeRefreshLayout mSwipeRefreshLayout; | |
// Try to work out with catalog id for now | |
public void setTAG_CATALOG_ID(String TAG_CATALOG_ID) { | |
this.TAG_CATALOG_ID = TAG_CATALOG_ID; | |
} | |
// Getting api for parent catalog | |
public String getTAG_API_URI_PARENT_CATALOG() { | |
return TAG_API_URI_PARENT_CATALOG; | |
} | |
// Setting api for parent catalog | |
public void setTAG_API_URI_PARENT_CATALOG(String TAG_API_URI_PARENT_CATALOG) { | |
this.TAG_API_URI_PARENT_CATALOG = TAG_API_URI_PARENT_CATALOG; | |
} | |
// Default constructor | |
public ParentCatalog(){} | |
// On creating the hole view | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
// Creating root view as @fragment_parent_catalog | |
View rootView = inflater.inflate(R.layout.fragment_parent_catalog, container, false); | |
// Initialize catalog list view data to array | |
parentCatalogList = new ArrayList<HashMap<String, String>>(); | |
// Initialize first async data request when user open the fragment view | |
// Initialize swipe refresh | |
mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.pull_to_refresh); | |
mSwipeRefreshLayout.setColorSchemeResources(R.color.brand_green, R.color.brand_green, R.color.brand_blue, R.color.brand_green); | |
// Bind on refresh action for view | |
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { | |
@Override | |
public void onRefresh() { | |
// Customize :) action bar | |
getActivity().getActionBar().setTitle("Refreshing data..."); | |
getActivity().getActionBar().setDisplayHomeAsUpEnabled(false); | |
getActivity().getActionBar().setDisplayShowHomeEnabled(false); | |
// Shown user progress of updates | |
mSwipeRefreshLayout.setRefreshing(true); | |
// Delay for 2 second in case if user got super-puper internet connection | |
mSwipeRefreshLayout.postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
parentCatalogList.clear(); | |
} | |
}, 2000); | |
} | |
}); | |
// Lets find list view to bind context menu | |
ListView parentCatalogListViewLongClickAction = (ListView) rootView.findViewById(R.id.catalog_list_view); | |
registerForContextMenu(parentCatalogListViewLongClickAction); | |
// Returning the view | |
return rootView; | |
} | |
@Override | |
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { | |
super.onCreateContextMenu(menu, v, menuInfo); | |
menu.add(0, v.getId(), 0, "Open url in browser"); | |
menu.add(1, v.getId(), 1, "Copy url for browser"); | |
menu.add(2, v.getId(), 2, "Share this page"); | |
menu.add(3, v.getId(), 3, "Information about parent catalog"); | |
} | |
@Override | |
public boolean onContextItemSelected(MenuItem item) { | |
return super.onContextItemSelected(item); | |
} | |
private class GetParentCatalogs extends AsyncTask<Void, Void, Void> { | |
@Override | |
protected Void doInBackground(Void... voids) { | |
// Initialize json data | |
JSONService jsonService = new JSONService(); | |
String jsonRequestString = jsonService.makeJsonServiceCall(TAG_API_URI_PARENT_CATALOG, JSONService.GET); | |
if(jsonRequestString != null){ | |
try{ | |
// Get json object | |
JSONObject jsonObject = new JSONObject(jsonRequestString); | |
Boolean authorized = jsonObject.getBoolean(TAG_API_STATUS); | |
if(authorized){ | |
// Json array to parent catalog array | |
parent_catalog_list = jsonObject.getJSONArray(TAG_PARENT_CATALOG); | |
for(int pc = 0; pc < parent_catalog_list.length(); pc++){ | |
// Going in each!!! | |
} | |
}else{ | |
// TODO: Write method if api reply on error | |
} | |
}catch (JSONException jsonException){ | |
jsonException.printStackTrace(); | |
} | |
}else{ | |
// TODO: Catch error with internet connections | |
} | |
return null; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment