Project

General

Profile

Download (7.1 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / dialogs / RubikDialogTutorialView.java @ fcf7320f

1 234a7582 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.dialogs;
21
22
import android.content.Context;
23 b91d4033 Leszek Koltunski
import android.content.res.Resources;
24 992748ab Leszek Koltunski
import android.os.Bundle;
25 234a7582 Leszek Koltunski
import android.util.AttributeSet;
26 b91d4033 Leszek Koltunski
import android.util.TypedValue;
27 234a7582 Leszek Koltunski
import android.view.View;
28 b91d4033 Leszek Koltunski
import android.widget.Button;
29 234a7582 Leszek Koltunski
import android.widget.FrameLayout;
30 ef1f3e34 Leszek Koltunski
import android.widget.ImageView;
31 b91d4033 Leszek Koltunski
import android.widget.LinearLayout;
32 ef1f3e34 Leszek Koltunski
import android.widget.TextView;
33 234a7582 Leszek Koltunski
34
import androidx.fragment.app.FragmentActivity;
35
36 992748ab Leszek Koltunski
import com.google.firebase.analytics.FirebaseAnalytics;
37
38
import org.distorted.main.BuildConfig;
39 234a7582 Leszek Koltunski
import org.distorted.main.R;
40
import org.distorted.main.RubikActivity;
41 804293f0 Leszek Koltunski
import org.distorted.objectlib.json.JsonReader;
42 d433b50e Leszek Koltunski
import org.distorted.objects.RubikObject;
43
import org.distorted.objects.RubikObjectList;
44 804293f0 Leszek Koltunski
45
import java.io.InputStream;
46 234a7582 Leszek Koltunski
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49
public class RubikDialogTutorialView extends FrameLayout
50
  {
51 a84c3a25 Leszek Koltunski
  private boolean mCanClick;
52
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55 234a7582 Leszek Koltunski
  public RubikDialogTutorialView(Context context, AttributeSet attrs, int defStyle)
56
    {
57
    super(context, attrs, defStyle);
58
    }
59
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62
  public RubikDialogTutorialView(Context context, AttributeSet attrs)
63
    {
64
    super(context, attrs);
65
    }
66
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69 ef1f3e34 Leszek Koltunski
  public RubikDialogTutorialView(FragmentActivity act, int width, int position)
70 234a7582 Leszek Koltunski
    {
71
    super(act);
72
73 fcf7320f Leszek Koltunski
    InputStream jsonStream = RubikObjectList.getExtrasStream(position,act);
74 804293f0 Leszek Koltunski
    String[][] tutorials=null;
75
76
    if( jsonStream!=null )
77
      {
78
      JsonReader reader = JsonReader.getInstance();
79
      reader.parseJsonTutorial(jsonStream);
80
      tutorials = reader.getTutorials();
81
      }
82
83
    if( tutorials!=null )
84
      {
85
      clickPossible(true);
86
      int widthT = (int)(width* RubikActivity.TUTORIAL_ITEM_TEXT);
87 234a7582 Leszek Koltunski
88 804293f0 Leszek Koltunski
      RubikActivity ract = (RubikActivity)getContext();
89
      Resources res = act.getResources();
90
      String packageName = act.getPackageName();
91 b91d4033 Leszek Koltunski
92 804293f0 Leszek Koltunski
      View tab = inflate( act, R.layout.dialog_tutorial_tab, null);
93
      LinearLayout layout = tab.findViewById(R.id.tabLayout);
94 234a7582 Leszek Koltunski
95 804293f0 Leszek Koltunski
      int colorB = getResources().getColor(R.color.light_grey);
96
      int colorT = getResources().getColor(R.color.white);
97 234a7582 Leszek Koltunski
98 804293f0 Leszek Koltunski
      int objectOrdinal = RubikObjectList.getObjectOrdinal(position);
99 234a7582 Leszek Koltunski
100 804293f0 Leszek Koltunski
      for (String[] tutorial : tutorials)
101
        {
102
        String coun = tutorial[0];
103
        String url  = tutorial[1];
104
        String desc = tutorial[2];
105
        String auth = tutorial[3];
106 6bc01aa4 Leszek Koltunski
107 804293f0 Leszek Koltunski
        int countryID = res.getIdentifier(coun, "drawable", packageName);
108 b91d4033 Leszek Koltunski
109 804293f0 Leszek Koltunski
        View row = createRow(ract, countryID, desc, url, auth, widthT, objectOrdinal, colorB, colorT);
110
        layout.addView(row);
111
        }
112 b91d4033 Leszek Koltunski
113 804293f0 Leszek Koltunski
      addView(tab);
114 b91d4033 Leszek Koltunski
      }
115 234a7582 Leszek Koltunski
    }
116 b91d4033 Leszek Koltunski
117 a84c3a25 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
118
119
  void clickPossible(boolean click)
120
    {
121
    mCanClick = click;
122
    }
123
124 b91d4033 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
125
126 992748ab Leszek Koltunski
  private View createRow(final RubikActivity act, int countryID, final String desc, final String url,
127 d433b50e Leszek Koltunski
                         final String auth, int size, final int obj, int colorB, int colorT)
128 b91d4033 Leszek Koltunski
    {
129 cb6d9c37 Leszek Koltunski
    float textSize = 0.5f*size;
130 b91d4033 Leszek Koltunski
    View row = inflate( act, R.layout.dialog_tutorial_row, null);
131
132 cb6d9c37 Leszek Koltunski
    LinearLayout layout = row.findViewById(R.id.tutorialLayout);
133
    layout.setMinimumHeight(size);
134
135
    Button butt = row.findViewById(R.id.tutorialButton);
136 ef1f3e34 Leszek Koltunski
    butt.setText(R.string.view);
137 6bc01aa4 Leszek Koltunski
    butt.setTextColor(colorT);
138
    butt.setBackgroundColor(colorB);
139 cb6d9c37 Leszek Koltunski
    butt.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
140
    butt.setHeight(size);
141 b91d4033 Leszek Koltunski
142
    butt.setOnClickListener( new View.OnClickListener()
143
      {
144
      @Override
145
      public void onClick(View v)
146
        {
147 a84c3a25 Leszek Koltunski
        if( mCanClick )
148
          {
149 7ac0ee88 Leszek Koltunski
          act.switchTutorial(url,obj);
150
          analyticsReport(act,desc,auth,obj);
151 a84c3a25 Leszek Koltunski
          clickPossible(false);
152
          }
153 b91d4033 Leszek Koltunski
        }
154
      });
155
156 ef1f3e34 Leszek Koltunski
    ImageView image = row.findViewById(R.id.tutorialCountry);
157
    int id = countryID!=0 ? countryID : R.drawable.un;
158
    image.setImageResource(id);
159
160
    TextView author = row.findViewById(R.id.tutorialAuthor);
161 cb6d9c37 Leszek Koltunski
    author.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
162 ef1f3e34 Leszek Koltunski
    author.setText(auth);
163
164
    TextView title  = row.findViewById(R.id.tutorialTitle);
165 cb6d9c37 Leszek Koltunski
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
166 ef1f3e34 Leszek Koltunski
    title.setText(desc);
167
168 b91d4033 Leszek Koltunski
    return row;
169
    }
170 992748ab Leszek Koltunski
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
173 d433b50e Leszek Koltunski
  private void analyticsReport(RubikActivity act, String desc, String author, int obj)
174 992748ab Leszek Koltunski
    {
175
    String message = desc+" ("+author+")";
176
177
    if( BuildConfig.DEBUG )
178
       {
179
       android.util.Log.d("tutorial", message);
180
       }
181
    else
182
      {
183
      FirebaseAnalytics analytics = act.getAnalytics();
184
185
      if( analytics!=null )
186
        {
187 d433b50e Leszek Koltunski
        RubikObject object = RubikObjectList.getObject(obj);
188
189 992748ab Leszek Koltunski
        Bundle bundle = new Bundle();
190
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, message);
191 d433b50e Leszek Koltunski
        bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, object==null ? "NULL" : object.getName() );
192 992748ab Leszek Koltunski
        analytics.logEvent(FirebaseAnalytics.Event.TUTORIAL_BEGIN, bundle);
193
        }
194
      }
195
    }
196 234a7582 Leszek Koltunski
  }