use of androidx.lifecycle.ViewModelProvider in project firebase-android-sdk by firebase.
the class HomeFragment method onCreateView.
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
homeViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
RecyclerView numbersList = root.findViewById(R.id.rv_numbers_home);
numbersList.setLayoutManager(new LinearLayoutManager(requireContext()));
numbersList.setHasFixedSize(true);
numbersList.setAdapter(new SlowListAdapter(NUM_LIST_ITEMS));
return root;
}
use of androidx.lifecycle.ViewModelProvider in project firebase-android-sdk by firebase.
the class SlowFragment method onCreateView.
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
slowViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(SlowViewModel.class);
View root = inflater.inflate(R.layout.fragment_slow, container, false);
TextView textView = root.findViewById(R.id.text_slow);
slowViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
RecyclerView numbersList = root.findViewById(R.id.rv_numbers_slow);
numbersList.setLayoutManager(new LinearLayoutManager(requireContext()));
numbersList.setHasFixedSize(true);
numbersList.setAdapter(new SlowListAdapter(NUM_LIST_ITEMS));
return root;
}
use of androidx.lifecycle.ViewModelProvider in project collect by getodk.
the class FormMapActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DaggerUtils.getComponent(this).inject(this);
setContentView(R.layout.form_map_activity);
Form form = loadForm();
selectionMapViewModel = new ViewModelProvider(this).get(SelectionMapViewModel.class);
selectionMapViewModel.setMapTitle(form.getDisplayName());
FormNavigator formNavigator = new FormNavigator(currentProjectProvider, settingsProvider, instancesRepositoryProvider::get);
getSupportFragmentManager().setFragmentResultListener(SelectionMapFragment.REQUEST_SELECT_ITEM, this, (requestKey, result) -> {
if (result.containsKey(SelectionMapFragment.RESULT_SELECTED_ITEM)) {
long instanceId = result.getLong(SelectionMapFragment.RESULT_SELECTED_ITEM);
formNavigator.editInstance(this, instanceId);
} else if (result.containsKey(SelectionMapFragment.RESULT_CREATE_NEW_ITEM)) {
formNavigator.newInstance(this, form.getDbId());
}
});
}
use of androidx.lifecycle.ViewModelProvider in project collect by getodk.
the class MainMenuActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
new ThemeUtils(this).setDarkModeForCurrentProject();
super.onCreate(savedInstanceState);
DaggerUtils.getComponent(this).inject(this);
setContentView(R.layout.main_menu);
settingsProvider.getMetaSettings().save(MetaKeys.FIRST_LAUNCH, false);
mainMenuViewModel = new ViewModelProvider(this, viewModelFactory).get(MainMenuViewModel.class);
currentProjectViewModel = new ViewModelProvider(this, currentProjectViewModelFactory).get(CurrentProjectViewModel.class);
currentProjectViewModel.getCurrentProject().observe(this, project -> {
invalidateOptionsMenu();
setTitle(project.getName());
});
initToolbar();
// enter data button. expects a result.
Button enterDataButton = findViewById(R.id.enter_data);
enterDataButton.setText(getString(R.string.enter_data_button));
enterDataButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), FillBlankFormActivity.class);
startActivity(i);
}
});
// review data button. expects a result.
reviewDataButton = findViewById(R.id.review_data);
reviewDataButton.setText(getString(R.string.review_data_button));
reviewDataButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), InstanceChooserList.class);
i.putExtra(ApplicationConstants.BundleKeys.FORM_MODE, ApplicationConstants.FormModes.EDIT_SAVED);
startActivity(i);
}
});
// send data button. expects a result.
sendDataButton = findViewById(R.id.send_data);
sendDataButton.setText(getString(R.string.send_data_button));
sendDataButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), InstanceUploaderListActivity.class);
startActivity(i);
}
});
// View sent forms
viewSentFormsButton = findViewById(R.id.view_sent_forms);
viewSentFormsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), InstanceChooserList.class);
i.putExtra(ApplicationConstants.BundleKeys.FORM_MODE, ApplicationConstants.FormModes.VIEW_SENT);
startActivity(i);
}
});
// manage forms button. no result expected.
getFormsButton = findViewById(R.id.get_forms);
getFormsButton.setText(getString(R.string.get_forms));
getFormsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String protocol = settingsProvider.getUnprotectedSettings().getString(ProjectKeys.KEY_PROTOCOL);
Intent i;
if (protocol.equalsIgnoreCase(ProjectKeys.PROTOCOL_GOOGLE_SHEETS)) {
if (new PlayServicesChecker().isGooglePlayServicesAvailable(MainMenuActivity.this)) {
i = new Intent(getApplicationContext(), GoogleDriveActivity.class);
} else {
new PlayServicesChecker().showGooglePlayServicesAvailabilityErrorDialog(MainMenuActivity.this);
return;
}
} else {
i = new Intent(getApplicationContext(), FormDownloadListActivity.class);
}
startActivity(i);
}
});
// manage forms button. no result expected.
manageFilesButton = findViewById(R.id.manage_forms);
manageFilesButton.setText(getString(R.string.manage_files));
manageFilesButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), DeleteSavedFormActivity.class);
startActivity(i);
}
});
TextView appName = findViewById(R.id.app_name);
appName.setText(String.format("%s %s", getString(R.string.collect_app_name), mainMenuViewModel.getVersion()));
TextView versionSHAView = findViewById(R.id.version_sha);
String versionSHA = mainMenuViewModel.getVersionCommitDescription();
if (versionSHA != null) {
versionSHAView.setText(versionSHA);
} else {
versionSHAView.setVisibility(View.GONE);
}
mainMenuViewModel.getSendableInstancesCount().observe(this, finalized -> {
if (finalized > 0) {
sendDataButton.setText(getString(R.string.send_data_button, String.valueOf(finalized)));
} else {
sendDataButton.setText(getString(R.string.send_data));
}
});
mainMenuViewModel.getEditableInstancesCount().observe(this, unsent -> {
if (unsent > 0) {
reviewDataButton.setText(getString(R.string.review_data_button, String.valueOf(unsent)));
} else {
reviewDataButton.setText(getString(R.string.review_data));
}
});
mainMenuViewModel.getSentInstancesCount().observe(this, sent -> {
if (sent > 0) {
viewSentFormsButton.setText(getString(R.string.view_sent_forms_button, String.valueOf(sent)));
} else {
viewSentFormsButton.setText(getString(R.string.view_sent_forms));
}
});
}
use of androidx.lifecycle.ViewModelProvider in project collect by getodk.
the class DrawActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.draw_layout);
DaggerUtils.getComponent(this).inject(this);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
PenColorPickerViewModel viewModel = new ViewModelProvider(this, factory).get(PenColorPickerViewModel.class);
viewModel.getPenColor().observe(this, penColor -> drawView.setColor(penColor));
fabActions = findViewById(R.id.fab_actions);
final FloatingActionButton fabSetColor = findViewById(R.id.fab_set_color);
final CardView cardViewSetColor = findViewById(R.id.cv_set_color);
final FloatingActionButton fabSaveAndClose = findViewById(R.id.fab_save_and_close);
final CardView cardViewSaveAndClose = findViewById(R.id.cv_save_and_close);
final FloatingActionButton fabClear = findViewById(R.id.fab_clear);
final CardView cardViewClear = findViewById(R.id.cv_clear);
fabActions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int status = Integer.parseInt(view.getTag().toString());
if (status == 0) {
status = 1;
fabActions.animate().rotation(45).setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(100).start();
AnimationUtils.scaleInAnimation(fabSetColor, 50, 150, new OvershootInterpolator(), true);
AnimationUtils.scaleInAnimation(cardViewSetColor, 50, 150, new OvershootInterpolator(), true);
AnimationUtils.scaleInAnimation(fabSaveAndClose, 100, 150, new OvershootInterpolator(), true);
AnimationUtils.scaleInAnimation(cardViewSaveAndClose, 100, 150, new OvershootInterpolator(), true);
AnimationUtils.scaleInAnimation(fabClear, 150, 150, new OvershootInterpolator(), true);
AnimationUtils.scaleInAnimation(cardViewClear, 150, 150, new OvershootInterpolator(), true);
fabSetColor.show();
cardViewSetColor.setVisibility(View.VISIBLE);
fabSaveAndClose.show();
cardViewSaveAndClose.setVisibility(View.VISIBLE);
fabClear.show();
cardViewClear.setVisibility(View.VISIBLE);
} else {
status = 0;
fabActions.animate().rotation(0).setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(100).start();
fabSetColor.hide();
cardViewSetColor.setVisibility(View.INVISIBLE);
fabSaveAndClose.hide();
cardViewSaveAndClose.setVisibility(View.INVISIBLE);
fabClear.hide();
cardViewClear.setVisibility(View.INVISIBLE);
}
view.setTag(status);
}
});
cardViewClear.setOnClickListener(this::clear);
fabClear.setOnClickListener(this::clear);
cardViewSaveAndClose.setOnClickListener(this::close);
fabSaveAndClose.setOnClickListener(this::close);
cardViewSetColor.setOnClickListener(this::setColor);
fabSetColor.setOnClickListener(this::setColor);
Bundle extras = getIntent().getExtras();
StoragePathProvider storagePathProvider = new StoragePathProvider();
if (extras == null) {
loadOption = OPTION_DRAW;
refImage = null;
savepointImage = new File(storagePathProvider.getTmpImageFilePath());
savepointImage.delete();
output = new File(storagePathProvider.getTmpImageFilePath());
} else {
if (extras.getInt(SCREEN_ORIENTATION) == 1) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
loadOption = extras.getString(OPTION);
if (loadOption == null) {
loadOption = OPTION_DRAW;
}
// refImage can also be present if resuming a drawing
Uri uri = (Uri) extras.get(REF_IMAGE);
if (uri != null) {
refImage = new File(uri.getPath());
}
String savepoint = extras.getString(SAVEPOINT_IMAGE);
if (savepoint != null) {
savepointImage = new File(savepoint);
if (!savepointImage.exists() && refImage != null && refImage.exists()) {
ImageFileUtils.copyImageAndApplyExifRotation(refImage, savepointImage);
}
} else {
savepointImage = new File(storagePathProvider.getTmpImageFilePath());
savepointImage.delete();
if (refImage != null && refImage.exists()) {
ImageFileUtils.copyImageAndApplyExifRotation(refImage, savepointImage);
}
}
uri = (Uri) extras.get(EXTRA_OUTPUT);
if (uri != null) {
output = new File(uri.getPath());
} else {
output = new File(storagePathProvider.getTmpImageFilePath());
}
}
if (OPTION_SIGNATURE.equals(loadOption)) {
alertTitleString = getString(R.string.quit_application, getString(R.string.sign_button));
} else if (OPTION_ANNOTATE.equals(loadOption)) {
alertTitleString = getString(R.string.quit_application, getString(R.string.markup_image));
} else {
alertTitleString = getString(R.string.quit_application, getString(R.string.draw_image));
}
drawView = findViewById(R.id.drawView);
drawView.setupView(OPTION_SIGNATURE.equals(loadOption));
}
Aggregations