Search in sources :

Example 1 with OnFileSelectedListener

use of net.iGap.module.FileListerDialog.OnFileSelectedListener in project iGap-Android by KianIranian-STDG.

the class FragmentChat method exportChat.

public void exportChat() {
    RealmResults<RealmRoomMessage> realmRoomMessages = DbManager.getInstance().doRealmTask(realm -> {
        return realm.where(RealmRoomMessage.class).equalTo("roomId", mRoomId).sort("createTime").findAll();
    });
    File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/iGap", "iGap Messages");
    if (!root.exists()) {
        root.mkdir();
    }
    FileListerDialog fileListerDialog = FileListerDialog.createFileListerDialog(G.fragmentActivity);
    fileListerDialog.setDefaultDir(root);
    fileListerDialog.setFileFilter(FileListerDialog.FILE_FILTER.DIRECTORY_ONLY);
    fileListerDialog.show();
    fileListerDialog.setOnFileSelectedListener(new OnFileSelectedListener() {

        @Override
        public void onFileSelected(File file, String path) {
            final MaterialDialog[] dialog = new MaterialDialog[1];
            if (realmRoomMessages.size() != 0 && chatType != CHANNEL) {
                G.handler.post(new Runnable() {

                    @Override
                    public void run() {
                        dialog[0] = new MaterialDialog.Builder(G.currentActivity).title(R.string.export_chat).content(R.string.just_wait_en).progress(false, realmRoomMessages.size(), true).show();
                    }
                });
                try {
                    File filepath = new File(file, title + ".txt");
                    FileWriter writer = new FileWriter(filepath);
                    for (RealmRoomMessage export : realmRoomMessages) {
                        if (export.getMessageType().toString().equalsIgnoreCase("TEXT")) {
                            writer.append(RealmRegisteredInfo.getNameWithId(export.getUserId()) + "  text message " + "  :  " + export.getMessage() + "  date  :" + HelperCalander.milladyDate(export.getCreateTime()) + "\n");
                        } else {
                            writer.append(RealmRegisteredInfo.getNameWithId(export.getUserId()) + "  text message " + export.getMessage() + "  :  message in format " + export.getMessageType() + "  date  :" + HelperCalander.milladyDate(export.getCreateTime()) + "\n");
                        }
                        G.handler.post(new Runnable() {

                            @Override
                            public void run() {
                                dialog[0].incrementProgress(1);
                            }
                        });
                    }
                    writer.flush();
                    writer.close();
                    G.handler.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            dialog[0].dismiss();
                        }
                    }, 500);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) FileListerDialog(net.iGap.module.FileListerDialog.FileListerDialog) OnFileSelectedListener(net.iGap.module.FileListerDialog.OnFileSelectedListener) FileWriter(java.io.FileWriter) RealmRoomMessage(net.iGap.realm.RealmRoomMessage) RealmString(net.iGap.realm.RealmString) HelperString(net.iGap.helper.HelperString) HelperSaveFile(net.iGap.helper.HelperSaveFile) File(java.io.File) AttachFile(net.iGap.module.AttachFile) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException)

Aggregations

MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 HelperSaveFile (net.iGap.helper.HelperSaveFile)1 HelperString (net.iGap.helper.HelperString)1 AttachFile (net.iGap.module.AttachFile)1 FileListerDialog (net.iGap.module.FileListerDialog.FileListerDialog)1 OnFileSelectedListener (net.iGap.module.FileListerDialog.OnFileSelectedListener)1 RealmRoomMessage (net.iGap.realm.RealmRoomMessage)1 RealmString (net.iGap.realm.RealmString)1