package com.codetroopers.betterpickers.sample.activity.recurrencepicker;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.text.format.Time;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.codetroopers.betterpickers.recurrencepicker.EventRecurrence;
import com.codetroopers.betterpickers.recurrencepicker.EventRecurrenceFormatter;
import com.codetroopers.betterpickers.recurrencepicker.RecurrencePickerDialogFragment;
import com.codetroopers.betterpickers.sample.R;
import com.codetroopers.betterpickers.sample.activity.BaseSampleActivity;
/**
* User: derek Date: 3/17/13 Time: 3:59 PM
*/
public class SampleRecurrenceBasicUsage extends BaseSampleActivity
implements RecurrencePickerDialogFragment.OnRecurrenceSetListener {
private static final String FRAG_TAG_RECUR_PICKER = "recurrencePickerDialogFragment";
private TextView mResultTextView;
private EventRecurrence mEventRecurrence = new EventRecurrence();
private String mRrule;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_and_button);
mResultTextView = (TextView) findViewById(R.id.text);
Button button = (Button) findViewById(R.id.button);
mResultTextView.setText(R.string.no_value);
button.setText(R.string.recurrence_picker_set);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getSupportFragmentManager();
Bundle bundle = new Bundle();
Time time = new Time();
time.setToNow();
bundle.putLong(RecurrencePickerDialogFragment.BUNDLE_START_TIME_MILLIS, time.toMillis(false));
bundle.putString(RecurrencePickerDialogFragment.BUNDLE_TIME_ZONE, time.timezone);
// may be more efficient to serialize and pass in EventRecurrence
bundle.putString(RecurrencePickerDialogFragment.BUNDLE_RRULE, mRrule);
RecurrencePickerDialogFragment rpd = (RecurrencePickerDialogFragment) fm.findFragmentByTag(
FRAG_TAG_RECUR_PICKER);
if (rpd != null) {
rpd.dismiss();
}
rpd = new RecurrencePickerDialogFragment();
rpd.setArguments(bundle);
rpd.setOnRecurrenceSetListener(SampleRecurrenceBasicUsage.this);
rpd.show(fm, FRAG_TAG_RECUR_PICKER);
}
});
}
@Override
public void onRecurrenceSet(String rrule) {
mRrule = rrule;
if (mRrule != null) {
mEventRecurrence.parse(mRrule);
}
populateRepeats();
}
@Override
public void onResume() {
// Example of reattaching to the fragment
super.onResume();
RecurrencePickerDialogFragment rpd = (RecurrencePickerDialogFragment) getSupportFragmentManager().findFragmentByTag(
FRAG_TAG_RECUR_PICKER);
if (rpd != null) {
rpd.setOnRecurrenceSetListener(this);
}
}
private void populateRepeats() {
Resources r = getResources();
String repeatString = "";
boolean enabled;
if (!TextUtils.isEmpty(mRrule)) {
repeatString = EventRecurrenceFormatter.getRepeatString(this, r, mEventRecurrence, true);
}
mResultTextView.setText(mRrule + "\n" + repeatString);
}
}