Search in sources :

Example 1 with GameState

use of se.oort.diplicity.apigen.GameState 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 2 with GameState

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

the class GameActivity method showGameStates.

public void showGameStates() {
    hideAllExcept(R.id.game_state_view);
    if (member == null) {
        findViewById(R.id.edit_game_state_button).setVisibility(View.GONE);
    }
    handleReq(gameStateService.ListGameStates(game.ID), new Sendable<MultiContainer<GameState>>() {

        @Override
        public void send(MultiContainer<GameState> gameStateMultiContainer) {
            GameState myState = null;
            TableLayout mutedTable = (TableLayout) findViewById(R.id.muted_table);
            mutedTable.removeAllViews();
            FloatingActionButton button = ((FloatingActionButton) mutedTable.findViewById(R.id.open_button));
            if (button != null) {
                mutedTable.removeView(button);
            }
            final List<String> nations = new ArrayList<String>();
            for (Member thisMember : game.Members) {
                if (member == null || !thisMember.Nation.equals(member.Nation)) {
                    nations.add(thisMember.Nation);
                }
                GameState foundState = null;
                for (SingleContainer<GameState> singleContainer : gameStateMultiContainer.Properties) {
                    if (singleContainer.Properties.Nation.equals(thisMember.Nation)) {
                        foundState = singleContainer.Properties;
                    }
                }
                if (member != null && thisMember.Nation.equals(member.Nation)) {
                    myState = foundState;
                }
                TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
                int margin = getResources().getDimensionPixelSize(R.dimen.muted_table_margin);
                params.bottomMargin = margin;
                params.topMargin = margin;
                params.leftMargin = margin;
                params.rightMargin = margin;
                TableRow tableRow = new TableRow(GameActivity.this);
                tableRow.setLayoutParams(params);
                LinearLayout playerSide = new LinearLayout(GameActivity.this);
                LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                playerSide.setLayoutParams(params);
                playerSide.setOrientation(LinearLayout.VERTICAL);
                UserView user = new UserView(GameActivity.this, null);
                user.setUser(GameActivity.this, thisMember.User, true);
                user.setLayoutParams(linearParams);
                playerSide.addView(user);
                TextView nation = new TextView(GameActivity.this);
                nation.setText(thisMember.Nation);
                nation.setLayoutParams(linearParams);
                playerSide.addView(nation);
                tableRow.addView(playerSide);
                TextView muteds = new TextView(GameActivity.this);
                muteds.setLayoutParams(params);
                if (foundState != null && foundState.Muted != null) {
                    muteds.setText(TextUtils.join(", ", foundState.Muted));
                } else {
                    muteds.setText("");
                }
                tableRow.addView(muteds);
                mutedTable.addView(tableRow);
            }
            final GameState finalMyState = myState;
            if (finalMyState != null) {
                ((FloatingActionButton) findViewById(R.id.edit_game_state_button)).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        final boolean[] checked = new boolean[nations.size()];
                        if (finalMyState.Muted != null) {
                            for (String muted : finalMyState.Muted) {
                                int pos = nations.indexOf(muted);
                                if (pos > -1) {
                                    checked[pos] = true;
                                }
                            }
                        }
                        final AlertDialog dialog = new AlertDialog.Builder(GameActivity.this).setMultiChoiceItems(nations.toArray(new String[] {}), checked, new DialogInterface.OnMultiChoiceClickListener() {

                            @Override
                            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                                checked[i] = b;
                            }
                        }).setTitle(R.string.muted).setPositiveButton(R.string.update, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialogInterface, int j) {
                                List<String> mutedMembers = new ArrayList<String>();
                                for (int i = 0; i < checked.length; i++) {
                                    if (checked[i]) {
                                        mutedMembers.add(nations.get(i));
                                    }
                                }
                                Collections.sort(mutedMembers);
                                finalMyState.Muted = mutedMembers;
                                handleReq(gameStateService.GameStateUpdate(finalMyState, game.ID, finalMyState.Nation), new Sendable<SingleContainer<GameState>>() {

                                    @Override
                                    public void send(SingleContainer<GameState> gameStateSingleContainer) {
                                        showGameStates();
                                    }
                                }, getResources().getString(R.string.updating_game_state));
                            }
                        }).show();
                    }
                });
            }
        }
    }, getResources().getString(R.string.loading_game_settings));
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList) UserView(se.oort.diplicity.UserView) MultiContainer(se.oort.diplicity.apigen.MultiContainer) FloatingActionButton(android.support.design.widget.FloatingActionButton) List(java.util.List) ArrayList(java.util.ArrayList) TextView(android.widget.TextView) TableLayout(android.widget.TableLayout) Member(se.oort.diplicity.apigen.Member) GameState(se.oort.diplicity.apigen.GameState) 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) SingleContainer(se.oort.diplicity.apigen.SingleContainer) TableRow(android.widget.TableRow) LinearLayout(android.widget.LinearLayout)

Aggregations

AlertDialog (android.support.v7.app.AlertDialog)2 View (android.view.View)2 TextView (android.widget.TextView)2 ArrayList (java.util.ArrayList)2 GameState (se.oort.diplicity.apigen.GameState)2 Member (se.oort.diplicity.apigen.Member)2 SingleContainer (se.oort.diplicity.apigen.SingleContainer)2 DialogInterface (android.content.DialogInterface)1 FloatingActionButton (android.support.design.widget.FloatingActionButton)1 NavigationView (android.support.design.widget.NavigationView)1 AdapterView (android.widget.AdapterView)1 CheckBox (android.widget.CheckBox)1 CompoundButton (android.widget.CompoundButton)1 ImageView (android.widget.ImageView)1 LinearLayout (android.widget.LinearLayout)1 ListView (android.widget.ListView)1 ScrollView (android.widget.ScrollView)1 TableLayout (android.widget.TableLayout)1 TableRow (android.widget.TableRow)1 List (java.util.List)1