Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresThread.java @ 4bd09fe2

1 8f139bfb leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
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.widget.LinearLayout;
13
14
import androidx.fragment.app.FragmentActivity;
15
import androidx.viewpager.widget.ViewPager;
16
17
import org.distorted.main.R;
18
19
import java.lang.ref.WeakReference;
20
import java.util.Vector;
21
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23
24
class RubikDialogScoresThread extends Thread
25
  {
26
  private final Object mObject;
27
  private final Vector<WorkLoad> mBuffers;
28
  private WeakReference<FragmentActivity> mWeakAct;
29
  private ViewPager mViewPager;
30
  private boolean mRunning;
31
32
  private static RubikDialogScoresThread mThis;
33
34
  private static class WorkLoad
35
    {
36
    int tab, level, numLevels;
37
    RubikDialogScoresView view;
38
    String[] country, name;
39
    int[] time;
40
41
    WorkLoad(int t, int l, int nl, RubikDialogScoresView v, String[] c, String[] n, int[] tm)
42
      {
43
      tab = t;
44
      level = l;
45
      numLevels = nl;
46
      view = v;
47
      country = c;
48
      name = n;
49
      time = tm;
50
      }
51
    }
52
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55
  private RubikDialogScoresThread()
56
    {
57
    mObject  = new Object();
58
    mRunning = true;
59
    mBuffers = new Vector<>();
60
    this.start();
61
    }
62
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
  static RubikDialogScoresThread getInstance()
66
    {
67
    if( mThis==null ) mThis = new RubikDialogScoresThread();
68
    return mThis;
69
    }
70
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
73
  void equip(FragmentActivity act, ViewPager pager)
74
    {
75
    mWeakAct = new WeakReference<>(act);
76
    mViewPager = pager;
77
    }
78
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
81
  void newWork(int t, int l, int nl, RubikDialogScoresView v, String[] c, String[] n, int[] tm)
82
    {
83
    synchronized(mObject)
84
      {
85
      WorkLoad load = new WorkLoad(t,l,nl,v,c,n,tm);
86
      mBuffers.add(load);
87
      mObject.notify();
88
      }
89
    }
90
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
93
  void exit()
94
    {
95
    synchronized(mObject)
96
      {
97
      mRunning = false;
98
      mThis = null;
99
      mObject.notify();
100
      }
101
    }
102
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
105
  public void run()
106
    {
107
    WorkLoad load;
108
109
    while(true)
110
      {
111
      synchronized(mObject)
112
        {
113
        do
114
          {
115
          load = getNextLoad();
116
          if( load!=null ) process(load);
117
          }
118
        while(load!=null);
119
120
        if( mRunning )
121
          {
122
          try { mObject.wait(); }
123
          catch(InterruptedException ignored) { }
124
          }
125
        else break;
126
        }
127
      }
128
    }
129
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
132
  private WorkLoad getNextLoad()
133
    {
134
    if( mViewPager==null ) return null;
135
136
    int currentTab = mViewPager.getCurrentItem();
137
    int size = mBuffers.size();
138
139
    for(int i=0; i<size; i++)
140
      {
141
      WorkLoad load = mBuffers.get(i);
142
      if( load.tab==currentTab ) return mBuffers.remove(i);
143
      }
144
145
    return size>0 ? mBuffers.remove(0) : null;
146
    }
147
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
150
  private void process(WorkLoad load)
151
    {
152
    final FragmentActivity act = mWeakAct.get();
153
    int tab = load.tab;
154
    int level = load.level;
155
    int numLevels = load.numLevels;
156
    RubikDialogScoresView view = load.view;
157
    String[] country = load.country;
158
    String[] name = load.name;
159
    int[] time = load.time;
160
    String title = level==numLevels ? act.getString(R.string.levelM) : act.getString(R.string.lv_placeholder, level+1);
161
162 bae9a515 leszek
    if( view!=null )
163 8f139bfb leszek
      {
164 bae9a515 leszek
      final LinearLayout section = view.createSection(act, tab, title, level, country, name, time);
165
166
      act.runOnUiThread(new Runnable()
167 8f139bfb leszek
        {
168 bae9a515 leszek
        @Override
169
        public void run()
170
          {
171
          view.addSection(act, section);
172
          }
173
        });
174
      }
175 8f139bfb leszek
    }
176
  }