Search in sources :

Example 6 with SingleContainer

use of se.oort.diplicity.apigen.SingleContainer in project android-diplicity by zond.

the class GameActivity method showGameResults.

public void showGameResults() {
    setVisibility(View.GONE, R.id.solo_winner_label, R.id.solo_winner, R.id.dias_members_label, R.id.dias_members, R.id.nmr_game_members_label, R.id.nmr_game_members, R.id.eliminated_members_label, R.id.eliminated_members);
    hideAllExcept(R.id.game_results_view);
    final ScrollView gameResultsView = (ScrollView) findViewById(R.id.game_results_view);
    gameResultsView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            flickFrameLayout.onTouchEvent(motionEvent);
            gameResultsView.onTouchEvent(motionEvent);
            return true;
        }
    });
    if (game.Finished) {
        handleReq(gameResultService.GameResultLoad(game.ID), new Sendable<SingleContainer<GameResult>>() {

            @Override
            public void send(SingleContainer<GameResult> gameResultSingleContainer) {
                MemberListPopulater populater = new MemberListPopulater();
                if (gameResultSingleContainer.Properties.SoloWinnerUser != null && !gameResultSingleContainer.Properties.SoloWinnerUser.equals("")) {
                    populater.populate(R.id.solo_winner, Arrays.asList(gameResultSingleContainer.Properties.SoloWinnerUser));
                    setVisibility(View.VISIBLE, R.id.solo_winner, R.id.solo_winner_label);
                }
                if (gameResultSingleContainer.Properties.DIASUsers != null) {
                    populater.populate(R.id.dias_members, gameResultSingleContainer.Properties.DIASUsers);
                    setVisibility(View.VISIBLE, R.id.dias_members, R.id.dias_members_label);
                }
                if (gameResultSingleContainer.Properties.EliminatedUsers != null) {
                    populater.populate(R.id.eliminated_members, gameResultSingleContainer.Properties.EliminatedUsers);
                    setVisibility(View.VISIBLE, R.id.eliminated_members, R.id.eliminated_members_label);
                }
                if (gameResultSingleContainer.Properties.NMRUsers != null) {
                    populater.populate(R.id.nmr_game_members, gameResultSingleContainer.Properties.NMRUsers);
                    setVisibility(View.VISIBLE, R.id.nmr_game_members, R.id.nmr_game_members_label);
                }
                if (gameResultSingleContainer.Properties.Scores != null) {
                    ((MemberTable) findViewById(R.id.scored_members)).setScores(gameResultSingleContainer.Properties.Scores);
                    populater.populate(R.id.scored_members, gameResultSingleContainer.Properties.AllUsers);
                    setVisibility(View.VISIBLE, R.id.scored_members, R.id.scored_members_label);
                }
            }
        }, getResources().getString(R.string.loading_game_result));
    }
}
Also used : SingleContainer(se.oort.diplicity.apigen.SingleContainer) ScrollView(android.widget.ScrollView) GameResult(se.oort.diplicity.apigen.GameResult) NavigationView(android.support.design.widget.NavigationView) UserView(se.oort.diplicity.UserView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) ScrollView(android.widget.ScrollView) MotionEvent(android.view.MotionEvent)

Example 7 with SingleContainer

use of se.oort.diplicity.apigen.SingleContainer in project android-diplicity by zond.

the class GameActivity method setOrder.

private void setOrder(String province, List<String> parts) {
    Sendable<SingleContainer<Order>> handler = new Sendable<SingleContainer<Order>>() {

        @Override
        public void send(SingleContainer<Order> orderSingleContainer) {
            MapView mv = (MapView) findViewById(R.id.map_view);
            mv.evaluateJS("window.map.removeOrders()");
            for (Map.Entry<String, Order> entry : orders.entrySet()) {
                mv.evaluateJS("window.map.addOrder(" + new Gson().toJson(entry.getValue().Parts) + ", col" + entry.getValue().Nation + ");");
            }
        }
    };
    if (parts == null || parts.size() == 0) {
        if (orders.containsKey(province)) {
            orders.remove(province);
            handleReq(orderService.OrderDelete(game.ID, phaseMeta.PhaseOrdinal.toString(), province), handler, getResources().getString(R.string.removing_order));
        }
    } else {
        Order order = new Order();
        order.GameID = game.ID;
        order.Nation = member.Nation;
        order.PhaseOrdinal = phaseMeta.PhaseOrdinal;
        order.Parts = parts;
        orders.put(province, order);
        handleReq(orderService.OrderCreate(order, game.ID, phaseMeta.PhaseOrdinal.toString()), handler, getResources().getString(R.string.saving_order));
    }
}
Also used : SingleContainer(se.oort.diplicity.apigen.SingleContainer) Order(se.oort.diplicity.apigen.Order) Sendable(se.oort.diplicity.Sendable) Gson(com.google.gson.Gson) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with SingleContainer

use of se.oort.diplicity.apigen.SingleContainer in project android-diplicity by zond.

the class UserView method getAvatarClickListener.

public static OnClickListener getAvatarClickListener(final RetrofitActivity retrofitActivity, final Game game, final Member member, final User user) {
    if (game == null) {
        return getAvatarClickListener(retrofitActivity, user);
    }
    Member me = retrofitActivity.getLoggedInMember(game);
    if (me == null || me.Nation.equals(member.Nation)) {
        return getAvatarClickListener(retrofitActivity, user);
    }
    final Member finalMe = me;
    return new OnClickListener() {

        @Override
        public void onClick(View v) {
            retrofitActivity.handleReq(JoinObservable.when(JoinObservable.from(retrofitActivity.userStatsService.UserStatsLoad(user.Id)).and(retrofitActivity.gameStateService.GameStateLoad(game.ID, finalMe.Nation)).and(retrofitActivity.banService.BanLoad(retrofitActivity.getLoggedInUser().Id, user.Id).onErrorReturn(new Func1<Throwable, SingleContainer<Ban>>() {

                @Override
                public SingleContainer<Ban> call(Throwable throwable) {
                    if (throwable instanceof HttpException) {
                        HttpException he = (HttpException) throwable;
                        if (he.code() == 404) {
                            return null;
                        }
                    }
                    throw new RuntimeException(throwable);
                }
            })).then(new Func3<SingleContainer<UserStats>, SingleContainer<GameState>, SingleContainer<Ban>, Object>() {

                @Override
                public Object call(final SingleContainer<UserStats> userStatsSingleContainer, final SingleContainer<GameState> gameStateSingleContainer, final SingleContainer<Ban> banSingleContainer) {
                    AlertDialog dialog = new AlertDialog.Builder(retrofitActivity).setView(R.layout.user_dialog).show();
                    setupUserDialog(retrofitActivity, dialog, userStatsSingleContainer, banSingleContainer);
                    final CheckBox mutedCheckBox = (CheckBox) dialog.findViewById(R.id.muted);
                    mutedCheckBox.setVisibility(VISIBLE);
                    mutedCheckBox.setChecked(gameStateSingleContainer.Properties.Muted != null && gameStateSingleContainer.Properties.Muted.contains(member.Nation));
                    mutedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            if (isChecked && (gameStateSingleContainer.Properties.Muted == null || !gameStateSingleContainer.Properties.Muted.contains(member.Nation))) {
                                if (gameStateSingleContainer.Properties.Muted == null) {
                                    gameStateSingleContainer.Properties.Muted = new ArrayList<String>();
                                }
                                gameStateSingleContainer.Properties.Muted.add(member.Nation);
                            } else if (!isChecked && gameStateSingleContainer.Properties.Muted != null && gameStateSingleContainer.Properties.Muted.contains(member.Nation)) {
                                gameStateSingleContainer.Properties.Muted.remove(member.Nation);
                            }
                            retrofitActivity.handleReq(retrofitActivity.gameStateService.GameStateUpdate(gameStateSingleContainer.Properties, game.ID, finalMe.Nation), new Sendable<SingleContainer<GameState>>() {

                                @Override
                                public void send(SingleContainer<GameState> gameStateSingleContainer) {
                                }
                            }, retrofitActivity.getResources().getString(R.string.updating));
                        }
                    });
                    return null;
                }
            })).toObservable(), new Sendable<Object>() {

                @Override
                public void send(Object o) {
                }
            }, retrofitActivity.getResources().getString(R.string.loading_user_stats));
        }
    };
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) ArrayList(java.util.ArrayList) GameState(se.oort.diplicity.apigen.GameState) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) Ban(se.oort.diplicity.apigen.Ban) SingleContainer(se.oort.diplicity.apigen.SingleContainer) UserStats(se.oort.diplicity.apigen.UserStats) CheckBox(android.widget.CheckBox) HttpException(retrofit2.adapter.rxjava.HttpException) Func1(rx.functions.Func1) Member(se.oort.diplicity.apigen.Member) Func3(rx.functions.Func3) CompoundButton(android.widget.CompoundButton)

Example 9 with SingleContainer

use of se.oort.diplicity.apigen.SingleContainer in project android-diplicity by zond.

the class MainActivity method onPostCreate.

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    addGameButton = (FloatingActionButton) findViewById(R.id.add_game_button);
    addGameButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            handleReq(userStatsService.UserStatsLoad(getLoggedInUser().Id), new Sendable<SingleContainer<UserStats>>() {

                @Override
                public void send(SingleContainer<UserStats> userStatsSingleContainer) {
                    final List<UserStats> statsContainer = new ArrayList<>();
                    statsContainer.add(userStatsSingleContainer.Properties);
                    final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setView(R.layout.create_game_dialog).show();
                    final Returner<Game, Boolean> validateGame = new Returner<Game, Boolean>() {

                        @Override
                        public Boolean Return(Game g) {
                            if (g.PhaseLengthMinutes == null)
                                g.PhaseLengthMinutes = DAY_IN_MINUTES;
                            if (g.PhaseLengthMinutes > 30 * DAY_IN_MINUTES) {
                                Toast.makeText(MainActivity.this, R.string.phase_length_must_be_less_than_30_days, Toast.LENGTH_LONG).show();
                                return false;
                            }
                            UserStats us = statsContainer.get(0);
                            if (g.MinRating == null)
                                g.MinRating = 0.0;
                            if (g.MinRating != 0.0 && g.MinRating > us.Glicko.PracticalRating) {
                                Toast.makeText(MainActivity.this, getResources().getString(R.string.minimum_rating_must_be_below_your_rating_x, us.Glicko.PracticalRating), Toast.LENGTH_LONG).show();
                                return false;
                            }
                            if (g.MaxRating == null)
                                g.MaxRating = 0.0;
                            if (g.MaxRating != 0.0 && g.MaxRating < us.Glicko.PracticalRating) {
                                Toast.makeText(MainActivity.this, getResources().getString(R.string.maximum_rating_must_be_above_your_rating_x, us.Glicko.PracticalRating), Toast.LENGTH_LONG).show();
                                return false;
                            }
                            if (g.MinReliability == null)
                                g.MinReliability = 0.0;
                            if (g.MinReliability != 0.0 && g.MinReliability > us.Reliability) {
                                Toast.makeText(MainActivity.this, getResources().getString(R.string.minimum_reliability_must_be_below_your_reliability_x, us.Reliability), Toast.LENGTH_LONG).show();
                                return false;
                            }
                            if (g.MinQuickness == null)
                                g.MinQuickness = 0.0;
                            if (g.MinQuickness != 0.0 && g.MinQuickness > us.Quickness) {
                                Toast.makeText(MainActivity.this, getResources().getString(R.string.minimum_quickness_must_be_below_your_quickness_x, us.Quickness), Toast.LENGTH_LONG).show();
                                return false;
                            }
                            if (g.MaxHated == null)
                                g.MaxHated = 0.0;
                            if (g.MaxHated != 0.0 && g.MaxHated < us.Hated) {
                                Toast.makeText(MainActivity.this, getResources().getString(R.string.maximum_hated_must_be_above_your_hated_x, us.Hated), Toast.LENGTH_LONG).show();
                                return false;
                            }
                            if (g.MaxHater == null)
                                g.MaxHater = 0.0;
                            if (g.MaxHater != 0.0 && g.MaxHater < us.Hater) {
                                Toast.makeText(MainActivity.this, getResources().getString(R.string.maximum_hater_must_be_above_your_hater_x, us.Hater), Toast.LENGTH_LONG).show();
                                return false;
                            }
                            return true;
                        }
                    };
                    final Spinner variants = ((Spinner) dialog.findViewById(R.id.variants));
                    final List<SpinnerVariantElement> variantNames = new ArrayList<>();
                    for (SingleContainer<VariantService.Variant> variantContainer : getVariants().Properties) {
                        SpinnerVariantElement el = new SpinnerVariantElement();
                        el.name = variantContainer.Properties.Name;
                        if (variantContainer.Properties.Nations != null) {
                            el.players = variantContainer.Properties.Nations.size();
                        }
                        variantNames.add(el);
                    }
                    int classical = 0;
                    Collections.sort(variantNames);
                    for (int i = 0; i < variantNames.size(); i++) {
                        if (variantNames.get(i).name.equals("Classical")) {
                            classical = i;
                        }
                    }
                    ArrayAdapter<SpinnerVariantElement> variantAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item, variantNames);
                    variants.setAdapter(variantAdapter);
                    variantAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    variants.setSelection(classical);
                    final EditText gameNameView = (EditText) dialog.findViewById(R.id.desc);
                    final EditText phaseLengthView = (EditText) dialog.findViewById(R.id.phase_length);
                    final Spinner phaseLengthUnitsSpinner = (Spinner) dialog.findViewById(R.id.phase_length_units);
                    final EditText minRatingView = (EditText) dialog.findViewById(R.id.min_rating);
                    final EditText maxRatingView = (EditText) dialog.findViewById(R.id.max_rating);
                    final EditText minReliabilityView = (EditText) dialog.findViewById(R.id.min_reliability);
                    final EditText minQuicknessView = (EditText) dialog.findViewById(R.id.min_quickness);
                    final EditText maxHatedView = (EditText) dialog.findViewById(R.id.max_hated);
                    final EditText maxHaterView = (EditText) dialog.findViewById(R.id.max_hater);
                    final View.OnFocusChangeListener gameNameListener = new View.OnFocusChangeListener() {

                        private final int key = random.nextInt(Integer.MAX_VALUE);

                        private boolean generatedName = true;

                        @Override
                        public void onFocusChange(View view, boolean b) {
                            if (view.equals(gameNameView)) {
                                if (gameNameView.getText().toString().isEmpty()) {
                                    generatedName = true;
                                }
                            }
                            if (generatedName) {
                                long phaseLength = getPhaseLengthMinutes(phaseLengthView, phaseLengthUnitsSpinner);
                                String battle;
                                if (phaseLength < DAY_IN_MINUTES) {
                                    battle = getKeyedString(R.array.blitz);
                                } else if (phaseLength <= 2 * DAY_IN_MINUTES) {
                                    battle = getKeyedString(R.array.battle);
                                } else {
                                    battle = getKeyedString(R.array.war);
                                }
                                String adjective;
                                if (getDoubleValue(minRatingView, 0) > 1400) {
                                    adjective = getKeyedString(R.array.quality);
                                } else if (getDoubleValue(maxRatingView, 2000) < 1000) {
                                    adjective = getKeyedString(R.array.fun);
                                } else if (getDoubleValue(minReliabilityView, 0) > 10) {
                                    adjective = getKeyedString(R.array.reliable);
                                } else if (getDoubleValue(minQuicknessView, 0) > 10) {
                                    adjective = getKeyedString(R.array.fast);
                                } else if (getDoubleValue(maxHatedView, 100) < 10) {
                                    adjective = getKeyedString(R.array.pleasant);
                                } else if (getDoubleValue(maxHaterView, 100) < 10) {
                                    adjective = getKeyedString(R.array.patient);
                                } else {
                                    adjective = getKeyedString(R.array.other);
                                }
                                String prize = getKeyedString(R.array.prize);
                                String calculatedName = getResources().getString(R.string.game_name_template, battle, adjective, prize);
                                if (view.equals(gameNameView)) {
                                    String enteredName = gameNameView.getText().toString();
                                    generatedName = (enteredName.isEmpty() || enteredName.equals(calculatedName));
                                } else {
                                    gameNameView.setText(calculatedName);
                                }
                            }
                        }

                        private double getDoubleValue(EditText editText, int def) {
                            try {
                                return Double.parseDouble(editText.getText().toString());
                            } catch (NumberFormatException e) {
                                return def;
                            }
                        }

                        private String getKeyedString(int arrayId) {
                            return getResources().getStringArray(arrayId)[key % getResources().getStringArray(arrayId).length];
                        }
                    };
                    AdapterView.OnItemSelectedListener phaseLengthUnitsListener = new AdapterView.OnItemSelectedListener() {

                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            gameNameListener.onFocusChange(view, false);
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
                            parent.setSelection(0);
                        }
                    };
                    setDefaultPhaseLength(phaseLengthView, phaseLengthUnitsSpinner);
                    setDefaultMinReliability(minReliabilityView, statsContainer.get(0));
                    setDefaultMinQuickness(minQuicknessView, statsContainer.get(0));
                    gameNameView.setOnFocusChangeListener(gameNameListener);
                    phaseLengthView.setOnFocusChangeListener(gameNameListener);
                    minRatingView.setOnFocusChangeListener(gameNameListener);
                    maxRatingView.setOnFocusChangeListener(gameNameListener);
                    minReliabilityView.setOnFocusChangeListener(gameNameListener);
                    minQuicknessView.setOnFocusChangeListener(gameNameListener);
                    maxHatedView.setOnFocusChangeListener(gameNameListener);
                    maxHaterView.setOnFocusChangeListener(gameNameListener);
                    phaseLengthUnitsSpinner.setOnItemSelectedListener(phaseLengthUnitsListener);
                    ((FloatingActionButton) dialog.findViewById(R.id.create_game_button)).setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View view) {
                            final Game game = new Game();
                            game.Desc = gameNameView.getText().toString();
                            game.Variant = variantNames.get(variants.getSelectedItemPosition()).name;
                            game.PhaseLengthMinutes = getPhaseLengthMinutes(phaseLengthView, phaseLengthUnitsSpinner);
                            try {
                                game.MinRating = Double.parseDouble(minRatingView.getText().toString());
                            } catch (NumberFormatException e) {
                            }
                            try {
                                game.MaxRating = Double.parseDouble(maxRatingView.getText().toString());
                            } catch (NumberFormatException e) {
                            }
                            try {
                                game.MinReliability = Double.parseDouble(minReliabilityView.getText().toString());
                            } catch (NumberFormatException e) {
                            }
                            try {
                                game.MinQuickness = Double.parseDouble(minQuicknessView.getText().toString());
                            } catch (NumberFormatException e) {
                            }
                            try {
                                game.MaxHated = Double.parseDouble(maxHatedView.getText().toString());
                            } catch (NumberFormatException e) {
                            }
                            try {
                                game.MaxHater = Double.parseDouble(maxHaterView.getText().toString());
                            } catch (NumberFormatException e) {
                            }
                            if (validateGame.Return(game)) {
                                handleReq(gameService.GameCreate(game), new Sendable<SingleContainer<Game>>() {

                                    @Override
                                    public void send(SingleContainer<Game> gameSingleContainer) {
                                        if (nextCursorContainer.get(0).length() == 0) {
                                            findViewById(R.id.empty_view).setVisibility(View.GONE);
                                            contentList.setVisibility(View.VISIBLE);
                                            gamesAdapter.items.add(gameSingleContainer);
                                            gamesAdapter.notifyDataSetChanged();
                                        }
                                        dialog.dismiss();
                                    }
                                }, new ErrorHandler(412, new Sendable<HttpException>() {

                                    @Override
                                    public void send(HttpException e) {
                                        handleReq(userStatsService.UserStatsLoad(getLoggedInUser().Id), new Sendable<SingleContainer<UserStats>>() {

                                            @Override
                                            public void send(SingleContainer<UserStats> userStatsSingleContainer) {
                                                statsContainer.set(0, userStatsSingleContainer.Properties);
                                                validateGame.Return(game);
                                            }
                                        }, getResources().getString(R.string.creating_game));
                                    }
                                }), getResources().getString(R.string.creating_game));
                            }
                        }
                    });
                }
            }, getResources().getString(R.string.loading_user_stats));
        }

        /**
         * Determine the current user entered phase length.
         *
         * @param phaseLengthView The view containing the selected quantity.
         * @param phaseLengthUnitsView The spiner containing the selected units.
         * @return The selected phase length in minutes.
         */
        private long getPhaseLengthMinutes(EditText phaseLengthView, Spinner phaseLengthUnitsView) {
            // Get the quantity entered
            long quantity;
            try {
                quantity = Long.valueOf(phaseLengthView.getText().toString());
            } catch (NumberFormatException e) {
                setDefaultPhaseLength(phaseLengthView, phaseLengthUnitsView);
                quantity = Long.valueOf(phaseLengthView.getText().toString());
            }
            // Use the quantity and the selected unit to find the phase length.
            long phaseLength;
            int selectedItem = phaseLengthUnitsView.getSelectedItemPosition();
            if (selectedItem == getIndexOfUnit(R.string._day_s)) {
                phaseLength = quantity * DAY_IN_MINUTES;
            } else if (selectedItem == getIndexOfUnit(R.string._hour_s)) {
                phaseLength = quantity * HOUR_IN_MINUTES;
            } else if (selectedItem == getIndexOfUnit(R.string._minute_s)) {
                phaseLength = quantity;
            } else {
                Log.e("Diplicity", "Programmer error: Unexpected phase length units selected");
                throw new IllegalStateException("Programmer error: Unexpected phase length units selected");
            }
            return phaseLength;
        }

        /**
         * Set the phase length to the default of one day.
         */
        private void setDefaultPhaseLength(EditText phaseLengthView, Spinner phaseLengthUnitsView) {
            phaseLengthView.setText("1");
            phaseLengthUnitsView.setSelection(getIndexOfUnit(R.string._day_s));
        }

        private void setDefaultMinReliability(EditText minReliabilityView, UserStats userStats) {
            if (userStats.Reliability > 10) {
                minReliabilityView.setText("10");
            } else {
                minReliabilityView.setText("" + userStats.Reliability.intValue());
            }
        }

        private void setDefaultMinQuickness(EditText minQuicknessView, UserStats userStats) {
            if (userStats.Quickness > 10) {
                minQuicknessView.setText("10");
            } else {
                minQuicknessView.setText("" + userStats.Quickness.intValue());
            }
        }

        /**
         * Get the index of the given unit string resource in the array list used by the units spinner.
         */
        private int getIndexOfUnit(int unitString) {
            List<String> unitLabels = asList(getResources().getStringArray(R.array.phaseLengthUnits));
            return unitLabels.indexOf(getResources().getString(unitString));
        }
    });
    setupNavigation();
    contentList = (RecyclerView) findViewById(R.id.content_list);
    LinearLayoutManager contentLayoutManager = new LinearLayoutManager(this);
    contentLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    contentList.setLayoutManager(contentLayoutManager);
    scrollListener = new EndlessRecyclerViewScrollListener(contentLayoutManager, nextCursor) {

        @Override
        public void onLoadMore(String cursor, int totalItemsCount, RecyclerView view) {
            if (cursor.length() > 0) {
                loadMoreProcContainer.get(0).send(cursor);
            }
        }
    };
    contentList.addOnScrollListener(scrollListener);
    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, contentLayoutManager.getOrientation());
    contentList.addItemDecoration(dividerItemDecoration);
    loadMoreProcContainer.add(null);
    if (!ACTION_VIEW_USER_GAMES.equals(getIntent().getAction()) && !Intent.ACTION_VIEW.equals(getIntent().getAction()))
        navigateTo(0, 0);
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) Spinner(android.widget.Spinner) ActionBarDrawerToggle(android.support.v7.app.ActionBarDrawerToggle) ArrayList(java.util.ArrayList) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) FloatingActionButton(android.support.design.widget.FloatingActionButton) HttpException(retrofit2.adapter.rxjava.HttpException) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) DrawerLayout(android.support.v4.widget.DrawerLayout) Toolbar(android.support.v7.widget.Toolbar) EditText(android.widget.EditText) DividerItemDecoration(android.support.v7.widget.DividerItemDecoration) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) RecyclerView(android.support.v7.widget.RecyclerView) ExpandableListView(android.widget.ExpandableListView) SingleContainer(se.oort.diplicity.apigen.SingleContainer) UserStats(se.oort.diplicity.apigen.UserStats) Game(se.oort.diplicity.apigen.Game) AdapterView(android.widget.AdapterView) RecyclerView(android.support.v7.widget.RecyclerView) ArrayAdapter(android.widget.ArrayAdapter)

Example 10 with SingleContainer

use of se.oort.diplicity.apigen.SingleContainer in project android-diplicity by zond.

the class MainActivity method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        Intent i = new Intent(this, PreferenceActivity.class);
        startActivity(i);
        return true;
    } else if (id == R.id.action_error_log) {
        AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setView(R.layout.error_log_dialog).show();
        ((EditText) dialog.findViewById(R.id.error_log)).setText(App.errorLog.toString());
    } else if (id == R.id.action_forum) {
        String url = "https://groups.google.com/forum/#!forum/diplicity-talk";
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    } else if (id == R.id.action_source) {
        String url = "https://github.com/zond/android-diplicity";
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    } else if (id == R.id.action_alarms) {
        AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setView(R.layout.alarms_dialog).show();
        Map<String, ?> alarms = Alarm.getAlarmPreferences(this).getAll();
        LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.alarms_list);
        if (alarms.size() == 0) {
            dialog.findViewById(R.id.empty_view).setVisibility(View.VISIBLE);
            layout.setVisibility(View.GONE);
        } else {
            dialog.findViewById(R.id.empty_view).setVisibility(View.GONE);
            for (Object json : Alarm.getAlarmPreferences(this).getAll().values()) {
                Alarm.Alert alert = Alarm.Alert.fromJSON("" + json);
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View row = inflater.inflate(R.layout.alarm_list_row, null);
                ((TextView) row.findViewById(R.id.desc)).setText(alert.desc);
                ((TextView) row.findViewById(R.id.at)).setText(alert.alertAt(this).toString());
                layout.addView(row);
            }
            layout.setVisibility(View.VISIBLE);
        }
    } else if (id == R.id.action_bans) {
        handleReq(banService.ListBans(getLoggedInUser().Id), new Sendable<MultiContainer<Ban>>() {

            private RelativeLayout.LayoutParams wrapContentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

            private void setMargins(RelativeLayout.LayoutParams params) {
                int margin = getResources().getDimensionPixelSize(R.dimen.member_table_margin);
                params.bottomMargin = margin;
                params.topMargin = margin;
                params.leftMargin = margin;
                params.rightMargin = margin;
            }

            private void setupView(final AlertDialog dialog, final MultiContainer<Ban> banMultiContainer, final SingleContainer<Ban> ban, View convertView) {
                User other = null;
                for (User u : ban.Properties.Users) {
                    if (!u.Id.equals(getLoggedInUser().Id)) {
                        other = u;
                        break;
                    }
                }
                if (other != null) {
                    final User finalOther = other;
                    ((UserView) convertView.findViewById(R.id.other_user)).setUser(MainActivity.this, other, true);
                    if (ban.Properties.OwnerIds.contains(getLoggedInUser().Id)) {
                        LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        FloatingActionButton button = (FloatingActionButton) inflater.inflate(R.layout.clear_button, null);
                        setMargins(wrapContentParams);
                        wrapContentParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                        wrapContentParams.addRule(RelativeLayout.ALIGN_PARENT_END);
                        wrapContentParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                        button.setLayoutParams(wrapContentParams);
                        button.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                handleReq(banService.BanDelete(getLoggedInUser().Id, finalOther.Id), new Sendable<SingleContainer<Ban>>() {

                                    @Override
                                    public void send(SingleContainer<Ban> banSingleContainer) {
                                        ban.Properties.OwnerIds.remove(getLoggedInUser().Id);
                                        if (ban.Properties.OwnerIds.isEmpty()) {
                                            banMultiContainer.Properties.remove(ban);
                                        }
                                        setupDialog(dialog, banMultiContainer);
                                    }
                                }, getResources().getString(R.string.updating));
                            }
                        });
                        ((RelativeLayout) convertView.findViewById(R.id.ban_list_row_layout)).addView(button);
                    }
                }
            }

            @Override
            public void send(MultiContainer<Ban> banMultiContainer) {
                AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setView(R.layout.bans_dialog).show();
                setupDialog(dialog, banMultiContainer);
            }

            private void setupDialog(AlertDialog dialog, MultiContainer<Ban> banMultiContainer) {
                List<SingleContainer<Ban>> asBannerList = new ArrayList<>();
                List<SingleContainer<Ban>> asBannedList = new ArrayList<>();
                for (SingleContainer<Ban> banSingleContainer : banMultiContainer.Properties) {
                    if (banSingleContainer.Properties.OwnerIds.contains(getLoggedInUser().Id)) {
                        asBannerList.add(banSingleContainer);
                    } else {
                        asBannedList.add(banSingleContainer);
                    }
                }
                ViewGroup parent = (ViewGroup) dialog.findViewById(R.id.bans_dialog_layout);
                LinearLayout asBanner = (LinearLayout) dialog.findViewById(R.id.hater_list);
                if (asBannerList.isEmpty()) {
                    dialog.findViewById(R.id.as_banner_no_data).setVisibility(View.VISIBLE);
                    asBanner.setVisibility(View.GONE);
                } else {
                    dialog.findViewById(R.id.as_banner_no_data).setVisibility(View.GONE);
                    asBanner.removeAllViews();
                    for (SingleContainer<Ban> ban : asBannerList) {
                        View itemView = LayoutInflater.from(MainActivity.this).inflate(R.layout.ban_list_row, parent, false);
                        asBanner.addView(itemView);
                        setupView(dialog, banMultiContainer, ban, itemView);
                    }
                }
                LinearLayout asBanned = (LinearLayout) dialog.findViewById(R.id.hated_list);
                if (asBannedList.isEmpty()) {
                    dialog.findViewById(R.id.as_banned_no_data).setVisibility(View.VISIBLE);
                    asBanned.setVisibility(View.GONE);
                } else {
                    dialog.findViewById(R.id.as_banner_no_data).setVisibility(View.GONE);
                    asBanned.removeAllViews();
                    for (SingleContainer<Ban> ban : asBannedList) {
                        View itemView = LayoutInflater.from(MainActivity.this).inflate(R.layout.ban_list_row, parent, false);
                        asBanned.addView(itemView);
                        setupView(dialog, banMultiContainer, ban, itemView);
                    }
                }
            }
        }, getResources().getString(R.string.loading_bans));
    }
    return super.onOptionsItemSelected(item);
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) User(se.oort.diplicity.apigen.User) ArrayList(java.util.ArrayList) MultiContainer(se.oort.diplicity.apigen.MultiContainer) FloatingActionButton(android.support.design.widget.FloatingActionButton) TextView(android.widget.TextView) ViewGroup(android.view.ViewGroup) Intent(android.content.Intent) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) RecyclerView(android.support.v7.widget.RecyclerView) ExpandableListView(android.widget.ExpandableListView) Ban(se.oort.diplicity.apigen.Ban) SingleContainer(se.oort.diplicity.apigen.SingleContainer) LayoutInflater(android.view.LayoutInflater) RelativeLayout(android.widget.RelativeLayout) LinearLayout(android.widget.LinearLayout)

Aggregations

SingleContainer (se.oort.diplicity.apigen.SingleContainer)17 TextView (android.widget.TextView)13 View (android.view.View)12 AdapterView (android.widget.AdapterView)9 ArrayList (java.util.ArrayList)8 UserView (se.oort.diplicity.UserView)8 NavigationView (android.support.design.widget.NavigationView)7 ListView (android.widget.ListView)7 ScrollView (android.widget.ScrollView)7 MultiContainer (se.oort.diplicity.apigen.MultiContainer)7 FloatingActionButton (android.support.design.widget.FloatingActionButton)6 AlertDialog (android.support.v7.app.AlertDialog)6 List (java.util.List)4 HttpException (retrofit2.adapter.rxjava.HttpException)4 Sendable (se.oort.diplicity.Sendable)4 Ban (se.oort.diplicity.apigen.Ban)4 Member (se.oort.diplicity.apigen.Member)4 MotionEvent (android.view.MotionEvent)3 CheckBox (android.widget.CheckBox)3 CompoundButton (android.widget.CompoundButton)3