From 54aa40eac1f3a3670a67765a6d59b5846f8d8c68 Mon Sep 17 00:00:00 2001 From: wb9688 Date: Sun, 19 Nov 2017 17:21:46 +0100 Subject: [PATCH] Add simple drawer for selecting service --- .../java/org/schabi/newpipe/MainActivity.java | 49 ++++++++++++++++++- .../newpipe/fragments/MainFragment.java | 5 +- app/src/main/res/layout/activity_main.xml | 28 ++++++++--- app/src/main/res/menu/drawer_items.xml | 5 ++ app/src/main/res/values/settings_keys.xml | 12 ++--- app/src/main/res/values/strings.xml | 6 +++ app/src/main/res/xml/content_settings.xml | 4 +- 7 files changed, 86 insertions(+), 23 deletions(-) create mode 100644 app/src/main/res/menu/drawer_items.xml diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 056db3500..7bc1747bb 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -28,8 +28,13 @@ import android.os.Looper; import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.design.widget.NavigationView; import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.view.GravityCompat; +import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; +import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; @@ -85,8 +90,50 @@ public class MainActivity extends AppCompatActivity implements HistoryListener { initFragments(); } - Toolbar toolbar = findViewById(R.id.toolbar); + final Toolbar toolbar = findViewById(R.id.toolbar); + final DrawerLayout drawer = findViewById(R.id.drawer_layout); + final NavigationView drawerItems = findViewById(R.id.navigation); setSupportActionBar(toolbar); + + final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close); + toggle.syncState(); + drawer.addDrawerListener(toggle); + + getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { + @Override + public void onBackStackChanged() { + if (getSupportFragmentManager().getBackStackEntryCount() > 1) { + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + toolbar.setNavigationOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + onBackPressed(); + } + }); + } else { + getSupportActionBar().setDisplayHomeAsUpEnabled(false); + toggle.syncState(); + toolbar.setNavigationOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + drawer.openDrawer(GravityCompat.START); + } + }); + } + } + }); + + drawerItems.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { + public boolean onNavigationItemSelected(@NonNull MenuItem item) { + SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit(); + editor.putString("service", item.getTitle().toString()); + editor.apply(); + drawer.closeDrawers(); + return true; + } + }); + + sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); initHistory(); diff --git a/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java index a3beb712c..f01807bf2 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java @@ -30,13 +30,10 @@ import org.schabi.newpipe.fragments.list.kiosk.KioskFragment; import org.schabi.newpipe.fragments.subscription.SubscriptionFragment; import org.schabi.newpipe.report.ErrorActivity; import org.schabi.newpipe.report.UserAction; -import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.KioskTranslator; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.ThemeHelper; -import java.util.concurrent.ExecutionException; - public class MainFragment extends BaseFragment implements TabLayout.OnTabSelectedListener { private ViewPager viewPager; private boolean showBlankTab = false; @@ -139,7 +136,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_search: - NavigationHelper.openSearchFragment(getFragmentManager(), Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("service", "0")), ""); + NavigationHelper.openSearchFragment(getFragmentManager(), NewPipe.getIdOfService(PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("service", "YouTube")), ""); return true; } return super.onOptionsItemSelected(item); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 88a015e81..7eafc6c69 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,18 +1,30 @@ - + android:layout_height="match_parent"> + android:orientation="vertical" + tools:context="org.schabi.newpipe.MainActivity"> + - + + - + + \ No newline at end of file diff --git a/app/src/main/res/menu/drawer_items.xml b/app/src/main/res/menu/drawer_items.xml new file mode 100644 index 000000000..2f82327c3 --- /dev/null +++ b/app/src/main/res/menu/drawer_items.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index d088432c9..89cc71e44 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -46,16 +46,12 @@ 144p - - YouTube - SoundCloud - - - 0 - 1 + + @string/youtube + @string/soundcloud service - 0 + @string/youtube video_mp4 video_webm diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 65b7ed386..ded1d770c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -309,4 +309,10 @@ Details Audio Settings Hold To Enqueue + + + Open Drawer + Close Drawer + YouTube + SoundCloud diff --git a/app/src/main/res/xml/content_settings.xml b/app/src/main/res/xml/content_settings.xml index 8778b65f2..2a84ef0ef 100644 --- a/app/src/main/res/xml/content_settings.xml +++ b/app/src/main/res/xml/content_settings.xml @@ -5,8 +5,8 @@