Search in sources :

Example 1 with CommentsFetcher

use of com.dar.nclientv2.api.comments.CommentsFetcher in project NClientV2 by Dar9586.

the class CommentActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Global.initActivity(this);
    setContentView(R.layout.activity_comment);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setTitle(R.string.comments);
    findViewById(R.id.page_switcher).setVisibility(View.GONE);
    int id = getIntent().getIntExtra(getPackageName() + ".GALLERYID", -1);
    if (id == -1) {
        finish();
        return;
    }
    recycler = findViewById(R.id.recycler);
    refresher = findViewById(R.id.refresher);
    refresher.setOnRefreshListener(() -> new CommentsFetcher(CommentActivity.this, id).start());
    EditText commentText = findViewById(R.id.commentText);
    findViewById(R.id.card).setVisibility(Login.isLogged() ? View.VISIBLE : View.GONE);
    findViewById(R.id.sendButton).setOnClickListener(v -> {
        if (commentText.getText().toString().length() < MINIUM_MESSAGE_LENGHT) {
            Toast.makeText(this, getString(R.string.minimum_comment_length, MINIUM_MESSAGE_LENGHT), Toast.LENGTH_SHORT).show();
            return;
        }
        String refererUrl = String.format(Locale.US, Utility.getBaseUrl() + "g/%d/", id);
        String submitUrl = String.format(Locale.US, Utility.getBaseUrl() + "api/gallery/%d/comments/submit", id);
        String requestString = createRequestString(commentText.getText().toString());
        commentText.setText("");
        RequestBody body = RequestBody.create(MediaType.get("application/json"), requestString);
        new AuthRequest(refererUrl, submitUrl, new Callback() {

            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                JsonReader reader = new JsonReader(response.body().charStream());
                Comment comment = null;
                reader.beginObject();
                while (reader.peek() != JsonToken.END_OBJECT) {
                    if ("comment".equals(reader.nextName())) {
                        comment = new Comment(reader);
                    } else {
                        reader.skipValue();
                    }
                }
                reader.close();
                if (comment != null && adapter != null)
                    adapter.addComment(comment);
            }
        }).setMethod("POST", body).start();
    });
    changeLayout(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
    recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    refresher.setRefreshing(true);
    new CommentsFetcher(CommentActivity.this, id).start();
}
Also used : EditText(android.widget.EditText) AuthRequest(com.dar.nclientv2.settings.AuthRequest) Call(okhttp3.Call) Comment(com.dar.nclientv2.api.comments.Comment) CommentsFetcher(com.dar.nclientv2.api.comments.CommentsFetcher) IOException(java.io.IOException) DividerItemDecoration(androidx.recyclerview.widget.DividerItemDecoration) Response(okhttp3.Response) Callback(okhttp3.Callback) JsonReader(android.util.JsonReader) Toolbar(androidx.appcompat.widget.Toolbar) RequestBody(okhttp3.RequestBody)

Aggregations

JsonReader (android.util.JsonReader)1 EditText (android.widget.EditText)1 Toolbar (androidx.appcompat.widget.Toolbar)1 DividerItemDecoration (androidx.recyclerview.widget.DividerItemDecoration)1 Comment (com.dar.nclientv2.api.comments.Comment)1 CommentsFetcher (com.dar.nclientv2.api.comments.CommentsFetcher)1 AuthRequest (com.dar.nclientv2.settings.AuthRequest)1 IOException (java.io.IOException)1 Call (okhttp3.Call)1 Callback (okhttp3.Callback)1 RequestBody (okhttp3.RequestBody)1 Response (okhttp3.Response)1