package app.fedilab.fedilabtube; /* Copyright 2020 Thomas Schneider * * This file is a part of TubeLab * * This program is free software; you can redistribute it and/or modify it under the terms of the * GNU General Public License as published by the Free Software Foundation; either version 3 of the * License, or (at your option) any later version. * * TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * Public License for more details. * * You should have received a copy of the GNU General Public License along with TubeLab; if not, * see . */ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.SearchView; import androidx.navigation.NavController; import androidx.navigation.Navigation; import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.NavigationUI; import com.google.android.material.bottomnavigation.BottomNavigationView; import org.jetbrains.annotations.NotNull; import app.fedilab.fedilabtube.asynctasks.RetrievePeertubeInformationAsyncTask; import app.fedilab.fedilabtube.helper.Helper; import static app.fedilab.fedilabtube.helper.Helper.academies; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BottomNavigationView navView = findViewById(R.id.nav_view); try { new RetrievePeertubeInformationAsyncTask(MainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } catch (Exception ignored) { } // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( R.id.navigation_discover, R.id.navigation_trending, R.id.navigation_most_liked, R.id.navigation_recently_added, R.id.navigation_home) .build(); NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); NavigationUI.setupWithNavController(navView, navController); } @Override public boolean onCreateOptionsMenu(@NotNull Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); MenuItem myActionMenuItem = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) myActionMenuItem.getActionView(); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { Intent intent = new Intent(MainActivity.this, SearchActivity.class); Bundle b = new Bundle(); b.putString("search", query.trim()); intent.putExtras(b); startActivity(intent); if (!searchView.isIconified()) { searchView.setIconified(true); } myActionMenuItem.collapseActionView(); return false; } @Override public boolean onQueryTextChange(String s) { return false; } }); MenuItem instanceItem = menu.findItem(R.id.action_change_instance); MenuItem uploadItem = menu.findItem(R.id.action_upload); MenuItem myVideosItem = menu.findItem(R.id.action_myvideos); MenuItem playslistItem = menu.findItem(R.id.action_playlist); if (Helper.isLoggedIn(MainActivity.this)) { instanceItem.setVisible(false); uploadItem.setVisible(true); myVideosItem.setVisible(true); playslistItem.setVisible(true); } else { instanceItem.setVisible(true); uploadItem.setVisible(false); myVideosItem.setVisible(false); playslistItem.setVisible(false); } return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.action_change_instance) { showRadioButtonDialog(); return true; } else if (item.getItemId() == R.id.action_account) { Intent intent = new Intent(MainActivity.this, LoginActivity.class); startActivity(intent); return true; } else if (item.getItemId() == R.id.action_upload) { Intent intent = new Intent(MainActivity.this, PeertubeUploadActivity.class); startActivity(intent); return true; } else if (item.getItemId() == R.id.action_myvideos) { Intent intent = new Intent(MainActivity.this, MyVideosActivity.class); startActivity(intent); return true; } else if (item.getItemId() == R.id.action_playlist) { Intent intent = new Intent(MainActivity.this, AllPlaylistsActivity.class); startActivity(intent); return true; } return super.onOptionsItemSelected(item); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (intent == null) return; Bundle extras = intent.getExtras(); if (extras != null && extras.containsKey(Helper.INTENT_ACTION)) { if (extras.getInt(Helper.INTENT_ACTION) == Helper.ADD_USER_INTENT) { recreate(); } } } private void showRadioButtonDialog() { AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); alt_bld.setTitle(R.string.instance_choice); final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); String acad = sharedpreferences.getString(Helper.PREF_INSTANCE, "tube.ac-lyon.fr"); int i = 0; for (String item : academies) { if (item.compareTo(acad) == 0) { break; } i++; } alt_bld.setSingleChoiceItems(academies, i, (dialog, item) -> { String newInstance = academies[item]; SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Helper.PREF_INSTANCE, newInstance); editor.commit(); dialog.dismiss(); recreate(); }); AlertDialog alert = alt_bld.create(); alert.show(); } }