Skip to content

Commit

Permalink
Fix lint error (fragment cannot init)
Browse files Browse the repository at this point in the history
  • Loading branch information
daohoangson committed Feb 23, 2017
1 parent dd25c58 commit 791fa63
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 44 deletions.
62 changes: 20 additions & 42 deletions app/src/main/java/com/xfrocks/api/androiddemo/RegisterActivity.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
package com.xfrocks.api.androiddemo;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import com.xfrocks.api.androiddemo.common.DatePickerDialogFragment;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.Locale;

public class RegisterActivity extends AppCompatActivity
implements TfaDialogFragment.TfaDialogListener {
implements TfaDialogFragment.TfaDialogListener,
DatePickerDialogFragment.Listener {

public static final String EXTRA_USER = "user";
public static final String RESULT_EXTRA_ACCESS_TOKEN = "access_token";
static final int DATE_PICKER_RC_DOB = 1;

private EditText mEmailView;
private EditText mUsernameView;
Expand Down Expand Up @@ -318,34 +316,8 @@ private void setViewsEnabled(boolean enabled) {
}

private void showDatePicker() {
DialogFragment newFragment = new DialogFragment() {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

if (mDobYear != null
&& mDobMonth != null
&& mDobDay != null) {
year = mDobYear;
month = mDobMonth - 1;
day = mDobDay;
}

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
setDate(year, month + 1, day);
mDobView.selectAll();
}
}, year, month, day);
}
};
newFragment.show(getSupportFragmentManager(), "DatePickerDialog");
DatePickerDialogFragment dpdf = DatePickerDialogFragment.newInstance(DATE_PICKER_RC_DOB, mDobYear, mDobMonth, mDobDay);
dpdf.show(getSupportFragmentManager(), "DatePickerDialog");
}

private void setDate(int year, int month, int day) {
Expand All @@ -356,11 +328,17 @@ private void setDate(int year, int month, int day) {
mDobView.setText(String.format(Locale.US, "%04d-%02d-%02d", year, month, day));
}

private class RegisterRequest extends Api.PostRequest {
public RegisterRequest(String username,
String email,
String password,
int dobYear, int dobMonth, int dobDay) {
@Override
public void onDatePickerDialogFragmentNewDate(int year, int monthFromOne, int day) {
setDate(year, monthFromOne, day);
mDobView.selectAll();
}

class RegisterRequest extends Api.PostRequest {
RegisterRequest(String username,
String email,
String password,
int dobYear, int dobMonth, int dobDay) {
super(Api.URL_USERS, new Api.Params(Api.URL_USERS_PARAM_USERNAME, username)
.and(Api.URL_USERS_PARAM_EMAIL, email)
.and(Api.URL_USERS_PARAM_PASSWORD, password)
Expand Down Expand Up @@ -410,8 +388,8 @@ protected void onComplete() {
}
}

private class AssociateRequest extends Api.PostRequest {
public AssociateRequest(int userId, String password, String tfaProviderId, String tfaProviderCode) {
class AssociateRequest extends Api.PostRequest {
AssociateRequest(int userId, String password, String tfaProviderId, String tfaProviderCode) {
super(Api.URL_OAUTH_TOKEN_ASSOCIATE, new Api.Params(Api.URL_USERS_PARAM_USER_ID, userId)
.and(Api.URL_USERS_PARAM_PASSWORD, password)
.and(Api.URL_USERS_PARAM_EXTRA_DATA, mExtraData)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.xfrocks.api.androiddemo.common;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;

import java.util.Calendar;

public class DatePickerDialogFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

static final String ARG_REQUEST_CODE = "request_code";
static final String ARG_YEAR = "year";
static final String ARG_MONTH = "month";
static final String ARG_DAY = "day";

public static DatePickerDialogFragment newInstance(int requestCode, Integer year, Integer monthFromOne, Integer day) {
DatePickerDialogFragment fragment = new DatePickerDialogFragment();

Bundle args = new Bundle();
args.putInt(ARG_REQUEST_CODE, requestCode);
if (year != null) {
args.putInt(ARG_YEAR, year);
}
if (monthFromOne != null) {
args.putInt(ARG_MONTH, monthFromOne);
}
if (day != null) {
args.putInt(ARG_DAY, day);
}
fragment.setArguments(args);

return fragment;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int monthFromZero = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

Integer argYear = null;
Integer argMonth = null;
Integer argDay = null;
Bundle args = getArguments();
if (args.containsKey(ARG_YEAR)) {
argYear = args.getInt(ARG_YEAR);
}
if (args.containsKey(ARG_MONTH)) {
argMonth = args.getInt(ARG_MONTH);
}
if (args.containsKey(ARG_DAY)) {
argDay = args.getInt(ARG_DAY);
}

if (argYear != null && argMonth != null && argDay != null) {
year = argYear;
monthFromZero = argMonth - 1;
day = argDay;
}

return new DatePickerDialog(getContext(), this, year, monthFromZero, day);
}

@Override
public void onDateSet(DatePicker datePicker, int year, int monthFromZero, int day) {
Activity activity = getActivity();
if (activity == null) {
return;
}

if (!(activity instanceof Listener)) {
return;
}
Listener listener = (Listener) activity;

listener.onDatePickerDialogFragmentNewDate(year, monthFromZero + 1, day);
}

public interface Listener {
void onDatePickerDialogFragmentNewDate(int year, int monthFromOne, int day);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.xfrocks.api.androiddemo.discussion;

import android.os.Bundle;

import com.xfrocks.api.androiddemo.Api;

import org.json.JSONArray;
Expand Down

0 comments on commit 791fa63

Please sign in to comment.