-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhiruili
committed
Jul 15, 2017
1 parent
22affea
commit 9fbc61f
Showing
16 changed files
with
372 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
app/src/main/java/com/example/zhiruili/videoconf/RecentCallsFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.example.zhiruili.videoconf; | ||
|
||
import android.content.ContentValues; | ||
import android.content.Context; | ||
import android.database.Cursor; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.os.Bundle; | ||
import android.support.v4.app.Fragment; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import com.example.zhiruili.videoconf.data.RecentCallsDbHelper; | ||
import com.example.zhiruili.videoconf.data.RecentCallsListContract.*; | ||
import com.example.zhiruili.videoconf.data.TestHelper; | ||
|
||
import java.util.List; | ||
|
||
import io.reactivex.Single; | ||
import io.reactivex.android.schedulers.AndroidSchedulers; | ||
import io.reactivex.schedulers.Schedulers; | ||
|
||
public class RecentCallsFragment extends Fragment { | ||
|
||
private OnFragmentInteractionListener mInteractionListener; | ||
private RecentCallsListAdapter mListAdapter = null; | ||
private SQLiteDatabase mDb; | ||
|
||
public RecentCallsFragment() { } | ||
|
||
@Override | ||
public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||
Bundle savedInstanceState) { | ||
final View rootView = inflater.inflate(R.layout.fragment_recent_calls, container, false); | ||
final RecyclerView callsListView = (RecyclerView) rootView.findViewById(R.id.rv_recent_calls_list); | ||
callsListView.setLayoutManager(new LinearLayoutManager(rootView.getContext())); | ||
|
||
Single | ||
.fromCallable(() -> new RecentCallsDbHelper(getActivity()).getWritableDatabase()) | ||
.doOnSuccess(db -> mDb = db) | ||
// .doOnSuccess(TestHelper::insertFakeData) | ||
.map(_ignore -> getAllRecentCalls()) | ||
.subscribeOn(Schedulers.io()) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.map(cursor -> new RecentCallsListAdapter(rootView.getContext(), cursor, mInteractionListener::onListItemClick)) | ||
.doOnSuccess(adapter -> mListAdapter = adapter) | ||
.doOnSuccess(callsListView::setAdapter) | ||
.doOnError(Throwable::printStackTrace) | ||
.subscribe(); | ||
|
||
return rootView; | ||
} | ||
|
||
@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; | ||
} | ||
|
||
@Override | ||
public void onDestroyView() { | ||
super.onDestroyView(); | ||
} | ||
|
||
public interface OnFragmentInteractionListener { | ||
void onListItemClick(String callId); | ||
} | ||
|
||
public Single<Boolean> updateCalls(List<String> callIds, boolean isCallIn) { | ||
return Single | ||
.fromCallable(() -> { | ||
mDb.beginTransaction(); | ||
for (String id : callIds) { | ||
mDb.delete(RecentCallsListEntry.TABLE_NAME, RecentCallsListEntry.COLUMN_PERSON_ID + "=?", new String[] { id }); | ||
} | ||
for (String id : callIds) { | ||
ContentValues cv = new ContentValues(); | ||
cv.put(RecentCallsListEntry.COLUMN_PERSON_ID, id); | ||
cv.put(RecentCallsListEntry.COLUMN_IS_CALL_IN, isCallIn); | ||
mDb.insert(RecentCallsListEntry.TABLE_NAME, null, cv); | ||
} | ||
mDb.setTransactionSuccessful(); | ||
return true; | ||
}) | ||
.doFinally(() -> mDb.endTransaction()) | ||
.map(_ignore -> getAllRecentCalls()) | ||
.doOnError(Throwable::printStackTrace) | ||
.subscribeOn(Schedulers.io()) | ||
.doOnSuccess(mListAdapter::swapCursor) | ||
.subscribeOn(AndroidSchedulers.mainThread()) | ||
.map(_ignore -> true) | ||
.onErrorReturn(err -> { | ||
err.printStackTrace(); | ||
return false; | ||
}); | ||
} | ||
|
||
public Cursor getAllRecentCalls() { | ||
return mDb.query( | ||
RecentCallsListEntry.TABLE_NAME, | ||
null, null, null, null, null, | ||
RecentCallsListEntry.COLUMN_CALL_TIME + " DESC"); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
app/src/main/java/com/example/zhiruili/videoconf/RecentCallsListAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.example.zhiruili.videoconf; | ||
|
||
import android.content.Context; | ||
import android.database.Cursor; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
|
||
import com.example.zhiruili.videoconf.data.RecentCallsListContract.*; | ||
|
||
public class RecentCallsListAdapter extends RecyclerView.Adapter<RecentCallsListAdapter.RecentCallViewHolder> { | ||
|
||
private Context mContext; | ||
private Cursor mCursor; | ||
private OnItemClickListener mClickListener; | ||
|
||
public RecentCallsListAdapter(Context context, Cursor cursor, OnItemClickListener listener) { | ||
mContext = context; | ||
mCursor = cursor; | ||
mClickListener = listener; | ||
} | ||
|
||
@Override | ||
public RecentCallViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | ||
LayoutInflater inflater = LayoutInflater.from(mContext); | ||
View view = inflater.inflate(R.layout.recent_calls_list_item, parent, false); | ||
return new RecentCallViewHolder(view); | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(RecentCallViewHolder holder, int position) { | ||
if (!mCursor.moveToPosition(position)) { | ||
return; | ||
} | ||
int idxCallId = mCursor.getColumnIndex(RecentCallsListEntry.COLUMN_PERSON_ID); | ||
String callId = mCursor.getString(idxCallId); | ||
int idxIsCallIn = mCursor.getColumnIndex(RecentCallsListEntry.COLUMN_IS_CALL_IN); | ||
boolean isCallIn = mCursor.getInt(idxIsCallIn) != 0; | ||
int idxCallTime = mCursor.getColumnIndex(RecentCallsListEntry.COLUMN_CALL_TIME); | ||
String callTimeString = mCursor.getString(idxCallTime); | ||
holder.callId.setText(callId); | ||
holder.callTime.setText(callTimeString); | ||
holder.callInOrOut.setText(isCallIn ? mContext.getString(R.string.label_call_in) : mContext.getString(R.string.label_call_out)); | ||
} | ||
|
||
void swapCursor(Cursor newCursor) { | ||
mCursor = newCursor; | ||
notifyDataSetChanged(); | ||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
return mCursor.getCount(); | ||
} | ||
|
||
class RecentCallViewHolder extends RecyclerView.ViewHolder { | ||
|
||
TextView callId; | ||
TextView callTime; | ||
TextView callInOrOut; | ||
|
||
public RecentCallViewHolder(View itemView) { | ||
super(itemView); | ||
callId = (TextView) itemView.findViewById(R.id.tv_call_id); | ||
callTime = (TextView) itemView.findViewById(R.id.tv_call_time); | ||
callInOrOut = (TextView) itemView.findViewById(R.id.tv_call_in_or_out); | ||
itemView.setOnClickListener(v -> mClickListener.onClick(callId.getText().toString())); | ||
} | ||
} | ||
|
||
public interface OnItemClickListener { | ||
void onClick(String callId); | ||
} | ||
} |
Oops, something went wrong.