Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogTutorialView.java @ 5e6d3e7d

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
  public static final float PANE_HEIGHT  = 0.06f;
42
  private boolean mCanClick;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

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

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

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

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  public RubikDialogTutorialView(FragmentActivity act, int height, int position)
61
    {
62
    super(act);
63

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

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

    
80
    if( tutorials!=null )
81
      {
82
      clickPossible(true);
83
      int size = (int)(height*PANE_HEIGHT);
84

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

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

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

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

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

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

    
108
      addView(tab);
109
      }
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

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

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

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

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

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

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

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

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

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

    
163
    return row;
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

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

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

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

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