Search in sources :

Example 1 with RewardListAdapter

use of com.odysee.app.adapter.RewardListAdapter in project odysee-android by OdyseeTeam.

the class RewardsFragment method fetchRewards.

private void fetchRewards() {
    Helper.setViewVisibility(rewardList, View.INVISIBLE);
    rewardsLoading.setVisibility(View.VISIBLE);
    Activity activity = getActivity();
    String authToken;
    AccountManager am = AccountManager.get(getContext());
    Account odyseeAccount = Helper.getOdyseeAccount(am.getAccounts());
    if (odyseeAccount != null) {
        authToken = am.peekAuthToken(odyseeAccount, "auth_token_type");
    } else {
        authToken = "";
    }
    Map<String, String> options = new HashMap<>();
    options.put("multiple_rewards_per_type", "true");
    if (odyseeAccount != null && authToken != null) {
        options.put("auth_token", authToken);
    }
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        Supplier<List<Reward>> supplier = new FetchRewardsSupplier(options);
        CompletableFuture<List<Reward>> cf = CompletableFuture.supplyAsync(supplier, executorService);
        cf.exceptionally(e -> {
            if (activity != null) {
                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        ((MainActivity) activity).showError(e.getMessage());
                    }
                });
            }
            return null;
        }).thenAccept(rewards -> {
            Lbryio.updateRewardsLists(rewards);
            if (activity != null) {
                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        rewardsLoading.setVisibility(View.GONE);
                        if (adapter == null) {
                            adapter = new RewardListAdapter(rewards, getContext());
                            adapter.setClickListener(RewardsFragment.this);
                            adapter.setDisplayMode(RewardListAdapter.DISPLAY_MODE_UNCLAIMED);
                            rewardList.setAdapter(adapter);
                        } else {
                            adapter.setRewards(rewards);
                        }
                        Helper.setViewVisibility(rewardList, View.VISIBLE);
                    }
                });
            }
        });
    } else {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                Callable<List<Reward>> callable = new Callable<List<Reward>>() {

                    @Override
                    public List<Reward> call() {
                        List<Reward> rewards = null;
                        try {
                            JSONArray results = (JSONArray) Lbryio.parseResponse(Lbryio.call("reward", "list", options, null));
                            rewards = new ArrayList<>();
                            if (results != null) {
                                for (int i = 0; i < results.length(); i++) {
                                    rewards.add(Reward.fromJSONObject(results.getJSONObject(i)));
                                }
                            }
                        } catch (ClassCastException | LbryioRequestException | LbryioResponseException | JSONException ex) {
                            if (activity != null) {
                                activity.runOnUiThread(new Runnable() {

                                    @Override
                                    public void run() {
                                        ((MainActivity) activity).showError(ex.getMessage());
                                    }
                                });
                            }
                        }
                        return rewards;
                    }
                };
                Future<List<Reward>> future = executorService.submit(callable);
                try {
                    List<Reward> rewards = future.get();
                    if (rewards != null) {
                        Lbryio.updateRewardsLists(rewards);
                        if (activity != null) {
                            activity.runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    rewardsLoading.setVisibility(View.GONE);
                                    if (adapter == null) {
                                        adapter = new RewardListAdapter(rewards, getContext());
                                        adapter.setClickListener(RewardsFragment.this);
                                        adapter.setDisplayMode(RewardListAdapter.DISPLAY_MODE_UNCLAIMED);
                                        rewardList.setAdapter(adapter);
                                    } else {
                                        adapter.setRewards(rewards);
                                    }
                                    Helper.setViewVisibility(rewardList, View.VISIBLE);
                                }
                            });
                        }
                    }
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
    }
}
Also used : Typeface(android.graphics.Typeface) Context(android.content.Context) Bundle(android.os.Bundle) ProgressBar(android.widget.ProgressBar) NonNull(androidx.annotation.NonNull) ClaimRewardTask(com.odysee.app.tasks.lbryinc.ClaimRewardTask) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) ClaimRewardSupplier(com.odysee.app.supplier.ClaimRewardSupplier) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) MaterialButton(com.google.android.material.button.MaterialButton) MainActivity(com.odysee.app.MainActivity) Map(java.util.Map) View(android.view.View) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) RecyclerView(androidx.recyclerview.widget.RecyclerView) Build(android.os.Build) RewardListAdapter(com.odysee.app.adapter.RewardListAdapter) ExecutorService(java.util.concurrent.ExecutorService) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) AccountManager(android.accounts.AccountManager) AsyncTask(android.os.AsyncTask) Account(android.accounts.Account) LayoutInflater(android.view.LayoutInflater) DecimalFormat(java.text.DecimalFormat) Helper(com.odysee.app.utils.Helper) FetchRewardsSupplier(com.odysee.app.supplier.FetchRewardsSupplier) ViewGroup(android.view.ViewGroup) Executors(java.util.concurrent.Executors) Color(android.graphics.Color) ExecutionException(java.util.concurrent.ExecutionException) Lbryio(com.odysee.app.utils.Lbryio) List(java.util.List) TextView(android.widget.TextView) Reward(com.odysee.app.model.lbryinc.Reward) BaseFragment(com.odysee.app.ui.BaseFragment) LbryAnalytics(com.odysee.app.utils.LbryAnalytics) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) Activity(android.app.Activity) Snackbar(com.google.android.material.snackbar.Snackbar) EditText(android.widget.EditText) R(com.odysee.app.R) JSONArray(org.json.JSONArray) Account(android.accounts.Account) HashMap(java.util.HashMap) RewardListAdapter(com.odysee.app.adapter.RewardListAdapter) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) MainActivity(com.odysee.app.MainActivity) Activity(android.app.Activity) MainActivity(com.odysee.app.MainActivity) Callable(java.util.concurrent.Callable) FetchRewardsSupplier(com.odysee.app.supplier.FetchRewardsSupplier) ExecutorService(java.util.concurrent.ExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) AccountManager(android.accounts.AccountManager) ArrayList(java.util.ArrayList) List(java.util.List) Reward(com.odysee.app.model.lbryinc.Reward)

Example 2 with RewardListAdapter

use of com.odysee.app.adapter.RewardListAdapter in project odysee-android by OdyseeTeam.

the class RewardsFragment method onCreateView.

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    root = inflater.inflate(R.layout.fragment_rewards, container, false);
    linkFilterUnclaimed = root.findViewById(R.id.rewards_filter_link_unclaimed);
    linkFilterAll = root.findViewById(R.id.rewards_filter_link_all);
    rewardList = root.findViewById(R.id.rewards_list);
    rewardsLoading = root.findViewById(R.id.rewards_list_loading);
    Context context = getContext();
    LinearLayoutManager llm = new LinearLayoutManager(context);
    rewardList.setLayoutManager(llm);
    adapter = new RewardListAdapter(Lbryio.allRewards, context);
    adapter.setClickListener(this);
    adapter.setDisplayMode(RewardListAdapter.DISPLAY_MODE_UNCLAIMED);
    rewardList.setAdapter(adapter);
    initUi();
    return root;
}
Also used : Context(android.content.Context) RewardListAdapter(com.odysee.app.adapter.RewardListAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager)

Aggregations

Context (android.content.Context)2 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)2 RewardListAdapter (com.odysee.app.adapter.RewardListAdapter)2 Account (android.accounts.Account)1 AccountManager (android.accounts.AccountManager)1 Activity (android.app.Activity)1 Color (android.graphics.Color)1 Typeface (android.graphics.Typeface)1 AsyncTask (android.os.AsyncTask)1 Build (android.os.Build)1 Bundle (android.os.Bundle)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 EditText (android.widget.EditText)1 ProgressBar (android.widget.ProgressBar)1 TextView (android.widget.TextView)1 NonNull (androidx.annotation.NonNull)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 MaterialButton (com.google.android.material.button.MaterialButton)1