Project

General

Profile

« Previous | Next » 

Revision 45831b78

Added by Leszek Koltunski over 1 year ago

Purchase Pane: post-purchase dialogs.

View differences:

src/main/java/org/distorted/purchase/PurchaseScreenPane.java
10 10
package org.distorted.purchase;
11 11

  
12 12
import android.graphics.drawable.Drawable;
13
import android.os.Bundle;
13 14
import android.util.TypedValue;
14 15
import android.view.View;
15 16
import android.widget.ImageButton;
......
19 20

  
20 21
import androidx.core.content.res.ResourcesCompat;
21 22

  
23
import org.distorted.dialogs.RubikDialogStarsError;
24
import org.distorted.external.RubikScores;
22 25
import org.distorted.main.R;
23 26
import org.distorted.objectlib.json.JsonReader;
24 27
import org.distorted.objects.RubikObject;
......
30 33

  
31 34
public class PurchaseScreenPane
32 35
{
33
  private static final int UNLOCK_ALL_PRICE = 500;
36
  public static final int UNLOCK_ALL_PRICE = 500;
34 37

  
35 38
  private static final int[] IMAGES =
36 39
    {
......
48 51
  private RubikObject mObject;
49 52

  
50 53
///////////////////////////////////////////////////////////////////////////////////////////////////
51
// TODO: charge the user an amount of stars
52 54

  
53
  private boolean chargeUser(RubikObject object)
55
  private boolean chargeUser(int amount)
54 56
    {
55
    return true;
57
    RubikScores scores = RubikScores.getInstance();
58
    int numStars = scores.getNumStars();
59

  
60
    if( numStars>=amount )
61
      {
62
      scores.changeNumStars(-amount);
63
      return true;
64
      }
65

  
66
    return false;
56 67
    }
57 68

  
58 69
///////////////////////////////////////////////////////////////////////////////////////////////////
59
// TODO: charge the user an amount of stars
60 70

  
61
  private boolean chargeUser()
71
  private void showError(PurchaseActivity act, int price)
62 72
    {
63
    return true;
73
    Bundle bundle = new Bundle();
74
    bundle.putInt("price", price );
75
    RubikDialogStarsError d = new RubikDialogStarsError();
76
    d.setArguments(bundle);
77
    d.show(act.getSupportFragmentManager(), null);
64 78
    }
65 79

  
66 80
///////////////////////////////////////////////////////////////////////////////////////////////////
67 81

  
68
  private void oneButtonClicked()
82
  private void showSuccess(PurchaseActivity act, RubikObject object)
83
    {
84
    Bundle bundle = new Bundle();
85
    bundle.putString("object", object.getUpperName() );
86
    RubikDialogStarsError d = new RubikDialogStarsError();
87
    d.setArguments(bundle);
88
    d.show(act.getSupportFragmentManager(), null);
89
    }
90

  
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

  
93
  private void oneButtonClicked(PurchaseActivity act)
69 94
    {
70 95
    if( mObject!=null )
71 96
      {
72
      if( !RubikObjectList.objectAlreadyBought(mObject) && chargeUser(mObject) )
97
      int price = mObject.getPrice();
98

  
99
      if( chargeUser(price) )
73 100
        {
74
        android.util.Log.e("D", "buying "+mObject.getUpperName());
75 101
        RubikObjectList.buyObject(mObject);
102
        showSuccess(act,mObject);
103
        }
104
      else
105
        {
106
        showError(act,price);
76 107
        }
77 108
      }
78 109
    }
79 110

  
80 111
///////////////////////////////////////////////////////////////////////////////////////////////////
81 112

  
82
  private void allButtonClicked()
113
  private void allButtonClicked(PurchaseActivity act)
83 114
    {
84
    if( !RubikObjectList.allAlreadyBought() && chargeUser() )
115
    int price = UNLOCK_ALL_PRICE;
116

  
117
    if( chargeUser(price) )
85 118
      {
86 119
      RubikObjectList.buyAll();
120
      showSuccess(act,null);
121
      }
122
    else
123
      {
124
      showError(act,price);
87 125
      }
88 126
    }
89 127

  
......
115 153
      @Override
116 154
      public void onClick(View v)
117 155
        {
118
        oneButtonClicked();
156
        oneButtonClicked(act);
119 157
        }
120 158
      });
121 159

  
......
124 162
      @Override
125 163
      public void onClick(View v)
126 164
        {
127
        allButtonClicked();
165
        allButtonClicked(act);
128 166
        }
129 167
      });
130 168
    }

Also available in: Unified diff