Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogTutorialView.java @ 05c044a5

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 ignored) { }
77
      }
78

    
79
    if( tutorials!=null )
80
      {
81
      clickPossible(true);
82
      int widthT = (int)(width* RubikActivity.TUTORIAL_ITEM_TEXT);
83

    
84
      RubikActivity ract = (RubikActivity)getContext();
85
      Resources res = act.getResources();
86
      String packageName = act.getPackageName();
87

    
88
      View tab = inflate( act, R.layout.dialog_tutorial_tab, null);
89
      LinearLayout layout = tab.findViewById(R.id.tabLayout);
90

    
91
      int colorB = getResources().getColor(R.color.light_grey);
92
      int colorT = getResources().getColor(R.color.white);
93

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

    
101
        int countryID = res.getIdentifier(coun, "drawable", packageName);
102

    
103
        View row = createRow(ract, countryID, desc, url, auth, widthT, objectOrdinal, colorB, colorT);
104
        layout.addView(row);
105
        }
106

    
107
      addView(tab);
108
      }
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  void clickPossible(boolean click)
114
    {
115
    mCanClick = click;
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

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

    
126
    LinearLayout layout = row.findViewById(R.id.tutorialLayout);
127
    layout.setMinimumHeight(size);
128

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

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

    
150
    ImageView image = row.findViewById(R.id.tutorialCountry);
151
    int id = countryID!=0 ? countryID : org.distorted.flags.R.drawable.unknown;
152
    image.setImageResource(id);
153

    
154
    TextView author = row.findViewById(R.id.tutorialAuthor);
155
    author.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
156
    author.setText(auth);
157

    
158
    TextView title  = row.findViewById(R.id.tutorialTitle);
159
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
160
    title.setText(desc);
161

    
162
    return row;
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  private void analyticsReport(RubikActivity act, String desc, String author, int obj)
168
    {
169
    String message = desc+" ("+author+")";
170

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

    
179
      if( analytics!=null )
180
        {
181
        RubikObject object = RubikObjectList.getObject(obj);
182

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