Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogTutorialView.java @ 280dc794

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 d433b50e Leszek Koltunski
import org.distorted.objects.RubikObject;
42
import org.distorted.objects.RubikObjectList;
43 eaf87d1d Leszek Koltunski
import org.distorted.tutorials.TutorialList;
44 234a7582 Leszek Koltunski
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
47
public class RubikDialogTutorialView extends FrameLayout
48
  {
49 a84c3a25 Leszek Koltunski
  private boolean mCanClick;
50
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 234a7582 Leszek Koltunski
  public RubikDialogTutorialView(Context context, AttributeSet attrs, int defStyle)
54
    {
55
    super(context, attrs, defStyle);
56
    }
57
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60
  public RubikDialogTutorialView(Context context, AttributeSet attrs)
61
    {
62
    super(context, attrs);
63
    }
64
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67 ef1f3e34 Leszek Koltunski
  public RubikDialogTutorialView(FragmentActivity act, int width, int position)
68 234a7582 Leszek Koltunski
    {
69
    super(act);
70
71 a84c3a25 Leszek Koltunski
    clickPossible(true);
72 ef1f3e34 Leszek Koltunski
    int widthT = (int)(width* RubikActivity.TUTORIAL_ITEM_TEXT);
73 234a7582 Leszek Koltunski
74 b91d4033 Leszek Koltunski
    RubikActivity ract = (RubikActivity)getContext();
75
    Resources res = act.getResources();
76
    String packageName = act.getPackageName();
77
78 992748ab Leszek Koltunski
    TutorialList list  = TutorialList.getObject(position);
79 d433b50e Leszek Koltunski
    int object = list.getObject();
80 234a7582 Leszek Koltunski
81
    View tab = inflate( act, R.layout.dialog_tutorial_tab, null);
82 b91d4033 Leszek Koltunski
    LinearLayout layout = tab.findViewById(R.id.tabLayout);
83 234a7582 Leszek Koltunski
84 992748ab Leszek Koltunski
    int numTutorials = list.getNumTutorials();
85 234a7582 Leszek Koltunski
86 6bc01aa4 Leszek Koltunski
    int colorB = getResources().getColor(R.color.light_grey);
87
    int colorT = getResources().getColor(R.color.white);
88
89 b91d4033 Leszek Koltunski
    for(int i=0; i<numTutorials; i++)
90
      {
91 992748ab Leszek Koltunski
      String coun = list.getTutorialLanguage(i);
92
      String desc = list.getTutorialDescription(i);
93
      String url  = list.getTutorialURL(i);
94
      String auth = list.getTutorialAuthor(i);
95 b91d4033 Leszek Koltunski
96
      int countryID = res.getIdentifier( coun, "drawable", packageName);
97
98 d433b50e Leszek Koltunski
      View row = createRow(ract,countryID,desc,url,auth,widthT,object,colorB,colorT);
99 b91d4033 Leszek Koltunski
      layout.addView(row);
100
      }
101 234a7582 Leszek Koltunski
102
    addView(tab);
103
    }
104 b91d4033 Leszek Koltunski
105 a84c3a25 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
106
107
  void clickPossible(boolean click)
108
    {
109
    mCanClick = click;
110
    }
111
112 b91d4033 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114 992748ab Leszek Koltunski
  private View createRow(final RubikActivity act, int countryID, final String desc, final String url,
115 d433b50e Leszek Koltunski
                         final String auth, int size, final int obj, int colorB, int colorT)
116 b91d4033 Leszek Koltunski
    {
117 cb6d9c37 Leszek Koltunski
    float textSize = 0.5f*size;
118 b91d4033 Leszek Koltunski
    View row = inflate( act, R.layout.dialog_tutorial_row, null);
119
120 cb6d9c37 Leszek Koltunski
    LinearLayout layout = row.findViewById(R.id.tutorialLayout);
121
    layout.setMinimumHeight(size);
122
123
    Button butt = row.findViewById(R.id.tutorialButton);
124 ef1f3e34 Leszek Koltunski
    butt.setText(R.string.view);
125 6bc01aa4 Leszek Koltunski
    butt.setTextColor(colorT);
126
    butt.setBackgroundColor(colorB);
127 cb6d9c37 Leszek Koltunski
    butt.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
128
    butt.setHeight(size);
129 b91d4033 Leszek Koltunski
130
    butt.setOnClickListener( new View.OnClickListener()
131
      {
132
      @Override
133
      public void onClick(View v)
134
        {
135 a84c3a25 Leszek Koltunski
        if( mCanClick )
136
          {
137 7ac0ee88 Leszek Koltunski
          act.switchTutorial(url,obj);
138
          analyticsReport(act,desc,auth,obj);
139 a84c3a25 Leszek Koltunski
          clickPossible(false);
140
          }
141 b91d4033 Leszek Koltunski
        }
142
      });
143
144 ef1f3e34 Leszek Koltunski
    ImageView image = row.findViewById(R.id.tutorialCountry);
145
    int id = countryID!=0 ? countryID : R.drawable.un;
146
    image.setImageResource(id);
147
148
    TextView author = row.findViewById(R.id.tutorialAuthor);
149 cb6d9c37 Leszek Koltunski
    author.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
150 ef1f3e34 Leszek Koltunski
    author.setText(auth);
151
152
    TextView title  = row.findViewById(R.id.tutorialTitle);
153 cb6d9c37 Leszek Koltunski
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
154 ef1f3e34 Leszek Koltunski
    title.setText(desc);
155
156 b91d4033 Leszek Koltunski
    return row;
157
    }
158 992748ab Leszek Koltunski
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161 d433b50e Leszek Koltunski
  private void analyticsReport(RubikActivity act, String desc, String author, int obj)
162 992748ab Leszek Koltunski
    {
163
    String message = desc+" ("+author+")";
164
165
    if( BuildConfig.DEBUG )
166
       {
167
       android.util.Log.d("tutorial", message);
168
       }
169
    else
170
      {
171
      FirebaseAnalytics analytics = act.getAnalytics();
172
173
      if( analytics!=null )
174
        {
175 d433b50e Leszek Koltunski
        RubikObject object = RubikObjectList.getObject(obj);
176
177 992748ab Leszek Koltunski
        Bundle bundle = new Bundle();
178
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, message);
179 d433b50e Leszek Koltunski
        bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, object==null ? "NULL" : object.getName() );
180 992748ab Leszek Koltunski
        analytics.logEvent(FirebaseAnalytics.Event.TUTORIAL_BEGIN, bundle);
181
        }
182
      }
183
    }
184 234a7582 Leszek Koltunski
  }