| /* |
| * Copyright (C) 2017 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package com.android.sample.githubbrowser; |
| |
| import android.app.Activity; |
| import android.app.Dialog; |
| import android.content.DialogInterface; |
| import android.content.Intent; |
| import android.databinding.DataBindingUtil; |
| import android.os.Bundle; |
| import android.support.annotation.NonNull; |
| import android.support.annotation.Nullable; |
| import android.support.v4.app.DialogFragment; |
| import android.support.v7.app.AlertDialog; |
| import android.view.LayoutInflater; |
| import android.view.ViewGroup; |
| import android.widget.EditText; |
| |
| import com.android.sample.githubbrowser.databinding.EditUserDetailsBinding; |
| import com.android.sample.githubbrowser.model.PersonDataModel; |
| import com.android.support.lifecycle.LifecycleRegistry; |
| import com.android.support.lifecycle.LifecycleRegistryProvider; |
| import com.android.support.lifecycle.Observer; |
| import com.android.support.lifecycle.ViewModelStore; |
| |
| /** |
| * Edit user details fragment. |
| */ |
| public class EditUserDetailsFragment extends DialogFragment implements LifecycleRegistryProvider { |
| public static final String LOGIN = "editUser.login"; |
| public static final String KEY_EMAIL = "edit.email"; |
| public static final String KEY_LOCATION = "edit.location"; |
| |
| private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this); |
| private PersonDataModel mPersonDataModel; |
| |
| public EditUserDetailsFragment() { |
| } |
| |
| @Override |
| public LifecycleRegistry getLifecycle() { |
| return mLifecycleRegistry; |
| } |
| |
| @NonNull |
| @Override |
| public Dialog onCreateDialog(Bundle savedInstanceState) { |
| final String login = getArguments().getString(LOGIN); |
| |
| // Configure the dialog to pass the data back when "OK" button is clicked |
| AlertDialog.Builder editBuilder = new AlertDialog.Builder(getContext()) |
| .setTitle("Edit details") |
| .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
| @Override |
| public void onClick(DialogInterface dialogInterface, int i) { |
| Intent edited = new Intent(); |
| edited.putExtra(KEY_EMAIL, getCurrentEmail()); |
| edited.putExtra(KEY_LOCATION, getCurrentLocation()); |
| getTargetFragment().onActivityResult(getTargetRequestCode(), |
| Activity.RESULT_OK, edited); } |
| }) |
| .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { |
| @Override |
| public void onClick(DialogInterface dialogInterface, int i) { |
| getTargetFragment().onActivityResult(getTargetRequestCode(), |
| Activity.RESULT_CANCELED, null); |
| } |
| }); |
| |
| // Inflate the main editor area and set it as custom view on the dialog |
| final LayoutInflater inflater = LayoutInflater.from(getContext()); |
| final EditUserDetailsBinding binding = DataBindingUtil.inflate( |
| inflater, R.layout.edit_user_details, null, false); |
| final ViewGroup editor = (ViewGroup) binding.getRoot(); |
| editBuilder.setView(editor); |
| final AlertDialog result = editBuilder.create(); |
| |
| // Get our view model instance and register ourselves to observe change to the |
| // user data. When a change is reported, update all UI elements based on the new |
| // data. |
| mPersonDataModel = ViewModelStore.get(this, login, PersonDataModel.class); |
| mPersonDataModel.getPersonData().observe(this, new Observer<PersonData>() { |
| @Override |
| public void onChanged(@Nullable PersonData personData) { |
| if (personData != null) { |
| android.util.Log.e("GithubBrowser", "Got data for editing from model"); |
| getDialog().setTitle(personData.name); |
| binding.setUser(personData); |
| binding.executePendingBindings(); |
| } |
| } |
| }); |
| |
| // Ask the model to load the data for this user. When the data becomes available (either |
| // immediately from the previous load or later on when it's fetched from remote API call), |
| // we will be notified since this fragment registered itself as an observer on the matching |
| // live data object. |
| mPersonDataModel.loadData(getContext(), login); |
| |
| return result; |
| } |
| |
| private String getCurrentEmail() { |
| EditText runtime = (EditText) getDialog().findViewById(R.id.email); |
| return runtime.getText().toString(); |
| } |
| |
| private String getCurrentLocation() { |
| EditText rated = (EditText) getDialog().findViewById(R.id.location); |
| return rated.getText().toString(); |
| } |
| } |