Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zetbaitsu committed Mar 24, 2017
2 parents 6dc6d33 + 35c4b43 commit 27f256f
Show file tree
Hide file tree
Showing 15 changed files with 571 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ allprojects {
Then add to your app module build.gradle
```groovy
dependencies {
compile 'com.qiscus.sdk:chat:1.14.0'
compile 'com.qiscus.sdk:chat:1.15.0'
}
```
# Let's make cools chatting apps!
Expand Down Expand Up @@ -115,7 +115,7 @@ Qiscus.buildChatWith("[email protected]")
});
```

Check sample apps -> [DragonFly](https://github.com/qiscus/qiscus-sdk-android-sample)
Check sample apps -> [DragonGo](https://github.com/qiscus/qiscus-sdk-android-sample)

License
-------
Expand Down
2 changes: 1 addition & 1 deletion chat/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ext {
siteUrl = 'https://github.com/qiscus/qiscus-sdk-android'
gitUrl = 'https://github.com/qiscus/qiscus-sdk-android.git'

libraryVersion = '1.14.0'
libraryVersion = '1.15.0'

developerId = 'qiscustech'
developerName = 'Qiscus Tech'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ public interface QiscusCommentStore {

QiscusComment getComment(int id, String uniqueId);

List<QiscusComment> getComments(int topicId);

List<QiscusComment> getComments(int topicId, int count);

Observable<List<QiscusComment>> getObservableComments(int topicId);

Observable<List<QiscusComment>> getObservableComments(int topicId, int count);

List<QiscusComment> getOlderCommentsThan(QiscusComment qiscusComment, int topicId, int count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,27 @@ public QiscusComment getComment(int id, String uniqueId) {
}
}

@Override
public List<QiscusComment> getComments(int topicId) {
String query = "SELECT * FROM "
+ QiscusDb.CommentTable.TABLE_NAME + " WHERE "
+ QiscusDb.CommentTable.COLUMN_TOPIC_ID + " = " + topicId + " "
+ "ORDER BY " + QiscusDb.CommentTable.COLUMN_TIME + " DESC";
Cursor cursor = sqLiteDatabase.rawQuery(query, null);
List<QiscusComment> qiscusComments = new ArrayList<>();
while (cursor.moveToNext()) {
QiscusComment qiscusComment = QiscusDb.CommentTable.parseCursor(cursor);
QiscusRoomMember qiscusRoomMember = getMember(qiscusComment.getSenderEmail());
if (qiscusRoomMember != null) {
qiscusComment.setSender(qiscusRoomMember.getUsername());
qiscusComment.setSenderAvatar(qiscusRoomMember.getAvatar());
}
qiscusComments.add(qiscusComment);
}
cursor.close();
return qiscusComments;
}

@Override
public List<QiscusComment> getComments(int topicId, int count) {
String query = "SELECT * FROM "
Expand All @@ -544,6 +565,14 @@ public List<QiscusComment> getComments(int topicId, int count) {
return qiscusComments;
}

@Override
public Observable<List<QiscusComment>> getObservableComments(final int topicId) {
return Observable.create(subscriber -> {
subscriber.onNext(getComments(topicId));
subscriber.onCompleted();
}, Emitter.BackpressureMode.BUFFER);
}

@Override
public Observable<List<QiscusComment>> getObservableComments(final int topicId, final int count) {
return Observable.create(subscriber -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2016 Qiscus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.qiscus.sdk.presenter;

import android.support.v4.util.Pair;

import com.qiscus.sdk.Qiscus;
import com.qiscus.sdk.data.model.QiscusComment;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
* Created on : March 23, 2017
* Author : zetbaitsu
* Name : Zetra
* GitHub : https://github.com/zetbaitsu
*/
public class QiscusPhotoViewerPresenter extends QiscusPresenter<QiscusPhotoViewerPresenter.View> {

public QiscusPhotoViewerPresenter(View view) {
super(view);
}

public void loadQiscusPhotos(int topicId) {
view.showLoading();
Qiscus.getDataStore()
.getObservableComments(topicId)
.map(qiscusComments -> {
List<Pair<QiscusComment, File>> qiscusPhotos = new ArrayList<>();
for (QiscusComment qiscusComment : qiscusComments) {
if (qiscusComment.isImage()) {
File localPath = Qiscus.getDataStore().getLocalPath(qiscusComment.getId());
if (localPath != null) {
qiscusPhotos.add(Pair.create(qiscusComment, localPath));
}
}
}
Collections.reverse(qiscusPhotos);
return qiscusPhotos;
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(qiscusPhotos -> {
if (view != null) {
view.onLoadQiscusPhotos(qiscusPhotos);
view.dismissLoading();
}
}, throwable -> {
throwable.printStackTrace();
if (view != null) {
view.showError("Something went wrong!");
view.dismissLoading();
}
});
}

public interface View extends QiscusPresenter.View {
void onLoadQiscusPhotos(List<Pair<QiscusComment, File>> qiscusPhotos);
}
}
162 changes: 147 additions & 15 deletions chat/src/main/java/com/qiscus/sdk/ui/QiscusPhotoViewerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,54 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.util.Pair;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.qiscus.sdk.Qiscus;
import com.qiscus.sdk.R;
import com.qiscus.sdk.data.model.QiscusComment;
import com.qiscus.sdk.ui.view.QiscusTouchImageView;
import com.qiscus.sdk.presenter.QiscusPhotoViewerPresenter;
import com.qiscus.sdk.ui.adapter.QiscusPhotoPagerAdapter;
import com.qiscus.sdk.ui.fragment.QiscusPhotoFragment;
import com.qiscus.sdk.util.QiscusDateUtil;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
* Created on : March 04, 2017
* Author : zetbaitsu
* Name : Zetra
* GitHub : https://github.com/zetbaitsu
*/
public class QiscusPhotoViewerActivity extends RxAppCompatActivity {
public class QiscusPhotoViewerActivity extends RxAppCompatActivity implements QiscusPhotoViewerPresenter.View,
ViewPager.OnPageChangeListener, QiscusPhotoFragment.ClickListener {
private static final String EXTRA_COMMENT = "extra_comment";
private static final String KEY_POSITION = "last_position";

private Toolbar toolbar;
private TextView tvTitle;
private ViewPager viewPager;
private ProgressBar progressBar;
private TextView senderName;
private TextView date;
private View infoPanel;
private Animation fadein, fadeout;

private QiscusComment qiscusComment;
private int position = -1;
private List<Pair<QiscusComment, File>> qiscusPhotos;

public static Intent generateIntent(Context context, QiscusComment qiscusComment) {
Intent intent = new Intent(context, QiscusPhotoViewerActivity.class);
Expand All @@ -51,23 +78,47 @@ public static Intent generateIntent(Context context, QiscusComment qiscusComment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_qiscus_photo_viewer);

QiscusTouchImageView imageView = (QiscusTouchImageView) findViewById(R.id.image_view);
TextView senderName = (TextView) findViewById(R.id.sender_name);
TextView date = (TextView) findViewById(R.id.date);
toolbar = (Toolbar) findViewById(R.id.toolbar);
tvTitle = (TextView) findViewById(R.id.tv_title);
viewPager = (ViewPager) findViewById(R.id.view_pager);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
senderName = (TextView) findViewById(R.id.sender_name);
date = (TextView) findViewById(R.id.date);
ImageButton shareButton = (ImageButton) findViewById(R.id.action_share);
infoPanel = findViewById(R.id.info_panel);
fadein = AnimationUtils.loadAnimation(this, R.anim.fadein);
fadeout = AnimationUtils.loadAnimation(this, R.anim.fadeout);

findViewById(R.id.back).setOnClickListener(v -> onBackPressed());
setSupportActionBar(toolbar);
viewPager.addOnPageChangeListener(this);

resolveData(savedInstanceState);

QiscusPhotoViewerPresenter presenter = new QiscusPhotoViewerPresenter(this);
presenter.loadQiscusPhotos(qiscusComment.getTopicId());

QiscusComment qiscusComment = getIntent().getParcelableExtra(EXTRA_COMMENT);
File imageFile = Qiscus.getDataStore().getLocalPath(qiscusComment.getId());
Glide.with(this)
.load(imageFile)
.into(imageView);
shareButton.setOnClickListener(v -> {
Pair<QiscusComment, File> qiscusPhoto = qiscusPhotos.get(position);
shareImage(qiscusPhoto.second);
});

senderName.setText(qiscusComment.getSender());
date.setText(QiscusDateUtil.toFullDateFormat(qiscusComment.getTime()));
shareButton.setOnClickListener(v -> shareImage(imageFile));
}

private void resolveData(Bundle savedInstanceState) {
qiscusComment = getIntent().getParcelableExtra(EXTRA_COMMENT);
if (qiscusComment == null && savedInstanceState != null) {
qiscusComment = savedInstanceState.getParcelable(EXTRA_COMMENT);
}

if (qiscusComment == null) {
finish();
return;
}
}

private void shareImage(File imageFile) {
Expand All @@ -76,4 +127,85 @@ private void shareImage(File imageFile) {
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
startActivity(Intent.createChooser(intent, "Share"));
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(EXTRA_COMMENT, qiscusComment);
outState.putInt(KEY_POSITION, position);
}

@Override
public void showError(String errorMessage) {
Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
finish();
}

@Override
public void showLoading() {
progressBar.setVisibility(View.VISIBLE);
}

@Override
public void dismissLoading() {
progressBar.setVisibility(View.GONE);
}

@Override
public void onLoadQiscusPhotos(List<Pair<QiscusComment, File>> qiscusPhotos) {
this.qiscusPhotos = qiscusPhotos;
initPhotos();
}

private void initPhotos() {
List<QiscusPhotoFragment> fragments = new ArrayList<>();
for (int i = 0; i < qiscusPhotos.size(); i++) {
Pair<QiscusComment, File> qiscusPhoto = qiscusPhotos.get(i);
fragments.add(QiscusPhotoFragment.newInstance(qiscusPhoto.second));
if (position == -1 && qiscusPhoto.first.equals(qiscusComment)) {
position = i;
}
}
viewPager.setAdapter(new QiscusPhotoPagerAdapter(getSupportFragmentManager(), fragments));
viewPager.setCurrentItem(position);
bindInfo();
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
this.position = position;
bindInfo();
}

private void bindInfo() {
Pair<QiscusComment, File> qiscusPhoto = qiscusPhotos.get(position);
senderName.setText(qiscusPhoto.first.getSender());
date.setText(QiscusDateUtil.toFullDateFormat(qiscusPhoto.first.getTime()));
tvTitle.setText((position + 1) + " of " + qiscusPhotos.size());
}

@Override
public void onPageScrollStateChanged(int state) {

}

@Override
public void onPhotoClick() {
if (infoPanel.getVisibility() == View.VISIBLE) {
infoPanel.startAnimation(fadeout);
toolbar.startAnimation(fadeout);
infoPanel.setVisibility(View.GONE);
toolbar.setVisibility(View.GONE);
} else {
infoPanel.startAnimation(fadein);
toolbar.startAnimation(fadein);
infoPanel.setVisibility(View.VISIBLE);
toolbar.setVisibility(View.VISIBLE);
}
}
}
Loading

0 comments on commit 27f256f

Please sign in to comment.