Skip to content

Commit

Permalink
Starting adding sync service
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahul Kadyan authored and znck-graveyard committed Dec 27, 2013
1 parent b74b8a7 commit bc696cb
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.support.v7.app.ActionBarActivity;
import android.view.*;
import android.widget.TextView;
import android.widget.Toast;
import org.techniche.technothlon.katana.tcd.TCDContent;

public class MainActivity extends ActionBarActivity
Expand All @@ -26,6 +25,7 @@ public class MainActivity extends ActionBarActivity
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
private boolean syncingTCD = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -95,14 +95,24 @@ public boolean onOptionsItemSelected(MenuItem item) {
if (id == R.id.action_settings) {
return true;
} else if (id == R.id.action_example) {
TCDContent.TCDLoader loader = new TCDContent.TCDLoader() {
@Override
public void finished() {

final Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
if (fragment instanceof TCDFragment) {
final TCDFragment frag = (TCDFragment) fragment;
if (!syncingTCD) {
syncingTCD = true;
TCDContent.TCDLoader loader = new TCDContent.TCDLoader() {
@Override
public void finished(int result) {
if (result == 0) {
frag.update();
syncingTCD = false;
}
}
};

loader.execute(MainActivity.context.getApplicationContext());
}
};
loader.execute(MainActivity.context.getApplicationContext());
Toast.makeText(this, "Syncing", Toast.LENGTH_SHORT).show();
}
}
return super.onOptionsItemSelected(item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected void onStart() {
final long timeStart = (new Date()).getTime();
(new TCDContent.TCDLoader() {
@Override
public void finished() {
public void finished(int result) {
long timeEnd = (new Date()).getTime();
long timeTaken = Math.abs(timeEnd - timeStart);
if(timeTaken < 4000) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public void onAttach(Activity activity) {
}
}

public void update(){
mAdapter.notifyDataSetChanged();
}

@Override
public void onDetach() {
super.onDetach();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
Expand All @@ -17,8 +18,8 @@
import java.util.Locale;

public class TCDSingleViewer extends ActionBarActivity implements ViewPager.OnPageChangeListener {
private static final String DEBUG_TAG = "TCD_Activity";
private static int pages;
private ShareActionProvider mShareActionProvider;
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
Expand All @@ -28,12 +29,13 @@ public class TCDSingleViewer extends ActionBarActivity implements ViewPager.OnPa
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
private ShareActionProvider mShareActionProvider;
private int activePage;
private GestureDetectorCompat gestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -42,18 +44,19 @@ protected void onCreate(Bundle savedInstanceState) {
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

pages = TCDContent.ITEMS.size();
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
activePage = getIntent().getExtras().getInt("open");
mViewPager.setCurrentItem(activePage);
mViewPager.requestDisallowInterceptTouchEvent(false);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tcdsingle_viewer, menu);

Expand All @@ -65,15 +68,15 @@ public boolean onCreateOptionsMenu(Menu menu) {
}

private Intent getDefaultIntent() {
Toast.makeText(getApplicationContext(), "Creating Intent with "+activePage, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Creating Intent with " + activePage, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_SEND);
TCDContent.TCDQuestion q = TCDContent.ITEM_MAP.get(TCDContent.ITEMS.get(this.activePage).id);
if (q != null)
{
if (q != null) {
intent.putExtra(Intent.EXTRA_SUBJECT, "Techno Coup D'œil #" + q.id + " - " + q.title);
intent.putExtra(Intent.EXTRA_TITLE, "Techno Coup D'œil #" + q.id + " - " + q.title);
// intent.putExtra(Intent.EXTRA_TEXT, "Hey! Check out this. " + q.facebook + "\nFollow Technothlon for more.");
intent.putExtra(Intent.EXTRA_TEXT, "Hey! Check out this Technothlon's question.\n" + q.question);
intent.putExtra(Intent.EXTRA_TEXT, "Hey! Check out this Technothlon's question.\n" + q.question + "\n\n" +
"Follow Technothlon - \nfb.com/technothlon.techniche\ngoogle.com/+technothlon\ntechnothlon.techniche.org");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, R.drawable.ic_launcher);
}
intent.setType("text/*");
Expand All @@ -85,10 +88,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()){
case R.id.action_settings: return true;
case R.id.action_share: mShareActionProvider.setShareIntent(getDefaultIntent()); return false;
default: return super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.action_share:
mShareActionProvider.setShareIntent(getDefaultIntent());
return false;
default:
return super.onOptionsItemSelected(item);
}
}

Expand All @@ -107,39 +114,6 @@ public void onPageScrollStateChanged(int i) {

}


/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance((position));
}

@Override
public int getCount() {
// Show 3 total pages.
return pages;
}

@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
TCDContent.TCDQuestion q = TCDContent.ITEM_MAP.get(TCDContent.ITEMS.get(position).id);
String title = "#"+q.id+" - "+q.title;
return title;
}
}

/**
* A placeholder fragment containing a simple view.
*/
Expand All @@ -150,6 +124,9 @@ public static class PlaceholderFragment extends Fragment {
*/
private static final String ARG_SECTION_NUMBER = "section_number";

public PlaceholderFragment() {
}

/**
* Returns a new instance of this fragment for the given section
* number.
Expand All @@ -162,14 +139,9 @@ public static PlaceholderFragment newInstance(int sectionNumber) {
return fragment;
}

public PlaceholderFragment() {
}

// TODO create view here

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tcdsingle_viewer, container, false);
TextView id = (TextView) rootView.findViewById(R.id.tcd_id),
title = (TextView) rootView.findViewById(R.id.tcd_title),
Expand All @@ -178,7 +150,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
status = (TextView) rootView.findViewById(R.id.tcd_status);
int position = getArguments().getInt(ARG_SECTION_NUMBER);
TCDContent.TCDQuestion q = TCDContent.ITEM_MAP.get(TCDContent.ITEMS.get(position).id);
id.setText("#"+q.id);
id.setText("#" + q.id);
id.setBackgroundResource(q.color);
title.setText(q.title);
question.setText(q.question);
Expand All @@ -188,4 +160,36 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
}
}

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance((position));
}

@Override
public int getCount() {
// Show 3 total pages.
return pages;
}

@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
TCDContent.TCDQuestion q = TCDContent.ITEM_MAP.get(TCDContent.ITEMS.get(position).id);
String title = "#" + q.id + " - " + q.title;
return title;
}
}

}
Loading

0 comments on commit bc696cb

Please sign in to comment.