|
| 1 | +package org.buildmlearn.toolkit.infoTemplate.fragment; |
| 2 | + |
| 3 | +import android.app.FragmentManager; |
| 4 | +import android.content.ContentValues; |
| 5 | +import android.content.Intent; |
| 6 | +import android.database.Cursor; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.support.v4.app.Fragment; |
| 9 | +import android.support.v4.app.LoaderManager.LoaderCallbacks; |
| 10 | +import android.support.v4.content.CursorLoader; |
| 11 | +import android.support.v4.content.Loader; |
| 12 | +import android.support.v7.app.AlertDialog; |
| 13 | +import android.support.v7.widget.Toolbar; |
| 14 | +import android.text.method.LinkMovementMethod; |
| 15 | +import android.view.LayoutInflater; |
| 16 | +import android.view.MenuItem; |
| 17 | +import android.view.View; |
| 18 | +import android.view.ViewGroup; |
| 19 | +import android.widget.TextView; |
| 20 | + |
| 21 | +import org.buildmlearn.toolkit.R; |
| 22 | +import org.buildmlearn.toolkit.infoTemplate.Constants; |
| 23 | +import org.buildmlearn.toolkit.infoTemplate.data.InfoContract; |
| 24 | +import org.buildmlearn.toolkit.infoTemplate.data.InfoDb; |
| 25 | + |
| 26 | +/** |
| 27 | + * Created by Anupam (opticod) on 20/6/16. |
| 28 | + */ |
| 29 | +public class DetailActivityFragment extends Fragment implements LoaderCallbacks<Cursor> { |
| 30 | + |
| 31 | + private static final int DETAIL_LOADER = 0; |
| 32 | + private final ContentValues infoValues; |
| 33 | + |
| 34 | + private View rootView; |
| 35 | + private String info_Id; |
| 36 | + private InfoDb db; |
| 37 | + |
| 38 | + public DetailActivityFragment() { |
| 39 | + setHasOptionsMenu(true); |
| 40 | + infoValues = new ContentValues(); |
| 41 | + } |
| 42 | + |
| 43 | + public static Fragment newInstance() { |
| 44 | + return new DetailActivityFragment(); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 49 | + Bundle savedInstanceState) { |
| 50 | + Bundle arguments = getArguments(); |
| 51 | + if (arguments != null) { |
| 52 | + info_Id = arguments.getString(Intent.EXTRA_TEXT); |
| 53 | + } |
| 54 | + rootView = inflater.inflate(R.layout.fragment_detail_info, container, false); |
| 55 | + |
| 56 | + db = new InfoDb(getContext()); |
| 57 | + db.open(); |
| 58 | + Toolbar maintoolbar = (Toolbar) rootView.findViewById(R.id.toolbar_main); |
| 59 | + maintoolbar.setTitle(getString(R.string.video_collection_title)); |
| 60 | + maintoolbar.inflateMenu(R.menu.menu_main_white); |
| 61 | + maintoolbar.setNavigationIcon(R.drawable.ic_home_white_24dp); |
| 62 | + |
| 63 | + maintoolbar.setNavigationOnClickListener(new View.OnClickListener() { |
| 64 | + @Override |
| 65 | + public void onClick(View v) { |
| 66 | + getActivity().getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); |
| 67 | + getActivity().getSupportFragmentManager().beginTransaction().replace(((ViewGroup) getView().getParent()).getId(), org.buildmlearn.toolkit.infoTemplate.fragment.MainActivityFragment.newInstance()).addToBackStack(null).commit(); |
| 68 | + } |
| 69 | + }); |
| 70 | + maintoolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { |
| 71 | + @Override |
| 72 | + public boolean onMenuItemClick(MenuItem menuItem) { |
| 73 | + switch (menuItem.getItemId()) { |
| 74 | + case R.id.action_about: |
| 75 | + AlertDialog.Builder builder = |
| 76 | + new AlertDialog.Builder(getActivity()); |
| 77 | + builder.setTitle(String.format("%1$s", getString(R.string.about_us))); |
| 78 | + builder.setMessage(getResources().getText(R.string.about_text_video)); |
| 79 | + builder.setPositiveButton("OK", null); |
| 80 | + AlertDialog welcomeAlert = builder.create(); |
| 81 | + welcomeAlert.show(); |
| 82 | + assert welcomeAlert.findViewById(android.R.id.message) != null; |
| 83 | + assert welcomeAlert.findViewById(android.R.id.message) != null; |
| 84 | + ((TextView) welcomeAlert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance()); |
| 85 | + break; |
| 86 | + } |
| 87 | + return true; |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + return rootView; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onActivityCreated(Bundle savedInstanceState) { |
| 96 | + getLoaderManager().initLoader(DETAIL_LOADER, null, this); |
| 97 | + super.onActivityCreated(savedInstanceState); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 102 | + if (null != info_Id) { |
| 103 | + switch (id) { |
| 104 | + case DETAIL_LOADER: |
| 105 | + |
| 106 | + return new CursorLoader(getActivity(), null, Constants.INFO_COLUMNS, null, null, null) { |
| 107 | + @Override |
| 108 | + public Cursor loadInBackground() { |
| 109 | + return db.getInfoCursorById(Integer.parseInt(info_Id)); |
| 110 | + } |
| 111 | + }; |
| 112 | + } |
| 113 | + } |
| 114 | + return null; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void onLoadFinished(Loader<Cursor> loader, final Cursor data) { |
| 119 | + if (!data.moveToFirst()) { |
| 120 | + return; |
| 121 | + } |
| 122 | + switch (loader.getId()) { |
| 123 | + case DETAIL_LOADER: |
| 124 | + String title = data.getString(Constants.COL_TITLE); |
| 125 | + |
| 126 | + ((TextView) rootView.findViewById(R.id.title)) |
| 127 | + .setText(title); |
| 128 | + |
| 129 | + String description = data.getString(Constants.COL_DESCRIPTION); |
| 130 | + |
| 131 | + ((TextView) rootView.findViewById(R.id.description)) |
| 132 | + .setText(description); |
| 133 | + |
| 134 | + final long numColumns = db.getCount(); |
| 135 | + |
| 136 | + if (Integer.parseInt(info_Id) == numColumns) { |
| 137 | + |
| 138 | + rootView.findViewById(R.id.next).setVisibility(View.INVISIBLE); |
| 139 | + rootView.findViewById(R.id.exit).setOnClickListener(new View.OnClickListener() { |
| 140 | + @Override |
| 141 | + public void onClick(View v) { |
| 142 | + getActivity().finish(); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + } else { |
| 147 | + |
| 148 | + rootView.findViewById(R.id.exit).setVisibility(View.INVISIBLE); |
| 149 | + rootView.findViewById(R.id.next).setOnClickListener(new View.OnClickListener() { |
| 150 | + @Override |
| 151 | + public void onClick(View v) { |
| 152 | + |
| 153 | + long nextInfoId = Integer.parseInt(info_Id) + 1; |
| 154 | + |
| 155 | + Bundle arguments = new Bundle(); |
| 156 | + arguments.putString(Intent.EXTRA_TEXT, String.valueOf(nextInfoId)); |
| 157 | + |
| 158 | + Fragment frag = DetailActivityFragment.newInstance(); |
| 159 | + frag.setArguments(arguments); |
| 160 | + getActivity().getSupportFragmentManager().beginTransaction().replace(((ViewGroup) getView().getParent()).getId(), frag).addToBackStack(null).commit(); |
| 161 | + |
| 162 | + } |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + if (Integer.parseInt(info_Id) == 1) { |
| 167 | + |
| 168 | + rootView.findViewById(R.id.previous).setVisibility(View.INVISIBLE); |
| 169 | + |
| 170 | + } else { |
| 171 | + |
| 172 | + rootView.findViewById(R.id.previous).setOnClickListener(new View.OnClickListener() { |
| 173 | + @Override |
| 174 | + public void onClick(View v) { |
| 175 | + |
| 176 | + int prevInfoId = Integer.parseInt(info_Id) - 1; |
| 177 | + |
| 178 | + if (prevInfoId >= 1) { |
| 179 | + |
| 180 | + Bundle arguments = new Bundle(); |
| 181 | + arguments.putString(Intent.EXTRA_TEXT, String.valueOf(prevInfoId)); |
| 182 | + |
| 183 | + Fragment frag = DetailActivityFragment.newInstance(); |
| 184 | + frag.setArguments(arguments); |
| 185 | + getActivity().getSupportFragmentManager().beginTransaction().replace(((ViewGroup) getView().getParent()).getId(), frag).addToBackStack(null).commit(); |
| 186 | + |
| 187 | + } |
| 188 | + } |
| 189 | + }); |
| 190 | + } |
| 191 | + |
| 192 | + if (infoValues.size() == 0) { |
| 193 | + infoValues.put(InfoContract.Info._ID, data.getString(Constants.COL_ID)); |
| 194 | + infoValues.put(InfoContract.Info.TITLE, data.getString(Constants.COL_TITLE)); |
| 195 | + infoValues.put(InfoContract.Info.DESCRIPTION, data.getString(Constants.COL_DESCRIPTION)); |
| 196 | + } |
| 197 | + break; |
| 198 | + default: |
| 199 | + throw new UnsupportedOperationException("Unknown Loader"); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public void onLoaderReset(Loader<Cursor> loader) { |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public void onDestroyView() { |
| 209 | + super.onDestroyView(); |
| 210 | + db.close(); |
| 211 | + } |
| 212 | + |
| 213 | +} |
0 commit comments