Search in sources :

Example 1 with AuthRequest

use of com.dar.nclientv2.settings.AuthRequest in project NClientV2 by Dar9586.

the class CommentAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(@NonNull CommentAdapter.ViewHolder holder, int pos) {
    int position = holder.getBindingAdapterPosition();
    Comment c = comments.get(position);
    holder.layout.setOnClickListener(v1 -> {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
            context.runOnUiThread(() -> holder.body.setMaxLines(holder.body.getMaxLines() == 7 ? 999 : 7));
        }
    });
    holder.close.setVisibility(c.getPosterId() != userId ? View.GONE : View.VISIBLE);
    holder.user.setText(c.getUsername());
    holder.body.setText(c.getComment());
    holder.date.setText(format.format(c.getPostDate()));
    holder.close.setOnClickListener(v -> {
        String refererUrl = String.format(Locale.US, Utility.getBaseUrl() + "g/%d/", galleryId);
        String submitUrl = String.format(Locale.US, Utility.getBaseUrl() + "api/comments/%d/delete", c.getId());
        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 {
                if (response.body().string().contains("true")) {
                    comments.remove(position);
                    context.runOnUiThread(() -> notifyItemRemoved(position));
                }
            }
        }).setMethod("POST", AuthRequest.EMPTY_BODY).start();
    });
    if (c.getAvatarUrl() == null || Global.getDownloadPolicy() != Global.DataUsageType.FULL)
        ImageDownloadUtility.loadImage(R.drawable.ic_person, holder.userImage);
    else
        ImageDownloadUtility.loadImage(context, c.getAvatarUrl(), holder.userImage);
}
Also used : Response(okhttp3.Response) Comment(com.dar.nclientv2.api.comments.Comment) AuthRequest(com.dar.nclientv2.settings.AuthRequest) Call(okhttp3.Call) Callback(okhttp3.Callback) IOException(java.io.IOException)

Example 2 with AuthRequest

use of com.dar.nclientv2.settings.AuthRequest in project NClientV2 by Dar9586.

the class TagsAdapter method onlineTagUpdate.

private void onlineTagUpdate(final Tag tag, final boolean add, final ImageView imgView) throws IOException {
    if (!Login.isLogged() || Login.getUser() == null)
        return;
    StringWriter sw = new StringWriter();
    JsonWriter jw = new JsonWriter(sw);
    jw.beginObject().name("added").beginArray();
    if (add)
        writeTag(jw, tag);
    jw.endArray().name("removed").beginArray();
    if (!add)
        writeTag(jw, tag);
    jw.endArray().endObject();
    final String url = String.format(Locale.US, Utility.getBaseUrl() + "users/%d/%s/blacklist", Login.getUser().getId(), Login.getUser().getCodename());
    final RequestBody ss = RequestBody.create(MediaType.get("application/json"), sw.toString());
    new AuthRequest(url, url, new Callback() {

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

        @Override
        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
            if (response.body().string().contains("ok")) {
                if (add)
                    Login.addOnlineTag(tag);
                else
                    Login.removeOnlineTag(tag);
                if (tagMode == TagMode.ONLINE)
                    updateLogo(imgView, add ? TagStatus.AVOIDED : TagStatus.DEFAULT);
            }
        }
    }).setMethod("POST", ss).start();
}
Also used : Response(okhttp3.Response) AuthRequest(com.dar.nclientv2.settings.AuthRequest) Call(okhttp3.Call) Callback(okhttp3.Callback) StringWriter(java.io.StringWriter) IOException(java.io.IOException) JsonWriter(android.util.JsonWriter) RequestBody(okhttp3.RequestBody)

Example 3 with AuthRequest

use of com.dar.nclientv2.settings.AuthRequest 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)

Example 4 with AuthRequest

use of com.dar.nclientv2.settings.AuthRequest in project NClientV2 by Dar9586.

the class GalleryActivity method downloadTorrent.

private void downloadTorrent() {
    if (!Global.hasStoragePermission(this)) {
        return;
    }
    String url = String.format(Locale.US, Utility.getBaseUrl() + "g/%d/download", gallery.getId());
    String referer = String.format(Locale.US, Utility.getBaseUrl() + "g/%d/", gallery.getId());
    new AuthRequest(referer, url, new Callback() {

        @Override
        public void onFailure(@NonNull Call call, @NonNull IOException e) {
            GalleryActivity.this.runOnUiThread(() -> Toast.makeText(GalleryActivity.this, R.string.failed, Toast.LENGTH_SHORT).show());
        }

        @Override
        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
            File file = new File(Global.TORRENTFOLDER, gallery.getId() + ".torrent");
            Utility.writeStreamToFile(response.body().byteStream(), file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            Uri torrentUri;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                torrentUri = FileProvider.getUriForFile(GalleryActivity.this, GalleryActivity.this.getPackageName() + ".provider", file);
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            } else {
                torrentUri = Uri.fromFile(file);
            }
            intent.setDataAndType(torrentUri, "application/x-bittorrent");
            try {
                GalleryActivity.this.startActivity(intent);
            } catch (RuntimeException ignore) {
                runOnUiThread(() -> Toast.makeText(GalleryActivity.this, R.string.failed, Toast.LENGTH_SHORT).show());
            }
            file.deleteOnExit();
        }
    }).setMethod("GET", null).start();
}
Also used : Response(okhttp3.Response) AuthRequest(com.dar.nclientv2.settings.AuthRequest) Call(okhttp3.Call) Callback(okhttp3.Callback) Intent(android.content.Intent) IOException(java.io.IOException) File(java.io.File) Uri(android.net.Uri)

Example 5 with AuthRequest

use of com.dar.nclientv2.settings.AuthRequest in project NClientV2 by Dar9586.

the class GalleryActivity method addToFavorite.

private void addToFavorite(final MenuItem item) {
    boolean wasFavorite = onlineFavoriteItem.getTitle().equals(getString(R.string.remove_from_online_favorites));
    String url = String.format(Locale.US, Utility.getBaseUrl() + "api/gallery/%d/%sfavorite", gallery.getId(), wasFavorite ? "un" : "");
    String galleryUrl = String.format(Locale.US, Utility.getBaseUrl() + "g/%d/", gallery.getId());
    LogUtility.d("Calling: " + url);
    new AuthRequest(galleryUrl, url, new Callback() {

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

        @Override
        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
            assert response.body() != null;
            String responseString = response.body().string();
            boolean nowIsFavorite = responseString.contains("true");
            updateIcon(nowIsFavorite);
        }
    }).setMethod("POST", AuthRequest.EMPTY_BODY).start();
}
Also used : Response(okhttp3.Response) AuthRequest(com.dar.nclientv2.settings.AuthRequest) Call(okhttp3.Call) Callback(okhttp3.Callback) IOException(java.io.IOException)

Aggregations

AuthRequest (com.dar.nclientv2.settings.AuthRequest)5 IOException (java.io.IOException)5 Call (okhttp3.Call)5 Callback (okhttp3.Callback)5 Response (okhttp3.Response)5 Comment (com.dar.nclientv2.api.comments.Comment)2 RequestBody (okhttp3.RequestBody)2 Intent (android.content.Intent)1 Uri (android.net.Uri)1 JsonReader (android.util.JsonReader)1 JsonWriter (android.util.JsonWriter)1 EditText (android.widget.EditText)1 Toolbar (androidx.appcompat.widget.Toolbar)1 DividerItemDecoration (androidx.recyclerview.widget.DividerItemDecoration)1 CommentsFetcher (com.dar.nclientv2.api.comments.CommentsFetcher)1 File (java.io.File)1 StringWriter (java.io.StringWriter)1