Skip to content

Commit

Permalink
refector waited to be called buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhiruiLi committed Jul 13, 2017
1 parent e4f4486 commit df7102e
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 152 deletions.
8 changes: 4 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zhiruili.videoconf">

<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class CallActivity
public final class CallActivity
extends AppCompatActivity
implements CallFragment.OnFragmentInteractionListener {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.zhiruili.videoconf;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
Expand All @@ -10,7 +9,7 @@

import java.util.ArrayList;

public class CallFragment extends Fragment {
public final class CallFragment extends Fragment {

private static final String ARG_CALL_LIST = "call_list";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* 注册登录页
*/
public class LoginActivity extends AppCompatActivity {
public final class LoginActivity extends AppCompatActivity {

private static final String TAG = LoginActivity.class.getSimpleName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
/**
* 应用主界面
*/
public class MainActivity
public final class MainActivity
extends AppCompatActivity
implements RecentCallsFragment.OnFragmentInteractionListener, ILVCallNotificationListener, ILVIncomingListener, ILVCallListener {
implements
ToCallBufferFragment.OnFragmentInteractionListener,
RecentCallsFragment.OnFragmentInteractionListener,
ILVCallNotificationListener, ILVIncomingListener, ILVCallListener {

private static final String TAG = MainActivity.class.getSimpleName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,15 @@
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatImageButton;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.example.zhiruili.videoconf.utils.TextUtils;
import com.example.zhiruili.videoconf.utils.ViewCreator;
import com.google.android.flexbox.FlexboxLayout;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.view.RxViewGroup;
import com.jakewharton.rxbinding2.widget.RxTextView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.example.zhiruili.videoconf.utils.TextUtils.isUserNameValid;

public class RecentCallsFragment extends Fragment {
public final class RecentCallsFragment extends Fragment {

private OnFragmentInteractionListener mInteractionListener;

private FlexboxLayout mWaitedToCalledBuffer;
private EditText mCalledAccountEditText;
private AppCompatImageButton mAddCalledAccountButton;
private AppCompatButton mStartCallingButton;
private RecyclerView mRecentCallsList;

public RecentCallsFragment() { }
Expand All @@ -54,60 +34,13 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_recent_calls, container, false);
bindViews(rootView);
initViews();
return rootView;
}

private void bindViews(View rootView) {
mWaitedToCalledBuffer = (FlexboxLayout) rootView.findViewById(R.id.fbl_waited_to_called_buffer);
mCalledAccountEditText = (EditText) rootView.findViewById(R.id.et_called_account_input);
mAddCalledAccountButton = (AppCompatImageButton) rootView.findViewById(R.id.ib_add_called_account);
mStartCallingButton = (AppCompatButton) rootView.findViewById(R.id.btn_start_calling);
mRecentCallsList = (RecyclerView) rootView.findViewById(R.id.rv_recent_calls_list);
}

private void initViews() {

RxViewGroup
.changeEvents(mWaitedToCalledBuffer)
.map(ignore -> mWaitedToCalledBuffer.getFlexItemCount() > 0)
.subscribe(show -> mStartCallingButton.setVisibility(show ? View.VISIBLE : View.GONE));

RxTextView
.afterTextChangeEvents(mCalledAccountEditText)
.map(_ignore -> mCalledAccountEditText.getText().toString().trim())
.map(text -> text.length() > 0)
.subscribe(show -> mAddCalledAccountButton.setVisibility(show ? View.VISIBLE : View.GONE));

RxView
.clicks(mAddCalledAccountButton)
.throttleFirst(500, TimeUnit.MILLISECONDS)
.map(_ignore -> mCalledAccountEditText.getText().toString().trim())
.doOnNext(account -> {
if (!isUserNameValid(account)) {
mCalledAccountEditText.setError(getString(R.string.error_invalid_user_name));
}
})
.filter(TextUtils::isUserNameValid)
.map(account -> ViewCreator.createTag(getActivity(), account, account, mWaitedToCalledBuffer::removeView))
.doOnNext(_ignore -> mCalledAccountEditText.setText(""))
.subscribe(tag -> mWaitedToCalledBuffer.addView(tag));

RxView
.clicks(mStartCallingButton)
.throttleFirst(500, TimeUnit.MILLISECONDS)
.filter(_ignore -> mWaitedToCalledBuffer.getFlexItemCount() > 0)
.map(_ignore -> mWaitedToCalledBuffer.getChildCount())
.map(count ->
new ArrayList<String>() {{
for (int i = 0; i < count; ++i) {
add(mWaitedToCalledBuffer.getFlexItemAt(i).getTag().toString());
}
}}
)
.subscribe(mInteractionListener::startCalling);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
Expand All @@ -126,6 +59,5 @@ public void onDetach() {
}

public interface OnFragmentInteractionListener {
void startCalling(List<String> calledAccounts);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package com.example.zhiruili.videoconf;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatImageButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.example.zhiruili.videoconf.utils.TextUtils;
import com.example.zhiruili.videoconf.utils.ViewCreator;
import com.google.android.flexbox.FlexboxLayout;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.view.RxViewGroup;
import com.jakewharton.rxbinding2.widget.RxTextView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public final class ToCallBufferFragment extends Fragment {

private static final String ARG_INIT_ACCOUNTS = "init_accounts";

private FlexboxLayout mWaitedToCalledBuffer;
private EditText mCalledAccountEditText;
private AppCompatImageButton mAddCalledAccountButton;
private AppCompatButton mStartCallingButton;
private ArrayList<String> mAccounts;
private OnFragmentInteractionListener mInteractionListener;

public ToCallBufferFragment() { }

public static ToCallBufferFragment newInstance(ArrayList<String> initAccounts) {
ToCallBufferFragment fragment = new ToCallBufferFragment();
Bundle args = new Bundle();
args.putStringArrayList(ARG_INIT_ACCOUNTS, initAccounts);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mAccounts = getArguments().getStringArrayList(ARG_INIT_ACCOUNTS);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_to_call_buffer, container, false);
bindViews(rootView);
initViews();
addAccountsToBuffer(mAccounts);
return rootView;
}

public void addAccountToBuffer(@NonNull final String account) {
final int count = mWaitedToCalledBuffer.getFlexItemCount();
View tagView = null;
for (int i = 0; i < count; ++i) {
if (account.equals(mWaitedToCalledBuffer.getFlexItemAt(i).getTag().toString())) {
tagView = mWaitedToCalledBuffer.getFlexItemAt(i);
mWaitedToCalledBuffer.removeViewAt(i);
break;
}
}
if (tagView == null) {
tagView = ViewCreator.createTag(getActivity(), account, account, mWaitedToCalledBuffer::removeView);
}
mWaitedToCalledBuffer.addView(tagView);
}

private void addAccountsToBuffer(List<String> accounts) {
if (accounts == null) {
return;
}
accounts.forEach(this::addAccountToBuffer);
}

private void bindViews(View rootView) {
mWaitedToCalledBuffer = (FlexboxLayout) rootView.findViewById(R.id.waited_to_called_buffer);
mCalledAccountEditText = (EditText) rootView.findViewById(R.id.et_called_account_input);
mAddCalledAccountButton = (AppCompatImageButton) rootView.findViewById(R.id.ib_add_called_account);
mStartCallingButton = (AppCompatButton) rootView.findViewById(R.id.btn_start_calling);
}

private void initViews() {

RxViewGroup
.changeEvents(mWaitedToCalledBuffer)
.map(ignore -> mWaitedToCalledBuffer.getFlexItemCount() > 0)
.subscribe(show -> mStartCallingButton.setVisibility(show ? View.VISIBLE : View.GONE));

RxTextView
.afterTextChangeEvents(mCalledAccountEditText)
.map(_ignore -> mCalledAccountEditText.getText().toString().trim())
.map(text -> text.length() > 0)
.subscribe(show -> mAddCalledAccountButton.setVisibility(show ? View.VISIBLE : View.GONE));

RxView
.clicks(mAddCalledAccountButton)
.throttleFirst(500, TimeUnit.MILLISECONDS)
.map(_ignore -> mCalledAccountEditText.getText().toString().trim())
.doOnNext(account -> {
if (!TextUtils.isUserNameValid(account)) {
mCalledAccountEditText.setError(getString(R.string.error_invalid_user_name));
}
})
.filter(TextUtils::isUserNameValid)
.doOnNext(_ignore -> mCalledAccountEditText.setText(""))
.subscribe(this::addAccountToBuffer);

RxView
.clicks(mStartCallingButton)
.throttleFirst(500, TimeUnit.MILLISECONDS)
.filter(_ignore -> mWaitedToCalledBuffer.getFlexItemCount() > 0)
.map(_ignore -> mWaitedToCalledBuffer.getChildCount())
.map(count ->
new ArrayList<String>() {{
for (int i = 0; i < count; ++i) {
add(mWaitedToCalledBuffer.getFlexItemAt(i).getTag().toString());
}
}}
)
.subscribe(mInteractionListener::startCalling);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mInteractionListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mInteractionListener = null;
}

public interface OnFragmentInteractionListener {
void startCalling(List<String> calledAccounts);
}
}
14 changes: 13 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,26 @@
android:orientation="vertical"
tools:context=".MainActivity">

<fragment
android:id="@+id/fragment_to_call_buffer"
android:name="com.example.zhiruili.videoconf.ToCallBufferFragment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_width="0dp"
android:layout_height="wrap_content" />

<FrameLayout
android:id="@+id/content_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toBottomOf="@+id/fragment_to_call_buffer" />

<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
Expand Down
Loading

0 comments on commit df7102e

Please sign in to comment.