Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogTutorialView.java @ 298f3977

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.dialogs;
11

    
12
import android.content.Context;
13
import android.content.res.Resources;
14
import android.os.Bundle;
15
import android.util.AttributeSet;
16
import android.util.TypedValue;
17
import android.view.View;
18
import android.widget.Button;
19
import android.widget.FrameLayout;
20
import android.widget.ImageView;
21
import android.widget.LinearLayout;
22
import android.widget.TextView;
23

    
24
import androidx.fragment.app.FragmentActivity;
25

    
26
import com.google.firebase.analytics.FirebaseAnalytics;
27

    
28
import org.distorted.main.BuildConfig;
29
import org.distorted.main.R;
30
import org.distorted.main.RubikActivity;
31
import org.distorted.objectlib.json.JsonReader;
32
import org.distorted.objects.RubikObject;
33
import org.distorted.objects.RubikObjectList;
34

    
35
import java.io.InputStream;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class RubikDialogTutorialView extends FrameLayout
40
  {
41
  private boolean mCanClick;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
  public RubikDialogTutorialView(Context context, AttributeSet attrs, int defStyle)
46
    {
47
    super(context, attrs, defStyle);
48
    }
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
  public RubikDialogTutorialView(Context context, AttributeSet attrs)
53
    {
54
    super(context, attrs);
55
    }
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  public RubikDialogTutorialView(FragmentActivity act, int width, int position)
60
    {
61
    super(act);
62

    
63
    int objectOrdinal = RubikObjectList.getObjectOrdinal(position);
64
    RubikObject robject = RubikObjectList.getObject(objectOrdinal);
65
    InputStream jsonStream = robject==null ? null : robject.getExtrasStream(act);
66
    String[][] tutorials=null;
67

    
68
    if( jsonStream!=null )
69
      {
70
      try
71
        {
72
        JsonReader reader = JsonReader.getInstance();
73
        reader.parseJsonTutorial(jsonStream);
74
        tutorials = reader.getTutorials();
75
        }
76
      catch(Exception ex)
77
        {
78
        tutorials = null;
79
        }
80
      }
81

    
82
    if( tutorials!=null )
83
      {
84
      clickPossible(true);
85
      int widthT = (int)(width* RubikActivity.TUTORIAL_ITEM_TEXT);
86

    
87
      RubikActivity ract = (RubikActivity)getContext();
88
      Resources res = act.getResources();
89
      String packageName = act.getPackageName();
90

    
91
      View tab = inflate( act, R.layout.dialog_tutorial_tab, null);
92
      LinearLayout layout = tab.findViewById(R.id.tabLayout);
93

    
94
      int colorB = getResources().getColor(R.color.light_grey);
95
      int colorT = getResources().getColor(R.color.white);
96

    
97
      for (String[] tutorial : tutorials)
98
        {
99
        String coun = tutorial[0];
100
        String url  = tutorial[1];
101
        String desc = tutorial[2];
102
        String auth = tutorial[3];
103

    
104
        int countryID = res.getIdentifier(coun, "drawable", packageName);
105

    
106
        View row = createRow(ract, countryID, desc, url, auth, widthT, objectOrdinal, colorB, colorT);
107
        layout.addView(row);
108
        }
109

    
110
      addView(tab);
111
      }
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  void clickPossible(boolean click)
117
    {
118
    mCanClick = click;
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  private View createRow(final RubikActivity act, int countryID, final String desc, final String url,
124
                         final String auth, int size, final int obj, int colorB, int colorT)
125
    {
126
    float textSize = 0.5f*size;
127
    View row = inflate( act, R.layout.dialog_tutorial_row, null);
128

    
129
    LinearLayout layout = row.findViewById(R.id.tutorialLayout);
130
    layout.setMinimumHeight(size);
131

    
132
    Button butt = row.findViewById(R.id.tutorialButton);
133
    butt.setText(R.string.view);
134
    butt.setTextColor(colorT);
135
    butt.setBackgroundColor(colorB);
136
    butt.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
137
    butt.setHeight(size);
138

    
139
    butt.setOnClickListener( new View.OnClickListener()
140
      {
141
      @Override
142
      public void onClick(View v)
143
        {
144
        if( mCanClick )
145
          {
146
          act.switchTutorial(url,obj);
147
          analyticsReport(act,desc,auth,obj);
148
          clickPossible(false);
149
          }
150
        }
151
      });
152

    
153
    ImageView image = row.findViewById(R.id.tutorialCountry);
154
    int id = countryID!=0 ? countryID : R.drawable.un;
155
    image.setImageResource(id);
156

    
157
    TextView author = row.findViewById(R.id.tutorialAuthor);
158
    author.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
159
    author.setText(auth);
160

    
161
    TextView title  = row.findViewById(R.id.tutorialTitle);
162
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
163
    title.setText(desc);
164

    
165
    return row;
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  private void analyticsReport(RubikActivity act, String desc, String author, int obj)
171
    {
172
    String message = desc+" ("+author+")";
173

    
174
    if( BuildConfig.DEBUG )
175
       {
176
       android.util.Log.d("tutorial", message);
177
       }
178
    else
179
      {
180
      FirebaseAnalytics analytics = act.getAnalytics();
181

    
182
      if( analytics!=null )
183
        {
184
        RubikObject object = RubikObjectList.getObject(obj);
185

    
186
        Bundle bundle = new Bundle();
187
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, message);
188
        bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, object==null ? "NULL" : object.getLowerName() );
189
        analytics.logEvent(FirebaseAnalytics.Event.TUTORIAL_BEGIN, bundle);
190
        }
191
      }
192
    }
193
  }
(21-21/23)