TubeLab-App-Android/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java

173 lines
6.8 KiB
Java
Raw Normal View History

2020-06-25 16:57:13 +02:00
package app.fedilab.fedilabtube;
2020-07-01 16:36:08 +02:00
/* 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 <http://www.gnu.org/licenses>. */
2020-06-25 16:57:13 +02:00
2020-06-27 12:23:03 +02:00
import android.content.Context;
2020-06-28 12:14:14 +02:00
import android.content.Intent;
2020-06-27 12:23:03 +02:00
import android.content.SharedPreferences;
2020-06-27 11:21:25 +02:00
import android.os.AsyncTask;
2020-06-25 16:57:13 +02:00
import android.os.Bundle;
2020-06-27 12:23:03 +02:00
import android.view.Menu;
import android.view.MenuItem;
2020-06-25 16:57:13 +02:00
2020-06-27 12:23:03 +02:00
import androidx.appcompat.app.AlertDialog;
2020-06-25 16:57:13 +02:00
import androidx.appcompat.app.AppCompatActivity;
2020-06-28 12:14:14 +02:00
import androidx.appcompat.widget.SearchView;
2020-06-25 16:57:13 +02:00
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
2020-06-27 11:21:25 +02:00
import com.google.android.material.bottomnavigation.BottomNavigationView;
2020-06-27 12:23:03 +02:00
import org.jetbrains.annotations.NotNull;
2020-06-27 11:21:25 +02:00
import app.fedilab.fedilabtube.asynctasks.RetrievePeertubeInformationAsyncTask;
2020-06-27 12:23:03 +02:00
import app.fedilab.fedilabtube.helper.Helper;
import static app.fedilab.fedilabtube.helper.Helper.academies;
2020-06-27 11:21:25 +02:00
2020-06-25 16:57:13 +02:00
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);
2020-06-27 11:21:25 +02:00
try {
new RetrievePeertubeInformationAsyncTask(MainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} catch (Exception ignored) {
}
2020-06-25 16:57:13 +02:00
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
2020-06-27 11:21:25 +02:00
R.id.navigation_discover, R.id.navigation_trending, R.id.navigation_most_liked, R.id.navigation_recently_added, R.id.navigation_home)
2020-06-25 16:57:13 +02:00
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
2020-06-27 12:23:03 +02:00
@Override
public boolean onCreateOptionsMenu(@NotNull Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
2020-06-30 12:00:55 +02:00
2020-06-28 19:11:39 +02:00
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
2020-06-28 12:14:14 +02:00
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);
2020-06-28 19:11:39 +02:00
if (!searchView.isIconified()) {
2020-06-28 12:14:14 +02:00
searchView.setIconified(true);
}
myActionMenuItem.collapseActionView();
return false;
}
2020-06-28 19:11:39 +02:00
2020-06-28 12:14:14 +02:00
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
2020-06-30 17:33:23 +02:00
MenuItem instanceItem = menu.findItem(R.id.action_change_instance);
2020-06-30 12:00:55 +02:00
MenuItem uploadItem = menu.findItem(R.id.action_upload);
2020-06-30 13:33:43 +02:00
MenuItem myVideosItem = menu.findItem(R.id.action_myvideos);
2020-06-30 17:26:20 +02:00
MenuItem playslistItem = menu.findItem(R.id.action_playlist);
2020-06-30 13:33:43 +02:00
if (Helper.isLoggedIn(MainActivity.this)) {
2020-06-30 17:33:23 +02:00
instanceItem.setVisible(false);
2020-06-30 12:00:55 +02:00
uploadItem.setVisible(true);
2020-06-30 13:33:43 +02:00
myVideosItem.setVisible(true);
2020-06-30 17:26:20 +02:00
playslistItem.setVisible(true);
2020-06-30 13:33:43 +02:00
} else {
2020-06-30 17:33:23 +02:00
instanceItem.setVisible(true);
2020-06-30 12:00:55 +02:00
uploadItem.setVisible(false);
2020-06-30 13:33:43 +02:00
myVideosItem.setVisible(false);
2020-06-30 17:26:20 +02:00
playslistItem.setVisible(false);
2020-06-30 12:00:55 +02:00
}
2020-06-27 12:23:03 +02:00
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_change_instance) {
showRadioButtonDialog();
return true;
2020-06-30 13:33:43 +02:00
} else if (item.getItemId() == R.id.action_account) {
2020-06-28 19:11:39 +02:00
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
return true;
2020-06-30 13:33:43 +02:00
} else if (item.getItemId() == R.id.action_upload) {
2020-06-30 12:00:55 +02:00
Intent intent = new Intent(MainActivity.this, PeertubeUploadActivity.class);
startActivity(intent);
return true;
2020-06-30 13:33:43 +02:00
} else if (item.getItemId() == R.id.action_myvideos) {
Intent intent = new Intent(MainActivity.this, MyVideosActivity.class);
startActivity(intent);
return true;
2020-06-30 17:26:20 +02:00
} else if (item.getItemId() == R.id.action_playlist) {
Intent intent = new Intent(MainActivity.this, AllPlaylistsActivity.class);
startActivity(intent);
return true;
2020-06-30 12:00:55 +02:00
}
2020-06-27 12:23:03 +02:00
return super.onOptionsItemSelected(item);
}
2020-06-28 19:11:39 +02:00
@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();
}
}
}
2020-06-27 12:23:03 +02:00
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);
2020-07-01 15:19:24 +02:00
String acad = sharedpreferences.getString(Helper.PREF_INSTANCE, "tube.ac-lyon.fr");
2020-06-27 12:23:03 +02:00
int i = 0;
2020-06-27 19:08:52 +02:00
for (String item : academies) {
if (item.compareTo(acad) == 0) {
2020-06-27 12:23:03 +02:00
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();
}
2020-06-25 16:57:13 +02:00
}