@@ -53,12 +53,15 @@ public class MainPresenterImpl implements MainPresenter {
53
53
private List <Note > notes ;
54
54
private final List <Note > filteredNotes ;
55
55
56
+ private ProgressDialog progressDialog ;
57
+
56
58
public MainPresenterImpl (Context context , @ NonNull Activity activity , MainView mainView ) {
57
59
this .context = context ;
58
60
this .activity = activity ;
59
61
this .mainView = mainView ;
60
62
filteredNotes = new ArrayList <>();
61
63
notes = new ArrayList <>();
64
+ progressDialog = new ProgressDialog (activity , R .style .CustomProgressDialog );
62
65
}
63
66
64
67
@ Override
@@ -87,93 +90,116 @@ public void initMobileSdk() {
87
90
});
88
91
}
89
92
93
+ private void showProgressDialog () {
94
+ progressDialog .showDialog ();
95
+ }
96
+
97
+ private void dismissProgressDialog () {
98
+ progressDialog .dismiss ();
99
+ }
100
+
90
101
91
102
@ Override
92
103
public void initNotes (Conditions conditions ) {
93
- mainView .showProgressDialog ();
94
- if (conditions instanceof Conditions .Search ) {
95
- filteredNotes .clear ();
96
- String query = ((Conditions .Search ) conditions ).getQuery ();
97
-
98
- for (Note note : notes ) {
99
- if (note .getNoteName ().contains (query ) ||
100
- note .getNoteText ().contains (query ) ||
101
- new SimpleDateFormat ("dd.MM.yyyy HH.mm" , Locale .getDefault ()).format (new Date (note .getAddNoteTime ())).contains (query )
102
- ) {
103
- filteredNotes .add (note );
104
+ new Thread (() -> {
105
+ if (conditions instanceof Conditions .Search ) {
106
+ filteredNotes .clear ();
107
+ String query = ((Conditions .Search ) conditions ).getQuery ();
108
+
109
+ for (Note note : notes ) {
110
+ if (note .getNoteName ().contains (query ) ||
111
+ note .getNoteText ().contains (query ) ||
112
+ new SimpleDateFormat ("dd.MM.yyyy HH.mm" , Locale .getDefault ()).format (new Date (note .getAddNoteTime ())).contains (query )
113
+ ) {
114
+ filteredNotes .add (note );
115
+ }
116
+ }
117
+ RecyclerView .LayoutManager layoutManager ;
118
+ if (isList ) {
119
+ layoutManager = new LinearLayoutManager (context );
120
+ } else {
121
+ layoutManager = new StaggeredGridLayoutManager (2 , StaggeredGridLayoutManager .VERTICAL );
104
122
}
123
+
124
+ mainView .initRecyclerView (filteredNotes , layoutManager );
105
125
}
106
- RecyclerView .LayoutManager layoutManager ;
107
- if (isList ) {
108
- layoutManager = new LinearLayoutManager (context );
109
- } else {
110
- layoutManager = new StaggeredGridLayoutManager (2 , StaggeredGridLayoutManager .VERTICAL );
126
+ else if (conditions instanceof Conditions .All ) {
127
+ notes .clear ();
128
+ SQLiteDatabaseManager sqLiteDatabaseManager = new SQLiteDatabaseManager (context );
129
+ sqLiteDatabaseNotes = sqLiteDatabaseManager .getWritableDatabase ();
130
+ notes = loadNotes ();
131
+
132
+ isList = true ;
133
+
134
+ activity .runOnUiThread (() -> {
135
+ mainView .changeFilterButtonImage (R .drawable .ic_baseline_filter_list );
136
+ mainView .initRecyclerView (notes , new LinearLayoutManager (context ));
137
+ });
111
138
}
112
-
113
- mainView .initRecyclerView (filteredNotes , layoutManager );
114
- } else if (conditions instanceof Conditions .All ) {
115
- notes .clear ();
116
- SQLiteDatabaseManager sqLiteDatabaseManager = new SQLiteDatabaseManager (context );
117
- sqLiteDatabaseNotes = sqLiteDatabaseManager .getWritableDatabase ();
118
- notes = loadNotes ();
119
-
120
- isList = true ;
121
- mainView .changeFilterButtonImage (R .drawable .ic_baseline_filter_list );
122
- mainView .initRecyclerView (notes , new LinearLayoutManager (context ));
123
- }
124
- mainView .dismissProgressDialog ();
139
+ }).start ();
125
140
}
126
141
127
142
@ Override
128
143
public void initOpenAds () {
129
- mainView . showProgressDialog ( );
144
+ activity . runOnUiThread ( this :: showProgressDialog );
130
145
final AppOpenAdLoader appOpenAdLoader = new AppOpenAdLoader (context );
131
146
final AdRequestConfiguration adRequestConfiguration = new AdRequestConfiguration .Builder (AdsIds .OPEN_AD_UNIT_ID ).build ();
132
147
133
148
AppOpenAdEventListener appOpenAdEventListener = new AppOpenAdEventListener () {
134
149
@ Override
135
150
public void onAdShown () {
136
- mainView . dismissProgressDialog ();
151
+ activity . runOnUiThread (() -> dismissProgressDialog () );
137
152
}
138
153
139
154
@ Override
140
155
public void onAdFailedToShow (@ NonNull final AdError adError ) {
141
- mainView . dismissProgressDialog ();
156
+ activity . runOnUiThread (() -> dismissProgressDialog () );
142
157
}
143
158
144
159
@ Override
145
160
public void onAdDismissed () {
146
161
clearAppOpenAd ();
162
+ activity .runOnUiThread (() -> dismissProgressDialog ());
147
163
}
148
164
149
165
@ Override
150
166
public void onAdClicked () {
151
- // Called when a click is recorded for an ad.
167
+ activity . runOnUiThread (() -> dismissProgressDialog ());
152
168
}
153
169
154
170
@ Override
155
171
public void onAdImpression (@ Nullable final ImpressionData impressionData ) {
156
- // Called when an impression is recorded for an ad.
172
+ activity . runOnUiThread (() -> dismissProgressDialog ());
157
173
}
158
174
};
159
175
AppOpenAdLoadListener appOpenAdLoadListener = new AppOpenAdLoadListener () {
160
176
@ Override
161
177
public void onAdLoaded (@ NonNull final AppOpenAd appOpenAd ) {
178
+ dismissProgressDialog ();
162
179
mainAppOpenAd = appOpenAd ;
163
- appOpenAd .setAdEventListener (appOpenAdEventListener );
164
- mainView .dismissProgressDialog ();
165
- mainAppOpenAd .show (activity );
180
+ activity .runOnUiThread (() -> {
181
+ mainAppOpenAd .setAdEventListener (appOpenAdEventListener );
182
+ mainAppOpenAd .show (activity );
183
+ });
166
184
}
167
185
168
186
@ Override
169
187
public void onAdFailedToLoad (@ NonNull final AdRequestError adRequestError ) {
170
- mainView . dismissProgressDialog ();
188
+ activity . runOnUiThread (() -> dismissProgressDialog () );
171
189
}
172
190
};
173
191
174
192
appOpenAdLoader .setAdLoadListener (appOpenAdLoadListener );
175
193
appOpenAdLoader .loadAd (adRequestConfiguration );
176
- mainView .dismissProgressDialog ();
194
+
195
+ new Thread (() -> {
196
+ try {
197
+ Thread .sleep (2000 );
198
+ } catch (InterruptedException e ) {
199
+ throw new RuntimeException (e );
200
+ }
201
+ activity .runOnUiThread (this ::dismissProgressDialog );
202
+ });
177
203
}
178
204
179
205
@ NonNull
0 commit comments