Mercury-IM/app/src/main/java/org/mercury_im/messenger/ui/login/AccountsFragment.java

107 lines
3.5 KiB
Java
Raw Normal View History

2019-06-03 01:16:49 +02:00
package org.mercury_im.messenger.ui.login;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.mercury_im.messenger.MercuryImApplication;
import org.mercury_im.messenger.R;
import org.mercury_im.messenger.persistence.model.AccountModel;
/**
* A fragment representing a list of Items.
* <p/>
2019-08-20 20:14:57 +02:00
* Activities containing this fragment MUST implement the {@link OnAccountListItemClickListener}
2019-06-03 01:16:49 +02:00
* interface.
*/
public class AccountsFragment extends Fragment {
2019-08-24 23:06:06 +02:00
private OnAccountListItemClickListener accountClickListener;
2019-06-03 01:16:49 +02:00
AccountsViewModel viewModel;
private AccountsRecyclerViewAdapter adapter;
2019-07-03 00:56:55 +02:00
private RecyclerView recyclerView;
2019-06-03 01:16:49 +02:00
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public AccountsFragment() {
}
// TODO: Customize parameter initialization
public static AccountsFragment newInstance() {
AccountsFragment fragment = new AccountsFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(MercuryImApplication.TAG, "AccountsFragment.onCreateView");
View view = inflater.inflate(R.layout.fragment_account_list, container, false);
viewModel = ViewModelProviders.of(this).get(AccountsViewModel.class);
// Set the adapter
if (view instanceof RecyclerView) {
2019-07-03 00:56:55 +02:00
recyclerView = (RecyclerView) view;
2019-06-03 01:16:49 +02:00
Context context = view.getContext();
recyclerView.setLayoutManager(new LinearLayoutManager(context));
this.adapter = new AccountsRecyclerViewAdapter(viewModel, accountClickListener);
2019-06-03 01:16:49 +02:00
viewModel.getAccounts().observe(this, roomAccountModels -> adapter.setValues(roomAccountModels));
recyclerView.setAdapter(adapter);
}
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
2019-08-20 20:14:57 +02:00
if (context instanceof OnAccountListItemClickListener) {
2019-08-24 23:06:06 +02:00
accountClickListener = (OnAccountListItemClickListener) context;
2019-06-03 01:16:49 +02:00
} else {
throw new RuntimeException(context.toString()
2019-08-20 20:14:57 +02:00
+ " must implement OnAccountListItemClickListener");
2019-06-03 01:16:49 +02:00
}
}
@Override
public void onDetach() {
super.onDetach();
2019-08-24 23:06:06 +02:00
accountClickListener = null;
2019-06-03 01:16:49 +02:00
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
2019-08-20 20:14:57 +02:00
public interface OnAccountListItemClickListener {
void onAccountListItemClick(AccountModel item);
2019-08-24 23:06:06 +02:00
void onAccountListItemLongClick(AccountModel item);
2019-06-03 01:16:49 +02:00
}
}