|
| 1 | +package ru.plumsoftware.notebook.presentation.activities.main.presenter; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.app.Activity; |
| 5 | +import android.content.Context; |
| 6 | +import android.database.Cursor; |
| 7 | +import android.database.sqlite.SQLiteDatabase; |
| 8 | + |
| 9 | +import androidx.annotation.NonNull; |
| 10 | +import androidx.annotation.Nullable; |
| 11 | + |
| 12 | +import com.yandex.mobile.ads.appopenad.AppOpenAd; |
| 13 | +import com.yandex.mobile.ads.appopenad.AppOpenAdEventListener; |
| 14 | +import com.yandex.mobile.ads.appopenad.AppOpenAdLoadListener; |
| 15 | +import com.yandex.mobile.ads.appopenad.AppOpenAdLoader; |
| 16 | +import com.yandex.mobile.ads.common.AdError; |
| 17 | +import com.yandex.mobile.ads.common.AdRequestConfiguration; |
| 18 | +import com.yandex.mobile.ads.common.AdRequestError; |
| 19 | +import com.yandex.mobile.ads.common.ImpressionData; |
| 20 | +import com.yandex.mobile.ads.common.MobileAds; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import ru.plumsoftware.data.database.SQLiteDatabaseManager; |
| 26 | +import ru.plumsoftware.data.model.database.DatabaseConstants; |
| 27 | +import ru.plumsoftware.data.model.ui.Note; |
| 28 | +import ru.plumsoftware.notebook.manager.ads.AdsIds; |
| 29 | +import ru.plumsoftware.notebook.presentation.activities.main.view.MainView; |
| 30 | +import ru.plumsoftware.notebook.presentation.presenters.main.MainPresenter; |
| 31 | + |
| 32 | +public class MainPresenterImpl implements MainPresenter { |
| 33 | + |
| 34 | + private MainView view; |
| 35 | + private final Context context; |
| 36 | + private final Activity activity; |
| 37 | + private SQLiteDatabase sqLiteDatabaseNotes; |
| 38 | + private AppOpenAd mainAppOpenAd = null; |
| 39 | + |
| 40 | + public MainPresenterImpl(MainView view, Context context, Activity activity) { |
| 41 | + this.view = view; |
| 42 | + this.context = context; |
| 43 | + this.activity = activity; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void initMobileSdk() { |
| 48 | + MobileAds.initialize(context, () -> { |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + @Override |
| 54 | + public void initNotes() { |
| 55 | + SQLiteDatabaseManager sqLiteDatabaseManager = new SQLiteDatabaseManager(context); |
| 56 | + sqLiteDatabaseNotes = sqLiteDatabaseManager.getWritableDatabase(); |
| 57 | + |
| 58 | + List<Note> notes = loadNotes(); |
| 59 | + view.showNotes(notes); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void initOpenAds() { |
| 64 | + final AppOpenAdLoader appOpenAdLoader = new AppOpenAdLoader(context); |
| 65 | + final AdRequestConfiguration adRequestConfiguration = new AdRequestConfiguration.Builder(AdsIds.OPEN_AD_UNIT_ID).build(); |
| 66 | + view.showLoading(); |
| 67 | + AppOpenAdEventListener appOpenAdEventListener = new AppOpenAdEventListener() { |
| 68 | + @Override |
| 69 | + public void onAdShown() { |
| 70 | + view.hideLoading(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void onAdFailedToShow(@NonNull final AdError adError) { |
| 75 | + view.hideLoading(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onAdDismissed() { |
| 80 | + clearAppOpenAd(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void onAdClicked() { |
| 85 | + // Called when a click is recorded for an ad. |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void onAdImpression(@Nullable final ImpressionData impressionData) { |
| 90 | + // Called when an impression is recorded for an ad. |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + AppOpenAdLoadListener appOpenAdLoadListener = new AppOpenAdLoadListener() { |
| 95 | + @Override |
| 96 | + public void onAdLoaded(@NonNull final AppOpenAd appOpenAd) { |
| 97 | + mainAppOpenAd = appOpenAd; |
| 98 | + appOpenAd.setAdEventListener(appOpenAdEventListener); |
| 99 | + mainAppOpenAd.show(activity); |
| 100 | + view.hideLoading(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onAdFailedToLoad(@NonNull final AdRequestError adRequestError) { |
| 105 | + view.hideLoading(); |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + appOpenAdLoader.setAdLoadListener(appOpenAdLoadListener); |
| 110 | + appOpenAdLoader.loadAd(adRequestConfiguration); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void loadData() { |
| 115 | + view.showLoading(); |
| 116 | + view.hideLoading(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void detachView() { |
| 121 | + view = null; |
| 122 | + } |
| 123 | + |
| 124 | + @NonNull |
| 125 | + private List<Note> loadNotes() { |
| 126 | + List<Note> notes = new ArrayList<>(); |
| 127 | + List<Note> pinnedNotes = loadPinnedNotes(); |
| 128 | + List<Note> simpleNotes = loadSimpleNotes(); |
| 129 | + notes.addAll(pinnedNotes); |
| 130 | + notes.addAll(simpleNotes); |
| 131 | + return notes; |
| 132 | + } |
| 133 | + |
| 134 | + @NonNull |
| 135 | + private List<Note> loadPinnedNotes() { |
| 136 | + List<Note> notes = new ArrayList<>(); |
| 137 | + |
| 138 | + @SuppressLint("Recycle") Cursor cursor = sqLiteDatabaseNotes.query( |
| 139 | + DatabaseConstants._NOTES_TABLE_NAME, // The table to query |
| 140 | + null, // The array of columns to return (pass null to get all) |
| 141 | + DatabaseConstants._IS_PINNED + " = ?", // The columns for the WHERE clause |
| 142 | + new String[]{"1"}, // The values for the WHERE clause |
| 143 | + null, // don't group the rows |
| 144 | + null, // don't filter by row groups |
| 145 | + //"DATE_FORMAT("+new SimpleDateFormat("")+", '%m%d')" // The sort order |
| 146 | + DatabaseConstants._ADD_NOTE_TIME + " DESC" |
| 147 | + ); |
| 148 | + while (cursor.moveToNext()) { |
| 149 | + int id = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._ID)); |
| 150 | + int notePromoResId = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_PROMO)); |
| 151 | + int isPinned = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._IS_PINNED)); |
| 152 | + int isLiked = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._IS_LIKED)); |
| 153 | + int colorRes = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_COLOR)); |
| 154 | + String noteName = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_NAME)); |
| 155 | + String noteText = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_TEXT)); |
| 156 | + long addTime = cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseConstants._ADD_NOTE_TIME)); |
| 157 | + String notificationChannelId = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstants._CHANNEL_ID)); |
| 158 | + int isNotify = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._IS_NOTIFY)); |
| 159 | + |
| 160 | + Note note = new Note( |
| 161 | + id, |
| 162 | + 0, |
| 163 | + notePromoResId, |
| 164 | + isPinned, |
| 165 | + isLiked, |
| 166 | + colorRes, |
| 167 | + noteName, |
| 168 | + noteText, |
| 169 | + addTime, |
| 170 | + 0, |
| 171 | + notificationChannelId, |
| 172 | + isNotify |
| 173 | + |
| 174 | + ); |
| 175 | + notes.add(note); |
| 176 | + } |
| 177 | + cursor.close(); |
| 178 | + return notes; |
| 179 | + } |
| 180 | + |
| 181 | + @NonNull |
| 182 | + private List<Note> loadSimpleNotes() { |
| 183 | + List<Note> notes = new ArrayList<>(); |
| 184 | + |
| 185 | + @SuppressLint("Recycle") Cursor cursor = sqLiteDatabaseNotes.query( |
| 186 | + DatabaseConstants._NOTES_TABLE_NAME, // The table to query |
| 187 | + null, // The array of columns to return (pass null to get all) |
| 188 | + DatabaseConstants._IS_PINNED + " = ?", // The columns for the WHERE clause |
| 189 | + new String[]{"0"}, // The values for the WHERE clause |
| 190 | + null, // don't group the rows |
| 191 | + null, // don't filter by row groups |
| 192 | + DatabaseConstants._ADD_NOTE_TIME + " DESC" // The sort order |
| 193 | + ); |
| 194 | + |
| 195 | + while (cursor.moveToNext()) { |
| 196 | + int id = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._ID)); |
| 197 | + int notePromoResId = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_PROMO)); |
| 198 | + int isPinned = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._IS_PINNED)); |
| 199 | + int isLiked = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._IS_LIKED)); |
| 200 | + int colorRes = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_COLOR)); |
| 201 | + String noteName = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_NAME)); |
| 202 | + String noteText = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_TEXT)); |
| 203 | + long addTime = cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseConstants._ADD_NOTE_TIME)); |
| 204 | + String notificationChannelId = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstants._CHANNEL_ID)); |
| 205 | + int isNotify = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._IS_NOTIFY)); |
| 206 | + |
| 207 | + Note note = new Note( |
| 208 | + id, |
| 209 | + 0, |
| 210 | + notePromoResId, |
| 211 | + isPinned, |
| 212 | + isLiked, |
| 213 | + colorRes, |
| 214 | + noteName, |
| 215 | + noteText, |
| 216 | + addTime, |
| 217 | + 0, |
| 218 | + notificationChannelId, |
| 219 | + isNotify |
| 220 | + ); |
| 221 | + notes.add(note); |
| 222 | + } |
| 223 | + cursor.close(); |
| 224 | + return notes; |
| 225 | + } |
| 226 | + |
| 227 | + private void clearAppOpenAd() { |
| 228 | + if (mainAppOpenAd != null) { |
| 229 | + mainAppOpenAd.setAdEventListener(null); |
| 230 | + mainAppOpenAd = null; |
| 231 | + } |
| 232 | + } |
| 233 | +} |
0 commit comments