ProductFragment.java 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 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.
 */

17
package com.example.android.persistence.ui;
18 19 20 21 22 23

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

I
Ian Lake 已提交
24
import androidx.annotation.NonNull;
25 26 27 28 29
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

30
import com.example.android.persistence.R;
31 32 33
import com.example.android.persistence.databinding.ProductFragmentBinding;
import com.example.android.persistence.viewmodel.ProductViewModel;

34
public class ProductFragment extends Fragment {
35 36 37 38 39 40 41 42 43

    private static final String KEY_PRODUCT_ID = "product_id";

    private ProductFragmentBinding mBinding;

    private CommentAdapter mCommentAdapter;

    @Nullable
    @Override
I
Ian Lake 已提交
44
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
45 46 47 48 49 50 51 52 53 54
            @Nullable Bundle savedInstanceState) {
        // Inflate this data binding layout
        mBinding = DataBindingUtil.inflate(inflater, R.layout.product_fragment, container, false);

        // Create and set the adapter for the RecyclerView.
        mCommentAdapter = new CommentAdapter(mCommentClickCallback);
        mBinding.commentList.setAdapter(mCommentAdapter);
        return mBinding.getRoot();
    }

I
Ian Lake 已提交
55 56
    private final CommentClickCallback mCommentClickCallback = comment -> {
        // no-op
57 58 59
    };

    @Override
60
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
61
        ProductViewModel.Factory factory = new ProductViewModel.Factory(
I
Ian Lake 已提交
62
                requireActivity().getApplication(), requireArguments().getInt(KEY_PRODUCT_ID));
63

64
        final ProductViewModel model = new ViewModelProvider(this, factory)
65 66 67 68 69 70 71 72 73 74
                .get(ProductViewModel.class);

        mBinding.setProductViewModel(model);

        subscribeToModel(model);
    }

    private void subscribeToModel(final ProductViewModel model) {

        // Observe product data
I
Ian Lake 已提交
75
        model.getObservableProduct().observe(getViewLifecycleOwner(), model::setProduct);
76 77

        // Observe comments
I
Ian Lake 已提交
78 79 80 81 82 83
        model.getComments().observe(getViewLifecycleOwner(), commentEntities -> {
            if (commentEntities != null) {
                mBinding.setIsLoading(false);
                mCommentAdapter.setCommentList(commentEntities);
            } else {
                mBinding.setIsLoading(true);
84 85 86 87
            }
        });
    }

88 89 90 91 92 93 94
    @Override
    public void onDestroyView() {
        mBinding = null;
        mCommentAdapter = null;
        super.onDestroyView();
    }

95 96 97 98 99 100 101 102 103
    /** Creates product fragment for specific product ID */
    public static ProductFragment forProduct(int productId) {
        ProductFragment fragment = new ProductFragment();
        Bundle args = new Bundle();
        args.putInt(KEY_PRODUCT_ID, productId);
        fragment.setArguments(args);
        return fragment;
    }
}