Пример #1
0
  @Override
  public void populateView(Displayable displayable) {
    HeaderRow row = (HeaderRow) displayable;
    title.setText(row.getLabel());
    if (row.isHasMore()) {
      more.setVisibility(View.VISIBLE);
      if (storeName == null || TextUtils.isEmpty(storeName)) {
        if (storeId == 0) {
          more.setOnClickListener(new BaseAdapter.IHasMoreOnClickListener(row, theme));
          moreLayout.setOnClickListener(new BaseAdapter.IHasMoreOnClickListener(row, theme));
        } else {
          more.setOnClickListener(new BaseAdapter.IHasMoreOnClickListener(row, theme, storeId));
          moreLayout.setOnClickListener(
              new BaseAdapter.IHasMoreOnClickListener(row, theme, storeId));
        }
      } else {
        if (storeId == 0) {
          more.setOnClickListener(new BaseAdapter.IHasMoreOnClickListener(row, theme, storeName));
          moreLayout.setOnClickListener(
              new BaseAdapter.IHasMoreOnClickListener(row, theme, storeName));
        } else {
          more.setOnClickListener(
              new BaseAdapter.IHasMoreOnClickListener(row, theme, storeName, storeId));
          moreLayout.setOnClickListener(
              new BaseAdapter.IHasMoreOnClickListener(row, theme, storeName, storeId));
        }
      }
    } else {
      more.setVisibility(View.GONE);
      moreLayout.setClickable(false);
      moreLayout.setFocusable(false);
    }

    if (theme != null) {
      @ColorInt int color = itemView.getContext().getResources().getColor(theme.getStoreHeader());
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        more.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
      } else {

        // put this in Utils when you need to tint a Button background
        Drawable wrapDrawable = DrawableCompat.wrap(more.getBackground());
        DrawableCompat.setTint(
            wrapDrawable, itemView.getContext().getResources().getColor(theme.getStoreHeader()));
        more.setBackgroundDrawable(DrawableCompat.unwrap(wrapDrawable));
      }
    }
  }
  @Override
  public void onBindViewHolder(final BaseViewHolder holder, final int position) {

    switch (holder.viewType) {
      case R.layout.layout_header:
        HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
        HeaderRow headerRow = (HeaderRow) displayableList.get(position);
        headerViewHolder.title.setText(headerRow.getLabel());
        headerViewHolder.more.setText(
            headerViewHolder.more.getContext().getString(R.string.update_all));
        ViewGroup.LayoutParams layoutParams = headerViewHolder.more.getLayoutParams();
        layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        headerViewHolder.more.setLayoutParams(layoutParams);
        headerViewHolder.more.setVisibility(!headerRow.isHasMore() ? View.GONE : View.VISIBLE);
        headerViewHolder.more.setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                List<UpdateRow> updateRows = new ArrayList<>();
                for (Displayable displayable : displayableList) {
                  if (displayable instanceof UpdateRow) {
                    UpdateRow row = (UpdateRow) displayable;
                    updateRows.add(row);
                  }
                  Analytics.Updates.updateAll();
                }
                BusProvider.getInstance().post(new OttoEvents.StartDownload(updateRows));
              }
            });
        break;
      case R.layout.update_row:
        final UpdateViewHolder updateViewHolder = (UpdateViewHolder) holder;

        final UpdateRow appItem = (UpdateRow) displayableList.get(position);

        updateViewHolder.name.setText(appItem.appName);
        updateViewHolder.appInstalledVersion.setText(appItem.versionNameInstalled);
        updateViewHolder.appUpdateVersion.setText(String.valueOf(appItem.versionName));
        updateViewHolder.updateButtonLayout.setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                Displayable displayable = displayableList.get(holder.getAdapterPosition());
                if (displayable instanceof UpdateRow) {
                  UpdateRow row = (UpdateRow) displayable;
                  List<UpdateRow> updateRows = new ArrayList<>();
                  updateRows.add(row);
                  BusProvider.getInstance().post(new OttoEvents.StartDownload(updateRows));
                }
                Analytics.Updates.update();
              }
            });

        updateViewHolder.itemView.setLongClickable(true);
        updateViewHolder.itemView.setOnLongClickListener(
            new View.OnLongClickListener() {
              @Override
              public boolean onLongClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                View dialogView =
                    LayoutInflater.from(v.getContext())
                        .inflate(R.layout.exclude_update_uninstall_menu, null);
                builder.setView(dialogView);
                final AlertDialog alertDialog = builder.create();
                dialogView
                    .findViewById(R.id.confirmIgnoreUpdateUninstall)
                    .setOnClickListener(
                        new View.OnClickListener() {
                          @Override
                          public void onClick(View v) {
                            new Thread(
                                    new Runnable() {
                                      @Override
                                      public void run() {

                                        AptoideDatabase database =
                                            new AptoideDatabase(Aptoide.getDb());
                                        database.addToExcludeUpdate(appItem);
                                        BusProvider.getInstance()
                                            .post(
                                                new OttoEvents.ExcludedUpdateAddedEvent(position));
                                      }
                                    })
                                .start();
                            int position = holder.getAdapterPosition();
                            if (position != RecyclerView.NO_POSITION) {
                              displayableList.remove(position);
                              notifyItemRemoved(position);
                            }

                            // When we remove the last update, we also need to remove the header.
                            checkAndRemoveHeaderRow(holder.getAdapterPosition());
                            Toast.makeText(v.getContext(), "Ignored", Toast.LENGTH_SHORT).show();
                            alertDialog.dismiss();
                          }
                        });
                alertDialog.show();
                return true;
              }

              /**
               * Check if there's any Update on the displayableList and if so, removes the Update
               * header
               */
              void checkAndRemoveHeaderRow(int position) {
                if (isUpdateListEmpty()) {
                  for (Iterator<Displayable> it = displayableList.iterator(); it.hasNext(); ) {
                    Displayable displayable = it.next();
                    if (displayable instanceof UpdateHeaderRow) {
                      it.remove();
                      notifyItemRemoved(position);
                    }
                  }
                }
              }

              /**
               * Iterates through the displayableList and check if there's an Update row type.
               *
               * @return
               */
              boolean isUpdateListEmpty() {
                for (Displayable displayable : displayableList) {
                  if (displayable instanceof UpdateRow) {
                    return false;
                  }
                }
                return true;
              }
            });
        updateViewHolder.itemView.setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                Intent i = new Intent(v.getContext(), AppViewActivity.class);
                i.putExtra(Constants.UPDATE_FROM_KEY, true);
                i.putExtra(Constants.MD5SUM_KEY, appItem.md5sum);
                i.putExtra(Constants.PACKAGENAME_KEY, appItem.packageName);
                i.putExtra(Constants.VERSIONNAME_KEY, appItem.versionName);
                i.putExtra(Constants.APPNAME_KEY, appItem.appName);
                i.putExtra(Constants.STORENAME_KEY, appItem.storeName);
                i.putExtra(Constants.ICON_KEY, appItem.icon);
                i.putExtra(
                    Constants.DOWNLOAD_FROM_KEY, "updates"); // renamed from: recommended_apps
                v.getContext().startActivity(i);
              }
            });
        Glide.with(updateViewHolder.itemView.getContext())
            .load(appItem.icon)
            .asBitmap()
            .into(updateViewHolder.icon);

        break;
      case R.layout.installed_row:
        final InstalledViewHolder installedViewHolder = (InstalledViewHolder) holder;

        final InstallRow installRow = (InstallRow) displayableList.get(position);

        installedViewHolder.name.setText(installRow.appName);
        installedViewHolder.tvAppVersion.setText(installRow.versionName);
        installedViewHolder.createReviewLayout.setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), UploadApkActivity.class);

                String packageName = installRow.packageName;

                intent.putExtra("package_name", packageName);
                v.getContext().startActivity(intent);

                Analytics.Updates.createReview();
              }
            });
        installedViewHolder.installedItemFrame.setOnLongClickListener(
            new View.OnLongClickListener() {
              @Override
              public boolean onLongClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                final View dialogView =
                    LayoutInflater.from(v.getContext())
                        .inflate(R.layout.exclude_update_uninstall_menu, null);
                builder.setView(dialogView);
                final AlertDialog alertDialog = builder.create();
                ((TextView) dialogView.findViewById(R.id.tvUpdateUninstall))
                    .setText(R.string.updatesTabAdapterUninstall);
                dialogView
                    .findViewById(R.id.confirmIgnoreUpdateUninstall)
                    .setOnClickListener(
                        new View.OnClickListener() {
                          @Override
                          public void onClick(View v) {
                            Fragment fragment = new UninstallRetainFragment();
                            Bundle args = new Bundle();
                            args.putString("name", installRow.appName);
                            args.putString("package", installRow.packageName);
                            args.putString("version", installRow.versionName);
                            args.putString("icon", installRow.icon);
                            fragment.setArguments(args);
                            activity
                                .getSupportFragmentManager()
                                .beginTransaction()
                                .add(fragment, "uninstall")
                                .commit();

                            //
                            // displayableList.remove(holder.getAdapterPosition());
                            //
                            // notifyItemRemoved(holder.getAdapterPosition());

                            alertDialog.dismiss();
                          }
                        });
                alertDialog.show();

                return true;
              }
            });
        installedViewHolder.installedItemFrame.setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                Intent intent =
                    activity.getPackageManager().getLaunchIntentForPackage(installRow.packageName);
                if (installedViewHolder.itemView != null
                    && installedViewHolder.itemView.getContext() != null
                    && intent != null) {
                  installedViewHolder.itemView.getContext().startActivity(intent);
                }
              }
            });
        //
        // installedViewHolder.downloads.setText(String.valueOf(installRow.versionName));
        //                installedViewHolder.ratingBar.setRating((float) installRow.rating);

        // installedViewHolder.icon.setImageURI(Uri.parse(installRow.icon));

        Glide.with(installedViewHolder.itemView.getContext())
            .load(installRow.icon)
            .into(installedViewHolder.icon);

        break;
    }
  }