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

112 lines
3.3 KiB
Java
Raw Normal View History

2019-12-21 03:46:17 +01:00
package org.mercury_im.messenger.ui.account;
2019-06-03 01:16:49 +02:00
import android.content.Context;
import android.content.Intent;
2019-06-03 01:16:49 +02:00
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
2019-06-03 01:16:49 +02:00
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton;
2019-06-03 01:16:49 +02:00
import org.mercury_im.messenger.R;
2019-12-06 15:52:50 +01:00
import org.mercury_im.messenger.entity.Account;
2019-06-03 01:16:49 +02:00
import butterknife.BindView;
import butterknife.ButterKnife;
2019-06-03 01:16:49 +02:00
/**
* 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;
@BindView(R.id.list)
RecyclerView recyclerView;
@BindView(R.id.fab)
ExtendedFloatingActionButton fab;
2019-07-03 00:56:55 +02:00
2019-06-03 01:16:49 +02:00
public AccountsFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2019-12-21 01:45:30 +01:00
viewModel = new ViewModelProvider(this).get(AccountsViewModel.class);
this.adapter = new AccountsRecyclerViewAdapter(viewModel, accountClickListener);
}
@Override
public void onResume() {
super.onResume();
2019-12-21 04:08:59 +01:00
observeViewModel();
}
private void observeViewModel() {
viewModel.getConnections().observe(this, adapter::setValues);
2019-06-03 01:16:49 +02:00
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_account_list, container, false);
ButterKnife.bind(this, view);
2019-06-03 01:16:49 +02:00
Context context = view.getContext();
fab.setOnClickListener(v -> startActivity(new Intent(context, LoginActivity.class)));
recyclerView.setLayoutManager(new LinearLayoutManager(context));
2019-12-21 01:45:30 +01:00
recyclerView.setAdapter(adapter);
2019-06-03 01:16:49 +02:00
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 {
2019-12-06 15:52:50 +01:00
void onAccountListItemClick(Account item);
2019-08-24 23:06:06 +02:00
2019-12-06 15:52:50 +01:00
void onAccountListItemLongClick(Account item);
2019-06-03 01:16:49 +02:00
}
}