Switch from accounts

This commit is contained in:
Thomas 2020-11-10 18:44:48 +01:00
parent 58f10d9372
commit 17c84f6c37
18 changed files with 923 additions and 102 deletions

View File

@ -20,7 +20,7 @@
<string name="show_more">Montrer plus</string>
<string name="show_less">Montrer moins</string>
<string name="no_instances">Aucune instance !</string>
<string name="set_play_screen_lock">Verrouillage d\'écran</string>
<string name="set_play_screen_lock_description">Continuer à lire des vidéos lorsque l\'écran est verrouillé</string>
@ -165,8 +165,12 @@
<string name="app_logo">Logo de lapplication</string>
<!-- languages not translated -->
<string name="subscriptions">Abonnements</string>
<string name="delete_instance">Supprimer l\'instance</string>
<string name="delete_instance_confirm">Etes-vous sûr de vouloir supprimer cette instance ?</string>
<string name="delete_comment">Supprimer le commentaire</string>
<string name="delete_comment_confirm">Etes-vous sûr de vouloir supprimer ce commentaire ?</string>
<string name="set_video_mode">Mode pour les vidéos</string>
<string name="filter">Filtrer</string>
<string name="sepia_search">Recherche sépia</string>

View File

@ -16,7 +16,7 @@
<string name="set_video_sensitive_choice" translatable="false">set_video_sensitive_choice</string>
<string name="set_video_in_list">Videos in list</string>
<string name="set_video_in_list_description">Change the layout for displaying videos in a list</string>
<string name="no_instances">No instances !</string>
<string name="show_more">Show more</string>
<string name="show_less">Show less</string>
@ -180,6 +180,9 @@
<!-- languages not translated -->
<string name="subscriptions">Subscriptions</string>
<string name="delete_instance">Delete an instance</string>
<string name="delete_instance_confirm">Are you sure to delete this instance?</string>
<string name="delete_comment">Delete a comment</string>
<string name="delete_comment_confirm">Are you sure to delete this comment?</string>
<string name="set_video_mode">Mode for videos</string>

View File

@ -41,15 +41,13 @@ import com.google.android.material.tabs.TabLayout;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
import app.fedilab.fedilabtube.client.data.AccountData.Account;
import app.fedilab.fedilabtube.drawer.OwnAccountsAdapter;
import app.fedilab.fedilabtube.fragment.DisplayAccountsFragment;
import app.fedilab.fedilabtube.fragment.DisplayChannelsFragment;
import app.fedilab.fedilabtube.fragment.DisplayNotificationsFragment;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.helper.SwitchAccountHelper;
import app.fedilab.fedilabtube.sqlite.AccountDAO;
import app.fedilab.fedilabtube.sqlite.Sqlite;
@ -210,42 +208,7 @@ public class AccountActivity extends AppCompatActivity {
finish();
return true;
} else if (item.getItemId() == R.id.action_add_account) {
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<Account> accounts = new AccountDAO(AccountActivity.this, db).getAllAccount();
AlertDialog.Builder builderSingle = new AlertDialog.Builder(AccountActivity.this);
builderSingle.setTitle(getString(R.string.list_of_accounts));
if (accounts != null) {
final OwnAccountsAdapter accountsListAdapter = new OwnAccountsAdapter(AccountActivity.this, accounts);
final Account[] accountArray = new Account[accounts.size()];
int i = 0;
for (Account account : accounts) {
accountArray[i] = account;
i++;
}
builderSingle.setAdapter(accountsListAdapter, (dialog, which) -> {
final Account account = accountArray[which];
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, account.getToken());
editor.putString(Helper.PREF_INSTANCE, account.getHost());
editor.putString(Helper.PREF_KEY_ID, account.getId());
editor.putString(Helper.PREF_KEY_NAME, account.getUsername());
editor.apply();
dialog.dismiss();
Intent intent = new Intent(AccountActivity.this, MainActivity.class);
startActivity(intent);
finish();
});
}
builderSingle.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
builderSingle.setPositiveButton(R.string.add_account, (dialog, which) -> {
Intent intent = new Intent(AccountActivity.this, LoginActivity.class);
startActivity(intent);
finish();
});
builderSingle.show();
SwitchAccountHelper.switchDialog(AccountActivity.this, true);
}
return super.onOptionsItemSelected(item);
}

View File

@ -39,7 +39,6 @@ import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.kobakei.ratethisapp.RateThisApp;
@ -55,7 +54,7 @@ import java.util.regex.Pattern;
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
import app.fedilab.fedilabtube.client.data.AccountData.Account;
import app.fedilab.fedilabtube.client.data.VideoData;
import app.fedilab.fedilabtube.client.data.InstanceData;
import app.fedilab.fedilabtube.client.entities.Error;
import app.fedilab.fedilabtube.client.entities.OauthParams;
import app.fedilab.fedilabtube.client.entities.PeertubeInformation;
@ -66,13 +65,16 @@ import app.fedilab.fedilabtube.client.entities.WellKnownNodeinfo;
import app.fedilab.fedilabtube.fragment.DisplayOverviewFragment;
import app.fedilab.fedilabtube.fragment.DisplayVideosFragment;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.helper.SwitchAccountHelper;
import app.fedilab.fedilabtube.services.RetrieveInfoService;
import app.fedilab.fedilabtube.sqlite.AccountDAO;
import app.fedilab.fedilabtube.sqlite.Sqlite;
import app.fedilab.fedilabtube.viewmodel.SearchVM;
import app.fedilab.fedilabtube.sqlite.StoredInstanceDAO;
import app.fedilab.fedilabtube.viewmodel.TimelineVM;
import es.dmoral.toasty.Toasty;
import static app.fedilab.fedilabtube.MainActivity.TypeOfConnection.NORMAL;
import static app.fedilab.fedilabtube.MainActivity.TypeOfConnection.SURFING;
import static app.fedilab.fedilabtube.helper.Helper.academies;
public class MainActivity extends AppCompatActivity {
@ -80,12 +82,20 @@ public class MainActivity extends AppCompatActivity {
public static PeertubeInformation peertubeInformation;
public static int PICK_INSTANCE = 5641;
public static int PICK_INSTANCE_SURF = 5642;
final FragmentManager fm = getSupportFragmentManager();
Fragment active;
private DisplayVideosFragment recentFragment, locaFragment, trendingFragment, subscriptionFragment, mostLikedFragment;
private DisplayOverviewFragment overviewFragment;
public static UserMe userMe;
public enum TypeOfConnection{
UNKNOWN,
NORMAL,
SURFING
}
private TypeOfConnection typeOfConnection;
private final BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= item -> {
@ -136,6 +146,7 @@ public class MainActivity extends AppCompatActivity {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
typeOfConnection = TypeOfConnection.UNKNOWN;
BottomNavigationView navView = findViewById(R.id.nav_view);
@ -144,6 +155,7 @@ public class MainActivity extends AppCompatActivity {
if( getSupportActionBar() != null) {
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
checkIfConnectedUsers();
Fragment fragment = getSupportFragmentManager().findFragmentByTag("5");
if (fragment != null)
@ -212,15 +224,6 @@ public class MainActivity extends AppCompatActivity {
if (Helper.isLoggedIn(MainActivity.this)) {
if( BuildConfig.full_instances) {
ImageButton instances = toolbar.findViewById(R.id.instances);
instances.setVisibility(View.VISIBLE);
instances.setOnClickListener(v->{
Intent intent = new Intent(MainActivity.this, ManageInstancesActivity.class);
startActivity(intent);
overridePendingTransition( R.anim.slide_in_up, R.anim.slide_out_up );
});
}
navView.inflateMenu(R.menu.bottom_nav_menu_connected);
new Thread(() -> {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
@ -353,7 +356,7 @@ public class MainActivity extends AppCompatActivity {
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);
@ -362,6 +365,36 @@ public class MainActivity extends AppCompatActivity {
MenuItem settingsItem = menu.findItem(R.id.action_settings);
MenuItem sepiaSearchItem = menu.findItem(R.id.action_sepia_search);
MenuItem incognitoItem = menu.findItem(R.id.action_incognito);
MenuItem instanceItem = menu.findItem(R.id.action_change_instance);
MenuItem accountItem = menu.findItem(R.id.action_account);
Toolbar toolbar = findViewById(R.id.toolbar);
ImageButton instances = toolbar.findViewById(R.id.instances);
if(BuildConfig.full_instances && ((Helper.isLoggedIn(MainActivity.this) && typeOfConnection == NORMAL) || typeOfConnection == SURFING)) {
instances.setVisibility(View.VISIBLE);
instances.setOnClickListener(null);
instances.setOnClickListener(v->{
Intent intent = new Intent(MainActivity.this, ManageInstancesActivity.class);
startActivity(intent);
overridePendingTransition( R.anim.slide_in_up, R.anim.slide_out_up );
});
}else {
instances.setVisibility(View.GONE);
}
switch (typeOfConnection){
case UNKNOWN:
instanceItem.setVisible(false);
accountItem.setVisible(false);
uploadItem.setVisible(false);
myVideosItem.setVisible(false);
playslistItem.setVisible(false);
historyItem.setVisible(false);
settingsItem.setVisible(false);
mostLikedItem.setVisible(false);
incognitoItem.setVisible(false);
break;
case NORMAL:
accountItem.setVisible(true);
if (Helper.isLoggedIn(MainActivity.this)) {
instanceItem.setVisible(false);
uploadItem.setVisible(true);
@ -384,18 +417,52 @@ public class MainActivity extends AppCompatActivity {
mostLikedItem.setVisible(false);
incognitoItem.setVisible(false);
}
break;
case SURFING:
instanceItem.setVisible(false);
accountItem.setVisible(true);
uploadItem.setVisible(false);
myVideosItem.setVisible(false);
playslistItem.setVisible(false);
historyItem.setVisible(false);
settingsItem.setVisible(false);
mostLikedItem.setVisible(false);
incognitoItem.setVisible(false);
break;
}
if (!BuildConfig.full_instances) {
sepiaSearchItem.setVisible(false);
}
return true;
}
private void checkIfConnectedUsers(){
new Thread(() -> {
try {
typeOfConnection = NORMAL;
if( !Helper.isLoggedIn(MainActivity.this)) {
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<Account> accounts = new AccountDAO(MainActivity.this, db).getAllAccount();
if( accounts != null && accounts.size() > 0 ) {
//The user is not authenticated and there accounts in db. That means the user is surfing some other instances
typeOfConnection = TypeOfConnection.SURFING;
}
}
runOnUiThread(this::invalidateOptionsMenu);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_change_instance) {
if (BuildConfig.full_instances) {
showRadioButtonDialogFullInstances(MainActivity.this);
showRadioButtonDialogFullInstances(MainActivity.this, false);
} else {
showRadioButtonDialog();
}
@ -405,12 +472,16 @@ public class MainActivity extends AppCompatActivity {
startActivity(intent);
} else if (item.getItemId() == R.id.action_account) {
Intent intent;
if( typeOfConnection == SURFING) {
SwitchAccountHelper.switchDialog(MainActivity.this, false);
} else {
if (Helper.isLoggedIn(MainActivity.this)) {
intent = new Intent(MainActivity.this, AccountActivity.class);
} else {
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);
@ -519,8 +590,10 @@ public class MainActivity extends AppCompatActivity {
alert.show();
}
@SuppressLint("ApplySharedPref")
public static void showRadioButtonDialogFullInstances(Activity activity) {
public static void showRadioButtonDialogFullInstances(Activity activity, boolean storeInDb) {
final SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
AlertDialog.Builder alt_bld = new AlertDialog.Builder(activity);
alt_bld.setTitle(R.string.instance_choice);
@ -541,11 +614,22 @@ public class MainActivity extends AppCompatActivity {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_INSTANCE, newInstance);
editor.commit();
if( storeInDb) {
newInstance = newInstance.trim().toLowerCase();
InstanceData.AboutInstance aboutInstance = new RetrofitPeertubeAPI(activity, newInstance, null).getAboutInstance();
SQLiteDatabase db = Sqlite.getInstance(activity.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
new StoredInstanceDAO(activity, db).insertInstance(aboutInstance, newInstance);
activity.runOnUiThread(() -> {
dialog.dismiss();
Helper.logoutNoRemoval(activity);
});
}else {
activity.runOnUiThread(() -> {
dialog.dismiss();
Intent intent = new Intent(activity, MainActivity.class);
activity.startActivity(intent);
});
}
} else {
activity.runOnUiThread(() -> Toasty.error(activity, activity.getString(R.string.not_valide_instance), Toast.LENGTH_LONG).show());
}
@ -557,7 +641,11 @@ public class MainActivity extends AppCompatActivity {
alt_bld.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
alt_bld.setNeutralButton(R.string.help, (dialog, which) -> {
Intent intent = new Intent(activity, InstancePickerActivity.class);
if(storeInDb) {
activity.startActivityForResult(intent, PICK_INSTANCE_SURF);
}else{
activity.startActivityForResult(intent, PICK_INSTANCE);
}
});
AlertDialog alert = alt_bld.create();
alert.show();
@ -569,6 +657,14 @@ public class MainActivity extends AppCompatActivity {
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_INSTANCE && resultCode == Activity.RESULT_OK) {
if (data != null && data.getData() != null) {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_INSTANCE, String.valueOf(data.getData()));
editor.commit();
finish();
}
}else if (requestCode == PICK_INSTANCE_SURF && resultCode == Activity.RESULT_OK) {
if (data != null && data.getData() != null) {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();

View File

@ -16,24 +16,28 @@ package app.fedilab.fedilabtube;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import java.util.List;
import app.fedilab.fedilabtube.client.data.InstanceData;
import app.fedilab.fedilabtube.databinding.ActivityManageInstancesBinding;
import app.fedilab.fedilabtube.drawer.AboutInstanceAdapter;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.viewmodel.InfoInstanceVM;
import static app.fedilab.fedilabtube.MainActivity.PICK_INSTANCE;
import static app.fedilab.fedilabtube.MainActivity.PICK_INSTANCE_SURF;
import static app.fedilab.fedilabtube.MainActivity.showRadioButtonDialogFullInstances;
public class ManageInstancesActivity extends AppCompatActivity {
public class ManageInstancesActivity extends AppCompatActivity implements AboutInstanceAdapter.AllInstancesRemoved {
private ActivityManageInstancesBinding binding;
@ -47,8 +51,24 @@ public class ManageInstancesActivity extends AppCompatActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
binding.loader.setVisibility(View.VISIBLE);
binding.noAction.setVisibility(View.GONE);
binding.lvInstances.setVisibility(View.GONE);
binding.actionButton.setOnClickListener(v-> showRadioButtonDialogFullInstances(ManageInstancesActivity.this, true));
binding.actionButton.setOnClickListener(v-> showRadioButtonDialogFullInstances(ManageInstancesActivity.this));
InfoInstanceVM viewModelInfoInstance = new ViewModelProvider(ManageInstancesActivity.this).get(InfoInstanceVM.class);
viewModelInfoInstance.getInstances().observe(ManageInstancesActivity.this, this::manageVIewInfoInstance);
}
private void manageVIewInfoInstance(List<InstanceData.AboutInstance> aboutInstances) {
binding.loader.setVisibility(View.GONE);
if( aboutInstances == null || aboutInstances.size() == 0) {
binding.noAction.setVisibility(View.VISIBLE);
binding.lvInstances.setVisibility(View.GONE);
return;
}
binding.noAction.setVisibility(View.GONE);
binding.lvInstances.setVisibility(View.VISIBLE);
}
@ -72,16 +92,16 @@ public class ManageInstancesActivity extends AppCompatActivity {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_INSTANCE && resultCode == Activity.RESULT_OK) {
if (requestCode == PICK_INSTANCE_SURF && resultCode == Activity.RESULT_OK) {
if (data != null && data.getData() != null) {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_INSTANCE, String.valueOf(data.getData()));
editor.commit();
Intent intent = new Intent(ManageInstancesActivity.this, MainActivity.class);
startActivity(intent);
finish();
Helper.logoutNoRemoval(ManageInstancesActivity.this);
}
}
}
@Override
public void onAllInstancesRemoved() {
binding.noAction.setVisibility(View.VISIBLE);
binding.lvInstances.setVisibility(View.GONE);
}
}

View File

@ -66,6 +66,10 @@ public interface PeertubeService {
@GET(".well-known/nodeinfo")
Call<WellKnownNodeinfo> getWellKnownNodeinfo();
//Instance info
@GET("config/about")
Call<InstanceData.AboutInstance> configAbout();
@GET("{nodeInfoPath}")
Call<WellKnownNodeinfo.NodeInfo> getNodeinfo(@Path(value = "nodeInfoPath", encoded = true) String nodeInfoPath);

View File

@ -597,6 +597,25 @@ public class RetrofitPeertubeAPI {
return apiResponse;
}
/**
* About the instance
* @return AboutInstance
*/
public InstanceData.AboutInstance getAboutInstance() {
PeertubeService peertubeService = init();
Call<InstanceData.AboutInstance> about = peertubeService.configAbout();
try {
Response<InstanceData.AboutInstance> response = about.execute();
if (response.isSuccessful() && response.body() != null) {
return response.body();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* Returns informations about Peertube such privacies, licenses, etc.
*

View File

@ -1,9 +1,12 @@
package app.fedilab.fedilabtube.client.data;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.SpannableStringBuilder;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
@ -239,5 +242,94 @@ public class InstanceData {
}
}
public static class AboutInstance implements Parcelable, Serializable {
@SerializedName("name")
private String name;
@SerializedName("shortDescription")
private String shortDescription;
@SerializedName("description")
private String description;
@SerializedName("terms")
private String terms;
private String host;
public AboutInstance(){}
protected AboutInstance(Parcel in) {
name = in.readString();
shortDescription = in.readString();
description = in.readString();
terms = in.readString();
host = in.readString();
}
public static final Creator<AboutInstance> CREATOR = new Creator<AboutInstance>() {
@Override
public AboutInstance createFromParcel(Parcel in) {
return new AboutInstance(in);
}
@Override
public AboutInstance[] newArray(int size) {
return new AboutInstance[size];
}
};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTerms() {
return terms;
}
public void setTerms(String terms) {
this.terms = terms;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeString(shortDescription);
parcel.writeString(description);
parcel.writeString(terms);
parcel.writeString(host);
}
}
}

View File

@ -0,0 +1,149 @@
package app.fedilab.fedilabtube.drawer;
/* 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>. */
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.PopupMenu;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import app.fedilab.fedilabtube.MainActivity;
import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
import app.fedilab.fedilabtube.client.data.InstanceData;
import app.fedilab.fedilabtube.databinding.DrawerAboutInstanceBinding;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.sqlite.Sqlite;
import app.fedilab.fedilabtube.sqlite.StoredInstanceDAO;
public class AboutInstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final List<InstanceData.AboutInstance> aboutInstances;
private final AboutInstanceAdapter aboutInstanceAdapter;
public AllInstancesRemoved allInstancesRemoved;
private Context context;
public AboutInstanceAdapter(List<InstanceData.AboutInstance> aboutInstances) {
aboutInstanceAdapter = this;
this.aboutInstances = aboutInstances;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return aboutInstances.size();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
DrawerAboutInstanceBinding itemBinding = DrawerAboutInstanceBinding .inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new ViewHolder(itemBinding);
}
@SuppressLint("ApplySharedPref")
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int i) {
context = viewHolder.itemView.getContext();
final ViewHolder holder = (ViewHolder) viewHolder;
final InstanceData.AboutInstance aboutInstance = aboutInstances.get(i);
holder.binding.aboutInstanceDescription.setText(aboutInstance.getShortDescription());
holder.binding.aboutInstanceName.setText(aboutInstance.getName());
holder.binding.playlistContainer.setOnClickListener(v->{
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_INSTANCE, aboutInstance.getHost());
editor.commit();
((Activity)context).runOnUiThread(() -> {
Intent intent = new Intent(context, MainActivity.class);
context.startActivity(intent);
});
});
holder.binding.instanceMore.setOnClickListener(v->{
PopupMenu popup = new PopupMenu(context, holder.binding.instanceMore);
popup.getMenuInflater()
.inflate(R.menu.instance_menu, popup.getMenu());
popup.setOnMenuItemClickListener(item -> {
int itemId = item.getItemId();
if (itemId == R.id.action_delete) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.delete_instance);
builder.setMessage(R.string.delete_instance_confirm);
builder.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(R.string.delete, (dialog, which) -> {
new Thread(() -> {
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
new StoredInstanceDAO(context, db).removeInstance(aboutInstance.getHost());
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> {
if (aboutInstances.size() == 0) {
allInstancesRemoved.onAllInstancesRemoved();
}
};
mainHandler.post(myRunnable);
}).start();
dialog.dismiss();
})
.setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss())
.show();
}
return true;
});
});
}
public interface AllInstancesRemoved {
void onAllInstancesRemoved();
}
static class ViewHolder extends RecyclerView.ViewHolder {
DrawerAboutInstanceBinding binding;
ViewHolder(DrawerAboutInstanceBinding itemView) {
super(itemView.getRoot());
binding = itemView;
}
}
}

View File

@ -121,6 +121,7 @@ public class Helper {
public static final String PREF_KEY_ID = "userID";
public static final String PREF_KEY_NAME = "my_user_name";
public static final String PREF_INSTANCE = "instance";
public static final String PREF_INSTANCE_SURF = "instance_surf";
public static final int EXTERNAL_STORAGE_REQUEST_CODE = 84;
public static final String SET_VIDEOS_PER_PAGE = "set_videos_per_page";
public static final String VIDEO_ID = "video_id_update";
@ -626,6 +627,27 @@ public class Helper {
}
/**
* Log out without removing user in db
*
* @param activity Activity
*/
public static void logoutNoRemoval(Activity activity) {
SharedPreferences sharedpreferences = activity.getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(PREF_KEY_OAUTH_TOKEN, null);
editor.putString(CLIENT_ID, null);
editor.putString(CLIENT_SECRET, null);
editor.putString(PREF_KEY_ID, null);
editor.putString(PREF_INSTANCE, null);
editor.putString(ID, null);
editor.apply();
Intent loginActivity = new Intent(activity, MainActivity.class);
activity.startActivity(loginActivity);
activity.finish();
}
public static int getAttColor(Context context, int attColor) {
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(attColor, typedValue, true);

View File

@ -0,0 +1,80 @@
package app.fedilab.fedilabtube.helper;
/* 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>. */
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import androidx.appcompat.app.AlertDialog;
import java.util.List;
import app.fedilab.fedilabtube.LoginActivity;
import app.fedilab.fedilabtube.MainActivity;
import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.client.data.AccountData;
import app.fedilab.fedilabtube.drawer.OwnAccountsAdapter;
import app.fedilab.fedilabtube.sqlite.AccountDAO;
import app.fedilab.fedilabtube.sqlite.Sqlite;
import static android.content.Context.MODE_PRIVATE;
public class SwitchAccountHelper {
public static void switchDialog(Activity activity, boolean withAddAccount){
SQLiteDatabase db = Sqlite.getInstance(activity.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<AccountData.Account> accounts = new AccountDAO(activity, db).getAllAccount();
AlertDialog.Builder builderSingle = new AlertDialog.Builder(activity);
builderSingle.setTitle(activity.getString(R.string.list_of_accounts));
if (accounts != null) {
final OwnAccountsAdapter accountsListAdapter = new OwnAccountsAdapter(activity, accounts);
final AccountData.Account[] accountArray = new AccountData.Account[accounts.size()];
int i = 0;
for (AccountData.Account account : accounts) {
accountArray[i] = account;
i++;
}
builderSingle.setAdapter(accountsListAdapter, (dialog, which) -> {
final AccountData.Account account = accountArray[which];
SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, account.getToken());
editor.putString(Helper.PREF_INSTANCE, account.getHost());
editor.putString(Helper.PREF_KEY_ID, account.getId());
editor.putString(Helper.PREF_KEY_NAME, account.getUsername());
editor.apply();
dialog.dismiss();
Intent intent = new Intent(activity, MainActivity.class);
activity.startActivity(intent);
activity.finish();
});
}
builderSingle.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
if( withAddAccount) {
builderSingle.setPositiveButton(R.string.add_account, (dialog, which) -> {
Intent intent = new Intent(activity, LoginActivity.class);
activity.startActivity(intent);
activity.finish();
});
}
builderSingle.show();
}
}

View File

@ -85,11 +85,13 @@ public class Sqlite extends SQLiteOpenHelper {
static final String COL_USER_INSTANCE = "USER_INSTANCE";
static final String TABLE_BOOKMARKED_INSTANCES = "BOOKMARKED_INSTANCES";
static final String COL_ABOUT = "ABOUT";
private final String CREATE_TABLE_STORED_INSTANCES = "CREATE TABLE "
+ TABLE_BOOKMARKED_INSTANCES + "("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_INSTANCE + " TEXT NOT NULL, "
+ COL_USER_ID + " TEXT NOT NULL, "
+ COL_ABOUT + " TEXT NOT NULL, "
+ COL_USER_INSTANCE + " TEXT NOT NULL)";
public Sqlite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {

View File

@ -0,0 +1,196 @@
package app.fedilab.fedilabtube.sqlite;
/* 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>. */
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import app.fedilab.fedilabtube.client.data.InstanceData;
import app.fedilab.fedilabtube.helper.Helper;
@SuppressWarnings("UnusedReturnValue")
public class StoredInstanceDAO {
private final SQLiteDatabase db;
public Context context;
public StoredInstanceDAO(Context context, SQLiteDatabase db) {
//Creation of the DB with tables
this.context = context;
this.db = db;
}
/**
* Insert instance info in database
*
* @param aboutInstance AboutInstance
* @param targetedInstance String
* @return boolean
*/
public boolean insertInstance(InstanceData.AboutInstance aboutInstance, String targetedInstance) {
ContentValues values = new ContentValues();
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = Helper.getLiveInstance(context);
values.put(Sqlite.COL_USER_ID, userId);
values.put(Sqlite.COL_USER_INSTANCE, instance);
values.put(Sqlite.COL_ABOUT, aboutInstanceToStringStorage(aboutInstance));
values.put(Sqlite.COL_INSTANCE, targetedInstance);
//Inserts account
try {
db.insertOrThrow(Sqlite.TABLE_BOOKMARKED_INSTANCES, null, values);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* Unserialized AboutInstance
*
* @param serializedAboutInstance String serialized AboutInstance
* @return AboutInstance
*/
public static InstanceData.AboutInstance restoreAboutInstanceFromString(String serializedAboutInstance) {
Gson gson = new Gson();
try {
return gson.fromJson(serializedAboutInstance, InstanceData.AboutInstance.class);
} catch (Exception e) {
return null;
}
}
/**
* Serialized AboutInstance class
*
* @param aboutInstance AboutInstance to serialize
* @return String serialized AboutInstance
*/
public static String aboutInstanceToStringStorage(InstanceData.AboutInstance aboutInstance) {
Gson gson = new Gson();
try {
return gson.toJson(aboutInstance);
} catch (Exception e) {
return null;
}
}
/**
* Insert instance info in database
*
* @param aboutInstance AboutInstance
* @param targetedInstance String
* @return int
*/
public int updateInstance(InstanceData.AboutInstance aboutInstance, String targetedInstance) {
ContentValues values = new ContentValues();
values.put(Sqlite.COL_ABOUT, aboutInstanceToStringStorage(aboutInstance));
try {
return db.update(Sqlite.TABLE_BOOKMARKED_INSTANCES,
values, Sqlite.COL_INSTANCE + " = ?",
new String[]{targetedInstance});
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
public int removeInstance(String instance) {
return db.delete(Sqlite.TABLE_BOOKMARKED_INSTANCES, Sqlite.COL_INSTANCE + " = '" +instance + "'", null);
}
/**
* Returns all Instance in db
*
* @return List<AboutInstance>
*/
public List<InstanceData.AboutInstance> getAllInstances() {
try {
Cursor c = db.query(Sqlite.TABLE_BOOKMARKED_INSTANCES, null, null, null, null, null, Sqlite.COL_INSTANCE + " ASC", null);
return cursorToListInstances(c);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/***
* Method to hydrate an Account from database
* @param c Cursor
* @return Account
*/
private InstanceData.AboutInstance cursorToInstance(Cursor c) {
//No element found
if (c.getCount() == 0) {
c.close();
return null;
}
//Take the first element
c.moveToFirst();
//New user
String aboutInstanceStr = c.getString(c.getColumnIndex(Sqlite.COL_ABOUT));
InstanceData.AboutInstance aboutInstance = restoreAboutInstanceFromString(aboutInstanceStr);
//Close the cursor
c.close();
return aboutInstance;
}
/***
* Method to hydrate an AboutInstance from database
* @param c Cursor
* @return List<AboutInstance>
*/
private List<InstanceData.AboutInstance> cursorToListInstances(Cursor c) {
//No element found
if (c.getCount() == 0) {
c.close();
return null;
}
List<InstanceData.AboutInstance> aboutInstances = new ArrayList<>();
while (c.moveToNext()) {
String aboutInstanceStr = c.getString(c.getColumnIndex(Sqlite.COL_ABOUT));
InstanceData.AboutInstance aboutInstance = restoreAboutInstanceFromString(aboutInstanceStr);
aboutInstances.add(aboutInstance);
}
//Close the cursor
c.close();
return aboutInstances;
}
}

View File

@ -0,0 +1,64 @@
package app.fedilab.fedilabtube.viewmodel;
/* 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>. */
import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import java.util.List;
import app.fedilab.fedilabtube.client.data.InstanceData;
import app.fedilab.fedilabtube.sqlite.Sqlite;
import app.fedilab.fedilabtube.sqlite.StoredInstanceDAO;
public class InfoInstanceVM extends AndroidViewModel {
private MutableLiveData<List<InstanceData.AboutInstance>> aboutInstanceMutableLiveData;
public InfoInstanceVM(@NonNull Application application) {
super(application);
}
public LiveData<List<InstanceData.AboutInstance>> getInstances() {
aboutInstanceMutableLiveData = new MutableLiveData<>();
loadInstances();
return aboutInstanceMutableLiveData;
}
private void loadInstances() {
Context _mContext = getApplication().getApplicationContext();
new Thread(() -> {
try {
SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<InstanceData.AboutInstance> instances = new StoredInstanceDAO(_mContext, db).getAllInstances();
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> aboutInstanceMutableLiveData.setValue(instances);
mainHandler.post(myRunnable);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}

View File

@ -18,6 +18,7 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
@ -30,7 +31,38 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<RelativeLayout
android:id="@+id/no_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/no_action_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:padding="10dp"
android:text="@string/no_instances"
android:textSize="25sp" />
</RelativeLayout>
<!-- Main Loader -->
<RelativeLayout
android:id="@+id/loader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone">
<com.github.ybq.android.spinkit.SpinKitView
style="@style/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:SpinKit_Color="?colorAccent" />
</RelativeLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/action_button"
android:layout_width="wrap_content"
@ -39,6 +71,7 @@
android:layout_alignParentEnd="true"
android:layout_margin="@dimen/fab_margin_button"
android:src="@drawable/ic_baseline_add_24"
android:contentDescription="@string/instances_picker"
android:tint="@android:color/white"
/>
</RelativeLayout>

View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?><!--
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>.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/playlist_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:dividerHorizontal"
android:orientation="vertical"
android:showDividers="end">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<TextView
android:id="@+id/about_instance_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/instance_more"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/about_instance_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/instance_more"
app:layout_constraintTop_toBottomOf="@+id/about_instance_name" />
<ImageButton
android:id="@+id/instance_more"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:contentDescription="@string/display_more"
android:src="@drawable/ic_baseline_more_vert_24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_delete"
android:icon="@drawable/ic_baseline_delete_24"
android:title="@string/delete"
app:showAsAction="ifRoom" />
</menu>

View File

@ -9,11 +9,11 @@
android:title="@string/search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always|collapseActionView" />
<item
android:id="@+id/action_change_instance"
android:icon="@drawable/ic_baseline_track_changes_24"
android:title="@string/change_instance"
android:visible="false"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_playlist"