From 019728c12a0ab04a4d6fc3e64685698ad4e49bc4 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Thu, 11 Oct 2018 23:18:23 +0300 Subject: [PATCH] move shoutbox to drawer --- app/build.gradle | 3 +- app/src/main/AndroidManifest.xml | 8 + .../mthmmy/activities/main/MainActivity.java | 12 +- .../{main => }/shoutbox/SendShoutTask.java | 2 +- .../{main => }/shoutbox/ShoutAdapter.java | 2 +- .../activities/shoutbox/ShoutboxActivity.java | 39 ++++ .../{main => }/shoutbox/ShoutboxFragment.java | 174 +++++++----------- .../{main => }/shoutbox/ShoutboxTask.java | 2 +- .../gr/thmmy/mthmmy/base/BaseActivity.java | 152 ++++++++------- .../mthmmy/viewmodel/ShoutboxViewModel.java | 58 ++++++ .../res/drawable/refresh_label_background.xml | 16 -- app/src/main/res/layout/activity_shoutbox.xml | 32 ++++ app/src/main/res/layout/fragment_shoutbox.xml | 32 +--- 13 files changed, 296 insertions(+), 236 deletions(-) rename app/src/main/java/gr/thmmy/mthmmy/activities/{main => }/shoutbox/SendShoutTask.java (97%) rename app/src/main/java/gr/thmmy/mthmmy/activities/{main => }/shoutbox/ShoutAdapter.java (99%) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxActivity.java rename app/src/main/java/gr/thmmy/mthmmy/activities/{main => }/shoutbox/ShoutboxFragment.java (58%) rename app/src/main/java/gr/thmmy/mthmmy/activities/{main => }/shoutbox/ShoutboxTask.java (98%) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/viewmodel/ShoutboxViewModel.java delete mode 100644 app/src/main/res/drawable/refresh_label_background.xml create mode 100644 app/src/main/res/layout/activity_shoutbox.xml diff --git a/app/build.gradle b/app/build.gradle index f6b7c4fa..0231fc74 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -40,7 +40,7 @@ tasks.whenTaskAdded { task -> task.getDependsOn().add({ def inputFile = new File("app/google-services.json") def json = new JsonSlurper().parseText(inputFile.text) - if(json.project_info.project_id != "mthmmy-release-3aef0") + if (json.project_info.project_id != "mthmmy-release-3aef0") throw new GradleException('Please supply the correct google-services.json for release or manually change the id above!') }) } @@ -77,6 +77,7 @@ dependencies { implementation 'net.gotev:uploadservice-okhttp:3.4.2' implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0' implementation 'com.google.android.material:material:1.0.0' + implementation 'androidx.constraintlayout:constraintlayout:1.1.2' } apply plugin: 'com.google.gms.google-services' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3e37e398..f621a7d2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -154,6 +154,14 @@ android:configChanges="orientation|screenSize" android:parentActivityName=".activities.main.MainActivity" android:theme="@style/AppTheme.NoActionBar" /> + + + \ No newline at end of file diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java index 3ba361df..f74811d7 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java @@ -23,7 +23,6 @@ import gr.thmmy.mthmmy.activities.board.BoardActivity; import gr.thmmy.mthmmy.activities.downloads.DownloadsActivity; import gr.thmmy.mthmmy.activities.main.forum.ForumFragment; import gr.thmmy.mthmmy.activities.main.recent.RecentFragment; -import gr.thmmy.mthmmy.activities.main.shoutbox.ShoutboxFragment; import gr.thmmy.mthmmy.activities.main.unread.UnreadFragment; import gr.thmmy.mthmmy.activities.profile.ProfileActivity; import gr.thmmy.mthmmy.activities.settings.SettingsActivity; @@ -79,10 +78,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); sectionsPagerAdapter.addFragment(RecentFragment.newInstance(1), "RECENT"); sectionsPagerAdapter.addFragment(ForumFragment.newInstance(2), "FORUM"); - if (sessionManager.isLoggedIn()) { + if (sessionManager.isLoggedIn()) sectionsPagerAdapter.addFragment(UnreadFragment.newInstance(3), "UNREAD"); - sectionsPagerAdapter.addFragment(ShoutboxFragment.newInstance(4), "SHOUTBOX"); - } //Set up the ViewPager with the sections adapter. viewPager = findViewById(R.id.container); @@ -102,8 +99,6 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF tabLayout.getTabAt(i).setIcon(getResources().getDrawable(R.drawable.ic_forum_white_24dp)); }else if (i == 2) { tabLayout.getTabAt(i).setIcon(getResources().getDrawable(R.drawable.ic_fiber_new_white_24dp)); - } else if (i == 3) { - tabLayout.getTabAt(i).setIcon(getResources().getDrawable(R.drawable.ic_announcement)); } } @@ -224,15 +219,12 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF } public void updateTabs() { - if (!sessionManager.isLoggedIn() && sectionsPagerAdapter.getCount() == 4) { - sectionsPagerAdapter.removeFragment(3); + if (!sessionManager.isLoggedIn() && sectionsPagerAdapter.getCount() == 3) { sectionsPagerAdapter.removeFragment(2); } else if (sessionManager.isLoggedIn() && sectionsPagerAdapter.getCount() == 2) { sectionsPagerAdapter.addFragment(UnreadFragment.newInstance(3), "UNREAD"); - sectionsPagerAdapter.addFragment(UnreadFragment.newInstance(4), "SHOUTBOX"); tabLayout.getTabAt(3).setIcon(R.drawable.ic_fiber_new_white_24dp); - tabLayout.getTabAt(4).setIcon(R.drawable.ic_announcement); } } //-------------------------------FragmentPagerAdapter END------------------------------------------- diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/SendShoutTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/SendShoutTask.java similarity index 97% rename from app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/SendShoutTask.java rename to app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/SendShoutTask.java index bfe9350e..3b2af22f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/SendShoutTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/SendShoutTask.java @@ -1,4 +1,4 @@ -package gr.thmmy.mthmmy.activities.main.shoutbox; +package gr.thmmy.mthmmy.activities.shoutbox; import org.jsoup.nodes.Document; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutAdapter.java similarity index 99% rename from app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutAdapter.java rename to app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutAdapter.java index 164d4105..7d041a32 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutAdapter.java @@ -1,4 +1,4 @@ -package gr.thmmy.mthmmy.activities.main.shoutbox; +package gr.thmmy.mthmmy.activities.shoutbox; import android.annotation.TargetApi; import android.content.Context; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxActivity.java new file mode 100644 index 00000000..fffc8079 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxActivity.java @@ -0,0 +1,39 @@ +package gr.thmmy.mthmmy.activities.shoutbox; + +import android.os.Bundle; + +import gr.thmmy.mthmmy.R; +import gr.thmmy.mthmmy.base.BaseActivity; + +public class ShoutboxActivity extends BaseActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_shoutbox); + + //Initialize toolbar + toolbar = findViewById(R.id.toolbar); + toolbar.setTitle("Shoutbox"); + setSupportActionBar(toolbar); + if (getSupportActionBar() != null) { + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setDisplayShowHomeEnabled(true); + } + + createDrawer(); + drawer.setSelection(SHOUTBOX_ID); + + if (savedInstanceState == null) { + getSupportFragmentManager().beginTransaction() + .replace(R.id.container, ShoutboxFragment.newInstance()) + .commitNow(); + } + } + + @Override + protected void onResume() { + drawer.setSelection(SHOUTBOX_ID); + super.onResume(); + } +} diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java similarity index 58% rename from app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxFragment.java rename to app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java index ceff90e5..85c20615 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java @@ -1,6 +1,5 @@ -package gr.thmmy.mthmmy.activities.main.shoutbox; +package gr.thmmy.mthmmy.activities.shoutbox; -import android.animation.ValueAnimator; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; @@ -8,16 +7,14 @@ import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputMethodManager; -import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.interpolator.view.animation.FastOutSlowInInterpolator; +import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProviders; import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; import gr.thmmy.mthmmy.R; -import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.editorview.EditorView; import gr.thmmy.mthmmy.editorview.EmojiKeyboard; import gr.thmmy.mthmmy.model.Shout; @@ -25,85 +22,30 @@ import gr.thmmy.mthmmy.model.Shoutbox; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.CustomRecyclerView; import gr.thmmy.mthmmy.utils.NetworkResultCodes; +import gr.thmmy.mthmmy.viewmodel.ShoutboxViewModel; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import timber.log.Timber; -public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.EmojiKeyboardOwner { - - private static final String TAG = "ShoutboxFragment"; +public class ShoutboxFragment extends Fragment implements EmojiKeyboard.EmojiKeyboardOwner { private MaterialProgressBar progressBar; - private TextView refreshLabel; private ShoutboxTask shoutboxTask; private ShoutAdapter shoutAdapter; private EmojiKeyboard emojiKeyboard; private EditorView editorView; - private Shoutbox shoutbox; - private ValueAnimator animator; - - public static ShoutboxFragment newInstance(int sectionNumber) { - ShoutboxFragment fragment = new ShoutboxFragment(); - Bundle args = new Bundle(); - args.putString(ARG_TAG, TAG); - args.putInt(ARG_SECTION_NUMBER, sectionNumber); - fragment.setArguments(args); - return fragment; - } - - private void onShoutboxTaskSarted() { - Timber.i("Starting shoutbox task..."); - hideRefreshLabel(); - progressBar.setVisibility(View.VISIBLE); - } - - private void onSendShoutTaskStarted() { - Timber.i("Start sending a shout..."); - progressBar.setVisibility(View.VISIBLE); - } - private void onSendShoutTaskFinished(int resultCode, Void ignored) { - editorView.setAlpha(1f); - editorView.setEnabled(true); - progressBar.setVisibility(View.INVISIBLE); - showRefreshLabel(); - if (resultCode == NetworkResultCodes.SUCCESSFUL) { - Timber.i("Shout was sent successfully"); - editorView.getEditText().getText().clear(); - shoutboxTask = new ShoutboxTask(ShoutboxFragment.this::onShoutboxTaskSarted, ShoutboxFragment.this::onShoutboxTaskFinished); - shoutboxTask.execute(SessionManager.shoutboxUrl.toString()); - } else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { - Timber.w("Failed to send shout"); - Toast.makeText(getContext(), "NetworkError", Toast.LENGTH_SHORT).show(); - } - } + private ShoutboxViewModel mViewModel; - private void onShoutboxTaskFinished(int resultCode, Shoutbox shoutbox) { - progressBar.setVisibility(View.INVISIBLE); - if (resultCode == NetworkResultCodes.SUCCESSFUL) { - Timber.i("Shoutbox loaded successfully"); - shoutAdapter.setShouts(shoutbox.getShouts()); - shoutAdapter.notifyDataSetChanged(); - this.shoutbox = shoutbox; - } else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { - Timber.w("Failed to retreive shoutbox due to network error"); - Toast.makeText(getContext(), "NetworkError", Toast.LENGTH_SHORT).show(); - } else { - Timber.wtf("Failed to retreive shoutbox due to unknown error"); - Toast.makeText(getContext(), "Failed to retrieve shoutbox, please contact mthmmy developer team", Toast.LENGTH_LONG).show(); - } + public static ShoutboxFragment newInstance() { + return new ShoutboxFragment(); } @Nullable @Override - public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, + @Nullable Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.fragment_shoutbox, container, false); - refreshLabel = rootView.findViewById(R.id.refresh_label); - refreshLabel.setOnClickListener(v -> { - shoutboxTask = new ShoutboxTask(this::onShoutboxTaskSarted, this::onShoutboxTaskFinished); - shoutboxTask.execute(SessionManager.shoutboxUrl.toString()); - }); - progressBar = rootView.findViewById(R.id.progressBar); CustomRecyclerView recyclerView = rootView.findViewById(R.id.shoutbox_recyclerview); shoutAdapter = new ShoutAdapter(getContext(), new Shout[0]); @@ -117,19 +59,6 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj imm.hideSoftInputFromWindow(editorView.getWindowToken(), 0); return false; }); - recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { - if (dy > 0) { - showRefreshLabel(); - } else { - hideRefreshLabel(); - } - } - }); - - shoutboxTask = new ShoutboxTask(this::onShoutboxTaskSarted, this::onShoutboxTaskFinished); - shoutboxTask.execute(SessionManager.shoutboxUrl.toString()); emojiKeyboard = rootView.findViewById(R.id.emoji_keyboard); editorView = rootView.findViewById(R.id.edior_view); @@ -137,17 +66,12 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj InputConnection ic = editorView.getInputConnection(); setEmojiKeyboardInputConnection(ic); editorView.setOnSubmitListener(view -> { - if (shoutbox == null) return; + if (mViewModel.getShoutboxMutableLiveData().getValue() == null) return; if (editorView.getText().toString().isEmpty()) { editorView.setError("Required"); return; } - editorView.setAlpha(0.5f); - editorView.setEnabled(false); - setEmojiKeyboardVisible(false); - new SendShoutTask(this::onSendShoutTaskStarted, this::onSendShoutTaskFinished) - .execute(shoutbox.getSendShoutUrl(), editorView.getText().toString(), shoutbox.getSc(), - shoutbox.getShoutName(), shoutbox.getShoutSend(), shoutbox.getShoutUrl()); + mViewModel.sendShout(editorView.getText().toString()); }); editorView.hideMarkdown(); editorView.setOnTouchListener((view, motionEvent) -> { @@ -160,30 +84,64 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj return rootView; } - private void hideRefreshLabel() { - if (refreshLabel.getVisibility() == View.GONE) return; - if (animator != null) animator.cancel(); - animator = getRefreshLabelAnimation(); - animator.start(); + @Override + public void onActivityCreated(@Nullable Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + mViewModel = ViewModelProviders.of(this).get(ShoutboxViewModel.class); + mViewModel.getShoutboxMutableLiveData().observe(this, shoutbox -> { + if (shoutbox != null) { + Timber.i("Shoutbox loaded successfully"); + shoutAdapter.setShouts(shoutbox.getShouts()); + shoutAdapter.notifyDataSetChanged(); + } + }); + mViewModel.setOnShoutboxTaskStarted(this::onShoutboxTaskSarted); + mViewModel.setOnShoutboxTaskFinished(this::onShoutboxTaskFinished); + mViewModel.setOnSendShoutTaskStarted(this::onSendShoutTaskStarted); + mViewModel.setOnSendShoutTaskFinished(this::onSendShoutTaskFinished); + + mViewModel.loadShoutbox(); } - private void showRefreshLabel() { - if (refreshLabel.getVisibility() == View.VISIBLE) return; - if (animator != null) animator.cancel(); - animator = getRefreshLabelAnimation(); - animator.reverse(); + private void onShoutboxTaskSarted() { + Timber.i("Starting shoutbox task..."); + progressBar.setVisibility(View.VISIBLE); } - private ValueAnimator getRefreshLabelAnimation() { - ValueAnimator animator = ValueAnimator.ofFloat(-200, 0); - animator.addUpdateListener(valueAnimator -> { - if (((Float) valueAnimator.getAnimatedValue()).intValue() == 1) refreshLabel.setVisibility(View.VISIBLE); - if (((Float) valueAnimator.getAnimatedValue()).intValue() == -199) refreshLabel.setVisibility(View.GONE); - refreshLabel.setTranslationY((float) valueAnimator.getAnimatedValue()); - }); - animator.setInterpolator(new FastOutSlowInInterpolator()); - animator.setDuration(200); - return animator; + private void onSendShoutTaskStarted() { + Timber.i("Start sending a shout..."); + editorView.setAlpha(0.5f); + editorView.setEnabled(false); + setEmojiKeyboardVisible(false); + progressBar.setVisibility(View.VISIBLE); + } + + private void onSendShoutTaskFinished(int resultCode, Void ignored) { + editorView.setAlpha(1f); + editorView.setEnabled(true); + progressBar.setVisibility(View.INVISIBLE); + if (resultCode == NetworkResultCodes.SUCCESSFUL) { + Timber.i("Shout was sent successfully"); + editorView.getEditText().getText().clear(); + shoutboxTask = new ShoutboxTask(ShoutboxFragment.this::onShoutboxTaskSarted, ShoutboxFragment.this::onShoutboxTaskFinished); + shoutboxTask.execute(SessionManager.shoutboxUrl.toString()); + } else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { + Timber.w("Failed to send shout"); + Toast.makeText(getContext(), "NetworkError", Toast.LENGTH_SHORT).show(); + } + } + + private void onShoutboxTaskFinished(int resultCode, Shoutbox shoutbox) { + progressBar.setVisibility(View.INVISIBLE); + if (resultCode == NetworkResultCodes.SUCCESSFUL) { + mViewModel.setShoutbox(shoutbox); + } else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { + Timber.w("Failed to retreive shoutbox due to network error"); + Toast.makeText(getContext(), "NetworkError", Toast.LENGTH_SHORT).show(); + } else { + Timber.wtf("Failed to retreive shoutbox due to unknown error"); + Toast.makeText(getContext(), "Failed to retrieve shoutbox, please contact mthmmy developer team", Toast.LENGTH_LONG).show(); + } } @Override diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxTask.java similarity index 98% rename from app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxTask.java rename to app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxTask.java index 289ec0fb..5368248c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxTask.java @@ -1,4 +1,4 @@ -package gr.thmmy.mthmmy.activities.main.shoutbox; +package gr.thmmy.mthmmy.activities.shoutbox; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index 752a1c52..442c4199 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -49,6 +49,7 @@ import gr.thmmy.mthmmy.activities.downloads.DownloadsActivity; import gr.thmmy.mthmmy.activities.main.MainActivity; import gr.thmmy.mthmmy.activities.profile.ProfileActivity; import gr.thmmy.mthmmy.activities.settings.SettingsActivity; +import gr.thmmy.mthmmy.activities.shoutbox.ShoutboxActivity; import gr.thmmy.mthmmy.activities.upload.UploadActivity; import gr.thmmy.mthmmy.model.Bookmark; import gr.thmmy.mthmmy.model.ThmmyFile; @@ -145,6 +146,7 @@ public abstract class BaseActivity extends AppCompatActivity { protected static final int LOG_ID = 4; protected static final int ABOUT_ID = 5; protected static final int SETTINGS_ID = 6; + protected static final int SHOUTBOX_ID = 7; private AccountHeader accountHeader; private ProfileDrawerItem profileDrawerItem; @@ -159,7 +161,7 @@ public abstract class BaseActivity extends AppCompatActivity { final int selectedPrimaryColor = ContextCompat.getColor(this, R.color.primary_dark); final int selectedSecondaryColor = ContextCompat.getColor(this, R.color.accent); - PrimaryDrawerItem homeItem, bookmarksItem, settingsItem, aboutItem; + PrimaryDrawerItem homeItem, bookmarksItem, settingsItem, aboutItem, shoutboxItem; IconicsDrawable homeIcon, homeIconSelected, downloadsIcon, downloadsIconSelected, uploadIcon, uploadIconSelected, settingsIcon, settingsIconSelected, bookmarksIcon, bookmarksIconSelected, aboutIcon, aboutIconSelected; @@ -230,6 +232,15 @@ public abstract class BaseActivity extends AppCompatActivity { .withIcon(homeIcon) .withSelectedIcon(homeIconSelected); + shoutboxItem = new PrimaryDrawerItem() + .withTextColor(primaryColor) + .withSelectedColor(selectedPrimaryColor) + .withSelectedTextColor(selectedSecondaryColor) + .withIdentifier(SHOUTBOX_ID) + .withName(R.string.shoutbox) + .withIcon(R.drawable.ic_announcement) + .withIconColor(primaryColor) + .withSelectedIconColor(selectedSecondaryColor); if (sessionManager.isLoggedIn()) //When logged in { @@ -301,26 +312,23 @@ public abstract class BaseActivity extends AppCompatActivity { .withSelectionListEnabledForSingleProfile(false) .withHeaderBackground(R.color.primary) .addProfiles(profileDrawerItem) - .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() { - @Override - public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) { - if (sessionManager.isLoggedIn()) { - Intent intent = new Intent(BaseActivity.this, ProfileActivity.class); - Bundle extras = new Bundle(); - extras.putString(BUNDLE_PROFILE_URL, "https://www.thmmy.gr/smf/index.php?action=profile"); - if (!sessionManager.hasAvatar()) - extras.putString(BUNDLE_PROFILE_THUMBNAIL_URL, ""); - else - extras.putString(BUNDLE_PROFILE_THUMBNAIL_URL, sessionManager.getAvatarLink()); - extras.putString(BUNDLE_PROFILE_USERNAME, sessionManager.getUsername()); - intent.putExtras(extras); - intent.setFlags(FLAG_ACTIVITY_NEW_TASK); - startActivity(intent); - return false; - } - return true; - + .withOnAccountHeaderListener((view, profile, currentProfile) -> { + if (sessionManager.isLoggedIn()) { + Intent intent = new Intent(BaseActivity.this, ProfileActivity.class); + Bundle extras = new Bundle(); + extras.putString(BUNDLE_PROFILE_URL, "https://www.thmmy.gr/smf/index.php?action=profile"); + if (!sessionManager.hasAvatar()) + extras.putString(BUNDLE_PROFILE_THUMBNAIL_URL, ""); + else + extras.putString(BUNDLE_PROFILE_THUMBNAIL_URL, sessionManager.getAvatarLink()); + extras.putString(BUNDLE_PROFILE_USERNAME, sessionManager.getUsername()); + intent.putExtras(extras); + intent.setFlags(FLAG_ACTIVITY_NEW_TASK); + startActivity(intent); + return false; } + return true; + }) .build(); @@ -331,63 +339,65 @@ public abstract class BaseActivity extends AppCompatActivity { .withDrawerWidthDp((int) BaseApplication.getInstance().getDpWidth() / 2) .withSliderBackgroundColor(ContextCompat.getColor(this, R.color.primary_light)) .withAccountHeader(accountHeader) - .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { - @Override - public boolean onItemClick(View view, int position, IDrawerItem drawerItem) { - if (drawerItem.equals(HOME_ID)) { - if (!(BaseActivity.this instanceof MainActivity)) { - Intent intent = new Intent(BaseActivity.this, MainActivity.class); - startActivity(intent); - } - } else if (drawerItem.equals(DOWNLOADS_ID)) { - if (!(BaseActivity.this instanceof DownloadsActivity)) { - Intent intent = new Intent(BaseActivity.this, DownloadsActivity.class); - Bundle extras = new Bundle(); - extras.putString(BUNDLE_DOWNLOADS_URL, ""); - extras.putString(BUNDLE_DOWNLOADS_TITLE, null); - intent.putExtras(extras); - startActivity(intent); - } - } else if (drawerItem.equals(UPLOAD_ID)) { - if (!(BaseActivity.this instanceof UploadActivity)) { - Intent intent = new Intent(BaseActivity.this, UploadActivity.class); - startActivity(intent); - } - } else if (drawerItem.equals(BOOKMARKS_ID)) { - if (!(BaseActivity.this instanceof BookmarkActivity)) { - Intent intent = new Intent(BaseActivity.this, BookmarkActivity.class); - startActivity(intent); - } - } else if (drawerItem.equals(LOG_ID)) { - if (!sessionManager.isLoggedIn()) //When logged out or if user is guest - { - Intent intent = new Intent(BaseActivity.this, LoginActivity.class); - startActivity(intent); - finish(); - overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out); - } else - new LogoutTask().execute(); - } else if (drawerItem.equals(ABOUT_ID)) { - if (!(BaseActivity.this instanceof AboutActivity)) { - Intent intent = new Intent(BaseActivity.this, AboutActivity.class); - startActivity(intent); - } - } else if (drawerItem.equals(SETTINGS_ID)) { - if (!(BaseActivity.this instanceof SettingsActivity)) { - Intent intent = new Intent(BaseActivity.this, SettingsActivity.class); - startActivity(intent); - } + .withOnDrawerItemClickListener((view, position, drawerItem) -> { + if (drawerItem.equals(HOME_ID)) { + if (!(BaseActivity.this instanceof MainActivity)) { + Intent intent = new Intent(BaseActivity.this, MainActivity.class); + startActivity(intent); + } + } else if (drawerItem.equals(SHOUTBOX_ID)) { + if (!(BaseActivity.this instanceof ShoutboxActivity)) { + Intent intent = new Intent(BaseActivity.this, ShoutboxActivity.class); + startActivity(intent); + } + } else if (drawerItem.equals(DOWNLOADS_ID)) { + if (!(BaseActivity.this instanceof DownloadsActivity)) { + Intent intent = new Intent(BaseActivity.this, DownloadsActivity.class); + Bundle extras = new Bundle(); + extras.putString(BUNDLE_DOWNLOADS_URL, ""); + extras.putString(BUNDLE_DOWNLOADS_TITLE, null); + intent.putExtras(extras); + startActivity(intent); + } + } else if (drawerItem.equals(UPLOAD_ID)) { + if (!(BaseActivity.this instanceof UploadActivity)) { + Intent intent = new Intent(BaseActivity.this, UploadActivity.class); + startActivity(intent); + } + } else if (drawerItem.equals(BOOKMARKS_ID)) { + if (!(BaseActivity.this instanceof BookmarkActivity)) { + Intent intent = new Intent(BaseActivity.this, BookmarkActivity.class); + startActivity(intent); + } + } else if (drawerItem.equals(LOG_ID)) { + if (!sessionManager.isLoggedIn()) //When logged out or if user is guest + { + Intent intent = new Intent(BaseActivity.this, LoginActivity.class); + startActivity(intent); + finish(); + overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out); + } else + new LogoutTask().execute(); + } else if (drawerItem.equals(ABOUT_ID)) { + if (!(BaseActivity.this instanceof AboutActivity)) { + Intent intent = new Intent(BaseActivity.this, AboutActivity.class); + startActivity(intent); + } + } else if (drawerItem.equals(SETTINGS_ID)) { + if (!(BaseActivity.this instanceof SettingsActivity)) { + Intent intent = new Intent(BaseActivity.this, SettingsActivity.class); + startActivity(intent); } - - drawer.closeDrawer(); - return true; } + + drawer.closeDrawer(); + return true; }); if (sessionManager.isLoggedIn()) - drawerBuilder.addDrawerItems(homeItem, bookmarksItem, downloadsItem, uploadItem, settingsItem, loginLogoutItem, aboutItem); + drawerBuilder.addDrawerItems(homeItem, shoutboxItem, bookmarksItem, downloadsItem, uploadItem, settingsItem, loginLogoutItem, aboutItem); else - drawerBuilder.addDrawerItems(homeItem, bookmarksItem, settingsItem, loginLogoutItem, aboutItem); + drawerBuilder.addDrawerItems(homeItem, shoutboxItem, bookmarksItem, settingsItem, loginLogoutItem, aboutItem); drawer = drawerBuilder.build(); diff --git a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/ShoutboxViewModel.java b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/ShoutboxViewModel.java new file mode 100644 index 00000000..5aa92f65 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/ShoutboxViewModel.java @@ -0,0 +1,58 @@ +package gr.thmmy.mthmmy.viewmodel; + +import android.os.AsyncTask; + +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; +import gr.thmmy.mthmmy.activities.shoutbox.SendShoutTask; +import gr.thmmy.mthmmy.activities.shoutbox.ShoutboxTask; +import gr.thmmy.mthmmy.model.Shoutbox; +import gr.thmmy.mthmmy.session.SessionManager; + +public class ShoutboxViewModel extends ViewModel { + private MutableLiveData shoutboxMutableLiveData = new MutableLiveData<>(); + private ShoutboxTask shoutboxTask; + private ShoutboxTask.OnTaskStartedListener onShoutboxTaskStarted; + private ShoutboxTask.OnNetworkTaskFinishedListener onShoutboxTaskFinished; + private SendShoutTask.OnTaskStartedListener onSendShoutTaskStarted; + private SendShoutTask.OnNetworkTaskFinishedListener onSendShoutTaskFinished; + + public void loadShoutbox() { + if (shoutboxTask != null && shoutboxTask.getStatus() == AsyncTask.Status.RUNNING) + shoutboxTask.cancel(true); + shoutboxTask = new ShoutboxTask(onShoutboxTaskStarted, onShoutboxTaskFinished); + shoutboxTask.execute(SessionManager.shoutboxUrl.toString()); + } + + public void sendShout(String shout) { + if (shoutboxMutableLiveData.getValue() == null) throw new IllegalStateException("Shoutbox task has not finished yet!"); + Shoutbox shoutbox = shoutboxMutableLiveData.getValue(); + new SendShoutTask(onSendShoutTaskStarted, onSendShoutTaskFinished) + .execute(shoutbox.getSendShoutUrl(), shout, shoutbox.getSc(), + shoutbox.getShoutName(), shoutbox.getShoutSend(), shoutbox.getShoutUrl()); + } + + public void setShoutbox(Shoutbox shoutbox) { + shoutboxMutableLiveData.setValue(shoutbox); + } + + public MutableLiveData getShoutboxMutableLiveData() { + return shoutboxMutableLiveData; + } + + public void setOnSendShoutTaskFinished(SendShoutTask.OnNetworkTaskFinishedListener onSendShoutTaskFinished) { + this.onSendShoutTaskFinished = onSendShoutTaskFinished; + } + + public void setOnSendShoutTaskStarted(SendShoutTask.OnTaskStartedListener onSendShoutTaskStarted) { + this.onSendShoutTaskStarted = onSendShoutTaskStarted; + } + + public void setOnShoutboxTaskFinished(ShoutboxTask.OnNetworkTaskFinishedListener onShoutboxTaskFinished) { + this.onShoutboxTaskFinished = onShoutboxTaskFinished; + } + + public void setOnShoutboxTaskStarted(ShoutboxTask.OnTaskStartedListener onShoutboxTaskStarted) { + this.onShoutboxTaskStarted = onShoutboxTaskStarted; + } +} diff --git a/app/src/main/res/drawable/refresh_label_background.xml b/app/src/main/res/drawable/refresh_label_background.xml deleted file mode 100644 index 14379c28..00000000 --- a/app/src/main/res/drawable/refresh_label_background.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/activity_shoutbox.xml b/app/src/main/res/layout/activity_shoutbox.xml new file mode 100644 index 00000000..e341b84d --- /dev/null +++ b/app/src/main/res/layout/activity_shoutbox.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + diff --git a/app/src/main/res/layout/fragment_shoutbox.xml b/app/src/main/res/layout/fragment_shoutbox.xml index b52a034f..cf6dc7a0 100644 --- a/app/src/main/res/layout/fragment_shoutbox.xml +++ b/app/src/main/res/layout/fragment_shoutbox.xml @@ -1,6 +1,5 @@ - - - - - - - - + android:layout_weight="1" + tools:listitem="@layout/fragment_shoutbox_shout_row" />