diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index afee53e..190808c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -31,7 +31,7 @@ android:value="" /> diff --git a/app/src/main/java/ru/plumsoftware/notebook/manager/ads/AdsIds.java b/app/src/main/java/ru/plumsoftware/notebook/manager/ads/AdsIds.java new file mode 100644 index 0000000..6674caa --- /dev/null +++ b/app/src/main/java/ru/plumsoftware/notebook/manager/ads/AdsIds.java @@ -0,0 +1,5 @@ +package ru.plumsoftware.notebook.manager.ads; + +public class AdsIds { + public static final String OPEN_AD_UNIT_ID = "R-M-1957919-3"; +} diff --git a/app/src/main/java/ru/plumsoftware/notebook/manager/extra/ExtraNames.java b/app/src/main/java/ru/plumsoftware/notebook/manager/extra/ExtraNames.java index 7aea6a1..9306236 100644 --- a/app/src/main/java/ru/plumsoftware/notebook/manager/extra/ExtraNames.java +++ b/app/src/main/java/ru/plumsoftware/notebook/manager/extra/ExtraNames.java @@ -8,4 +8,8 @@ public static class LocalNotificationExtra { public static String notificationChannelId = "notificationChannelId"; public static Pair colorPair = new Pair<>("color", 0); } + + public static class MainActivity { + public static String isLoadAppOpenAd = "isLoadAppOpenAd"; + } } diff --git a/app/src/main/java/ru/plumsoftware/notebook/manager/notification/NotificationManager.java b/app/src/main/java/ru/plumsoftware/notebook/manager/notification/NotificationManager.java index c112ba2..cb22c58 100644 --- a/app/src/main/java/ru/plumsoftware/notebook/manager/notification/NotificationManager.java +++ b/app/src/main/java/ru/plumsoftware/notebook/manager/notification/NotificationManager.java @@ -13,7 +13,7 @@ import java.util.Calendar; import ru.plumsoftware.notebook.R; -import ru.plumsoftware.notebook.presentation.activities.MainActivity; +import ru.plumsoftware.notebook.presentation.activities.main.view.MainActivity; public class NotificationManager { diff --git a/app/src/main/java/ru/plumsoftware/notebook/presentation/activities/AddNoteActivity.java b/app/src/main/java/ru/plumsoftware/notebook/presentation/activities/AddNoteActivity.java index 3ef3691..855afcc 100644 --- a/app/src/main/java/ru/plumsoftware/notebook/presentation/activities/AddNoteActivity.java +++ b/app/src/main/java/ru/plumsoftware/notebook/presentation/activities/AddNoteActivity.java @@ -52,6 +52,7 @@ import ru.plumsoftware.data.model.ui.Colors; import ru.plumsoftware.data.model.ui.Note; import ru.plumsoftware.data.model.ui.Shape; +import ru.plumsoftware.notebook.presentation.activities.main.view.MainActivity; import ru.plumsoftware.notebook.presentation.dialogs.ProgressDialog; import ru.plumsoftware.notebook.R; import ru.plumsoftware.notebook.presentation.adapters.ColorAdapter; diff --git a/app/src/main/java/ru/plumsoftware/notebook/presentation/activities/main/presenter/MainPresenterImpl.java b/app/src/main/java/ru/plumsoftware/notebook/presentation/activities/main/presenter/MainPresenterImpl.java new file mode 100644 index 0000000..768130c --- /dev/null +++ b/app/src/main/java/ru/plumsoftware/notebook/presentation/activities/main/presenter/MainPresenterImpl.java @@ -0,0 +1,233 @@ +package ru.plumsoftware.notebook.presentation.activities.main.presenter; + +import android.annotation.SuppressLint; +import android.app.Activity; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.yandex.mobile.ads.appopenad.AppOpenAd; +import com.yandex.mobile.ads.appopenad.AppOpenAdEventListener; +import com.yandex.mobile.ads.appopenad.AppOpenAdLoadListener; +import com.yandex.mobile.ads.appopenad.AppOpenAdLoader; +import com.yandex.mobile.ads.common.AdError; +import com.yandex.mobile.ads.common.AdRequestConfiguration; +import com.yandex.mobile.ads.common.AdRequestError; +import com.yandex.mobile.ads.common.ImpressionData; +import com.yandex.mobile.ads.common.MobileAds; + +import java.util.ArrayList; +import java.util.List; + +import ru.plumsoftware.data.database.SQLiteDatabaseManager; +import ru.plumsoftware.data.model.database.DatabaseConstants; +import ru.plumsoftware.data.model.ui.Note; +import ru.plumsoftware.notebook.manager.ads.AdsIds; +import ru.plumsoftware.notebook.presentation.activities.main.view.MainView; +import ru.plumsoftware.notebook.presentation.presenters.main.MainPresenter; + +public class MainPresenterImpl implements MainPresenter { + + private MainView view; + private final Context context; + private final Activity activity; + private SQLiteDatabase sqLiteDatabaseNotes; + private AppOpenAd mainAppOpenAd = null; + + public MainPresenterImpl(MainView view, Context context, Activity activity) { + this.view = view; + this.context = context; + this.activity = activity; + } + + @Override + public void initMobileSdk() { + MobileAds.initialize(context, () -> { + }); + } + + + @Override + public void initNotes() { + SQLiteDatabaseManager sqLiteDatabaseManager = new SQLiteDatabaseManager(context); + sqLiteDatabaseNotes = sqLiteDatabaseManager.getWritableDatabase(); + + List notes = loadNotes(); + view.showNotes(notes); + } + + @Override + public void initOpenAds() { + final AppOpenAdLoader appOpenAdLoader = new AppOpenAdLoader(context); + final AdRequestConfiguration adRequestConfiguration = new AdRequestConfiguration.Builder(AdsIds.OPEN_AD_UNIT_ID).build(); + view.showLoading(); + AppOpenAdEventListener appOpenAdEventListener = new AppOpenAdEventListener() { + @Override + public void onAdShown() { + view.hideLoading(); + } + + @Override + public void onAdFailedToShow(@NonNull final AdError adError) { + view.hideLoading(); + } + + @Override + public void onAdDismissed() { + clearAppOpenAd(); + } + + @Override + public void onAdClicked() { + // Called when a click is recorded for an ad. + } + + @Override + public void onAdImpression(@Nullable final ImpressionData impressionData) { + // Called when an impression is recorded for an ad. + } + }; + + AppOpenAdLoadListener appOpenAdLoadListener = new AppOpenAdLoadListener() { + @Override + public void onAdLoaded(@NonNull final AppOpenAd appOpenAd) { + mainAppOpenAd = appOpenAd; + appOpenAd.setAdEventListener(appOpenAdEventListener); + mainAppOpenAd.show(activity); + view.hideLoading(); + } + + @Override + public void onAdFailedToLoad(@NonNull final AdRequestError adRequestError) { + view.hideLoading(); + } + }; + + appOpenAdLoader.setAdLoadListener(appOpenAdLoadListener); + appOpenAdLoader.loadAd(adRequestConfiguration); + } + + @Override + public void loadData() { + view.showLoading(); + view.hideLoading(); + } + + @Override + public void detachView() { + view = null; + } + + @NonNull + private List loadNotes() { + List notes = new ArrayList<>(); + List pinnedNotes = loadPinnedNotes(); + List simpleNotes = loadSimpleNotes(); + notes.addAll(pinnedNotes); + notes.addAll(simpleNotes); + return notes; + } + + @NonNull + private List loadPinnedNotes() { + List notes = new ArrayList<>(); + + @SuppressLint("Recycle") Cursor cursor = sqLiteDatabaseNotes.query( + DatabaseConstants._NOTES_TABLE_NAME, // The table to query + null, // The array of columns to return (pass null to get all) + DatabaseConstants._IS_PINNED + " = ?", // The columns for the WHERE clause + new String[]{"1"}, // The values for the WHERE clause + null, // don't group the rows + null, // don't filter by row groups + //"DATE_FORMAT("+new SimpleDateFormat("")+", '%m%d')" // The sort order + DatabaseConstants._ADD_NOTE_TIME + " DESC" + ); + while (cursor.moveToNext()) { + int id = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._ID)); + int notePromoResId = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_PROMO)); + int isPinned = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._IS_PINNED)); + int isLiked = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._IS_LIKED)); + int colorRes = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_COLOR)); + String noteName = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_NAME)); + String noteText = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_TEXT)); + long addTime = cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseConstants._ADD_NOTE_TIME)); + String notificationChannelId = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstants._CHANNEL_ID)); + int isNotify = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._IS_NOTIFY)); + + Note note = new Note( + id, + 0, + notePromoResId, + isPinned, + isLiked, + colorRes, + noteName, + noteText, + addTime, + 0, + notificationChannelId, + isNotify + + ); + notes.add(note); + } + cursor.close(); + return notes; + } + + @NonNull + private List loadSimpleNotes() { + List notes = new ArrayList<>(); + + @SuppressLint("Recycle") Cursor cursor = sqLiteDatabaseNotes.query( + DatabaseConstants._NOTES_TABLE_NAME, // The table to query + null, // The array of columns to return (pass null to get all) + DatabaseConstants._IS_PINNED + " = ?", // The columns for the WHERE clause + new String[]{"0"}, // The values for the WHERE clause + null, // don't group the rows + null, // don't filter by row groups + DatabaseConstants._ADD_NOTE_TIME + " DESC" // The sort order + ); + + while (cursor.moveToNext()) { + int id = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._ID)); + int notePromoResId = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_PROMO)); + int isPinned = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._IS_PINNED)); + int isLiked = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._IS_LIKED)); + int colorRes = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_COLOR)); + String noteName = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_NAME)); + String noteText = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstants._NOTE_TEXT)); + long addTime = cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseConstants._ADD_NOTE_TIME)); + String notificationChannelId = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstants._CHANNEL_ID)); + int isNotify = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstants._IS_NOTIFY)); + + Note note = new Note( + id, + 0, + notePromoResId, + isPinned, + isLiked, + colorRes, + noteName, + noteText, + addTime, + 0, + notificationChannelId, + isNotify + ); + notes.add(note); + } + cursor.close(); + return notes; + } + + private void clearAppOpenAd() { + if (mainAppOpenAd != null) { + mainAppOpenAd.setAdEventListener(null); + mainAppOpenAd = null; + } + } +} diff --git a/app/src/main/java/ru/plumsoftware/notebook/presentation/activities/MainActivity.java b/app/src/main/java/ru/plumsoftware/notebook/presentation/activities/main/view/MainActivity.java similarity index 80% rename from app/src/main/java/ru/plumsoftware/notebook/presentation/activities/MainActivity.java rename to app/src/main/java/ru/plumsoftware/notebook/presentation/activities/main/view/MainActivity.java index d5a015a..1eab0cd 100644 --- a/app/src/main/java/ru/plumsoftware/notebook/presentation/activities/MainActivity.java +++ b/app/src/main/java/ru/plumsoftware/notebook/presentation/activities/main/view/MainActivity.java @@ -1,4 +1,4 @@ -package ru.plumsoftware.notebook.presentation.activities; +package ru.plumsoftware.notebook.presentation.activities.main.view; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -16,7 +16,6 @@ import android.database.sqlite.SQLiteDatabase; import android.graphics.Color; import android.os.Bundle; -import android.os.Handler; import android.util.Log; import android.view.View; import android.widget.ImageButton; @@ -42,20 +41,28 @@ import ru.plumsoftware.data.model.database.DatabaseConstants; import ru.plumsoftware.data.model.ui.Note; import ru.plumsoftware.notebook.R; +import ru.plumsoftware.notebook.presentation.activities.AddNoteActivity; +import ru.plumsoftware.notebook.presentation.activities.main.presenter.MainPresenterImpl; import ru.plumsoftware.notebook.presentation.adapters.NoteAdapter; import ru.plumsoftware.notebook.presentation.dialogs.ProgressDialog; +import ru.plumsoftware.notebook.presentation.presenters.main.MainPresenter; + +public class MainActivity extends AppCompatActivity implements MainView { + + private MainPresenter presenter; + + private Context context; + private Activity activity; + + private static RecyclerView recyclerViewNotes; + private ProgressDialog progressDialog; -public class MainActivity extends AppCompatActivity { public static SQLiteDatabase sqLiteDatabaseNotes; - private static RecyclerView recyclerViewNotes; //recyclerViewGroups; private static boolean isList = true; private int color, opacityRes = R.drawable.ic_coffee; - private Context context; - private Activity activity; - private ProgressDialog progressDialog; private AppOpenAd mAppOpenAd = null; @Override @@ -63,97 +70,28 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notepad); - MobileAds.initialize(this, () -> { - - }); - -// FVI - SearchView searchView = (SearchView) findViewById(R.id.searchView); - recyclerViewNotes = (RecyclerView) findViewById(R.id.recyclerViewNotes); - //recyclerViewGroups = (RecyclerView) findViewById(R.id.recyclerViewGroups); - ImageView filterAsList = (ImageView) findViewById(R.id.filterAsList); - ImageButton addNote = (ImageButton) findViewById(R.id.addNote); - -// Data +// Base variables context = MainActivity.this; activity = MainActivity.this; - Handler handler = new Handler(); - progressDialog = new ProgressDialog(MainActivity.this, R.style.CustomProgressDialog); - try (SQLiteDatabaseManager sqLiteDatabaseManager = new SQLiteDatabaseManager(context)) { - sqLiteDatabaseNotes = sqLiteDatabaseManager.getWritableDatabase(); - } - List notes = new ArrayList<>(); - - notes = loadNotes(context); - -// load ad - if (getIntent().getBooleanExtra("isLoadAppOpenAd", true)) { - final AppOpenAdLoader appOpenAdLoader = new AppOpenAdLoader(this); - final String AD_UNIT_ID = "R-M-1957919-3"; - final AdRequestConfiguration adRequestConfiguration = new AdRequestConfiguration.Builder(AD_UNIT_ID).build(); - - progressDialog.showDialog(); - AppOpenAdEventListener appOpenAdEventListener = new AppOpenAdEventListener() { - @Override - public void onAdShown() { - // Called when ad is shown. - } + presenter = new MainPresenterImpl(this, context, activity); - @Override - public void onAdFailedToShow(@NonNull final AdError adError) { - // Called when ad failed to show. - progressDialog.dismiss(); - } +// Find views by id + SearchView searchView = findViewById(R.id.searchView); + recyclerViewNotes = findViewById(R.id.recyclerViewNotes); + ImageView filterAsList = findViewById(R.id.filterAsList); + ImageButton addNote = findViewById(R.id.addNote); + progressDialog = new ProgressDialog(context, R.style.CustomProgressDialog); - @Override - public void onAdDismissed() { - // Called when ad is dismissed. - // Clean resources after dismiss and preload new ad. - clearAppOpenAd(); -// showAppOpenAd(); - } - @Override - public void onAdClicked() { - // Called when a click is recorded for an ad. - } - - @Override - public void onAdImpression(@Nullable final ImpressionData impressionData) { - // Called when an impression is recorded for an ad. - } - }; - - AppOpenAdLoadListener appOpenAdLoadListener = new AppOpenAdLoadListener() { - @Override - public void onAdLoaded(@NonNull final AppOpenAd appOpenAd) { - // The ad was loaded successfully. Now you can show loaded ad. - mAppOpenAd = appOpenAd; - mAppOpenAd.setAdEventListener(appOpenAdEventListener); - showAppOpenAd(); - progressDialog.dismiss(); - } + presenter.initMobileSdk(); + presenter.initNotes(); - @Override - public void onAdFailedToLoad(@NonNull final AdRequestError adRequestError) { - progressDialog.dismiss(); - } - }; - - appOpenAdLoader.setAdLoadListener(appOpenAdLoadListener); - - appOpenAdLoader.loadAd(adRequestConfiguration); +// load ad + if (getIntent().getBooleanExtra("isLoadAppOpenAd", true)) { + presenter.initOpenAds(); } - - NoteAdapter noteAdapter = new NoteAdapter(context, activity, notes, 1); - //GroupAdapter groupAdapter = new GroupAdapter(context, activity, groups, 1); - - recyclerViewNotes.setHasFixedSize(true); - recyclerViewNotes.setLayoutManager(new LinearLayoutManager(context)); - recyclerViewNotes.setAdapter(noteAdapter); - filterAsList.setImageResource(R.drawable.ic_baseline_filter_list); color = getResources().getColor(R.color.note_blue); @@ -432,22 +370,9 @@ public void saveNote(String name, String text, int or, int c, long time) { .show(); } - public static void reloadRecyclerView(Context context, Activity activity) { - recyclerViewNotes.setVisibility(View.GONE); - List notes = loadNotes(context); - //Collections.reverse(notes); - NoteAdapter noteAdapter = new NoteAdapter(context, activity, notes, 1); - recyclerViewNotes.setHasFixedSize(true); - recyclerViewNotes.setLayoutManager(new LinearLayoutManager(context)); - recyclerViewNotes.setAdapter(noteAdapter); - recyclerViewNotes.setVisibility(View.VISIBLE); - isList = true; - } - @Override protected void onDestroy() { super.onDestroy(); - sqLiteDatabaseNotes.close(); } private void clearAppOpenAd() { @@ -457,10 +382,22 @@ private void clearAppOpenAd() { } } + @Override + public void showNotes(List noteList) { + NoteAdapter noteAdapter = new NoteAdapter(context, activity, noteList, 1); - private void showAppOpenAd() { - if (mAppOpenAd != null) { - mAppOpenAd.show(activity); - } + recyclerViewNotes.setHasFixedSize(true); + recyclerViewNotes.setLayoutManager(new LinearLayoutManager(context)); + recyclerViewNotes.setAdapter(noteAdapter); + } + + @Override + public void showLoading() { + progressDialog.showDialog(); + } + + @Override + public void hideLoading() { + progressDialog.dismiss(); } } \ No newline at end of file diff --git a/app/src/main/java/ru/plumsoftware/notebook/presentation/activities/main/view/MainView.java b/app/src/main/java/ru/plumsoftware/notebook/presentation/activities/main/view/MainView.java new file mode 100644 index 0000000..94b4d69 --- /dev/null +++ b/app/src/main/java/ru/plumsoftware/notebook/presentation/activities/main/view/MainView.java @@ -0,0 +1,13 @@ +package ru.plumsoftware.notebook.presentation.activities.main.view; + +import java.util.List; + +import ru.plumsoftware.data.model.ui.Note; + +public interface MainView { + void showNotes(List noteList); + + void showLoading(); + + void hideLoading(); +} diff --git a/app/src/main/java/ru/plumsoftware/notebook/presentation/adapters/NoteAdapter.java b/app/src/main/java/ru/plumsoftware/notebook/presentation/adapters/NoteAdapter.java index 56bcfcc..e13c2e4 100644 --- a/app/src/main/java/ru/plumsoftware/notebook/presentation/adapters/NoteAdapter.java +++ b/app/src/main/java/ru/plumsoftware/notebook/presentation/adapters/NoteAdapter.java @@ -48,7 +48,7 @@ import ru.plumsoftware.data.model.database.DatabaseConstants; import ru.plumsoftware.data.model.ui.Note; import ru.plumsoftware.notebook.presentation.activities.AddNoteActivity; -import ru.plumsoftware.notebook.presentation.activities.MainActivity; +import ru.plumsoftware.notebook.presentation.activities.main.view.MainActivity; import ru.plumsoftware.notebook.presentation.dialogs.ProgressDialog; import ru.plumsoftware.notebook.R; diff --git a/app/src/main/java/ru/plumsoftware/notebook/presentation/presenters/main/MainPresenter.java b/app/src/main/java/ru/plumsoftware/notebook/presentation/presenters/main/MainPresenter.java new file mode 100644 index 0000000..4ad4707 --- /dev/null +++ b/app/src/main/java/ru/plumsoftware/notebook/presentation/presenters/main/MainPresenter.java @@ -0,0 +1,11 @@ +package ru.plumsoftware.notebook.presentation.presenters.main; + +public interface MainPresenter { + void initMobileSdk(); + void initNotes(); + void initOpenAds(); + + void loadData(); + + void detachView(); +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index fb22174..fdda3de 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,7 +6,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/black" - tools:context=".presentation.activities.MainActivity"> + tools:context=".presentation.activities.main.view.MainActivity">