2019-06-03 01:16:49 +02:00
|
|
|
package org.mercury_im.messenger.ui.login;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2019-10-12 17:04:02 +02:00
|
|
|
import android.content.Intent;
|
2019-06-03 01:16:49 +02:00
|
|
|
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;
|
2019-10-12 17:04:02 +02:00
|
|
|
import androidx.lifecycle.ViewModelProvider;
|
2019-06-03 01:16:49 +02:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2019-10-12 17:04:02 +02:00
|
|
|
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton;
|
|
|
|
|
2019-06-03 01:16:49 +02:00
|
|
|
import org.mercury_im.messenger.MercuryImApplication;
|
|
|
|
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
|
|
|
|
2019-10-12 17:04:02 +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;
|
|
|
|
|
2019-10-12 17:04:02 +02:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
|
|
|
View view = inflater.inflate(R.layout.fragment_account_list, container, false);
|
2019-10-12 17:04:02 +02:00
|
|
|
ButterKnife.bind(this, view);
|
|
|
|
viewModel = new ViewModelProvider(this).get(AccountsViewModel.class);
|
2019-06-03 01:16:49 +02:00
|
|
|
|
|
|
|
// Set the adapter
|
2019-10-12 17:04:02 +02:00
|
|
|
Context context = view.getContext();
|
|
|
|
|
|
|
|
fab.setOnClickListener(v -> startActivity(new Intent(context, LoginActivity.class)));
|
|
|
|
recyclerView.setLayoutManager(new LinearLayoutManager(context));
|
|
|
|
this.adapter = new AccountsRecyclerViewAdapter(viewModel, accountClickListener);
|
|
|
|
viewModel.getAccounts().observe(this, roomAccountModels -> adapter.setValues(roomAccountModels));
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|