Open dialog

This commit is contained in:
Thomas 2020-09-16 17:27:07 +02:00
parent 106bacb4b1
commit 33c7f12180
12 changed files with 522 additions and 1 deletions

View File

@ -220,4 +220,11 @@
<string name="Donate">Faire un don</string>
<string name="source_code">Code source</string>
<string name="issue_tracker">Suivi des tickets</string>
<string name="action_instance_empty_content">No instances match these criteria</string>
<string name="instances_picker">Instances picker</string>
<string name="pickup_instance">Pickup this instance</string>
<string name="sensitive_content">Sensitive content: %1$s</string>
<string name="followers_instance">%1$s followers instances</string>
<string name="help">Help</string>
</resources>

View File

@ -238,4 +238,10 @@
<string name="Donate">Donate</string>
<string name="source_code">Source code</string>
<string name="issue_tracker">Issue tracker</string>
<string name="action_instance_empty_content">No instances match these criteria</string>
<string name="instances_picker">Instances picker</string>
<string name="pickup_instance">Pickup this instance</string>
<string name="sensitive_content">Sensitive content: %1$s</string>
<string name="followers_instance">%1$s followers instances</string>
<string name="help">Help</string>
</resources>

View File

@ -65,6 +65,13 @@
android:configChanges="orientation|screenSize"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysHidden" />
<activity
android:name=".InstancePickerActivity"
android:configChanges="orientation|screenSize"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysHidden" />
<activity
android:name=".PlaylistsActivity"
android:configChanges="orientation|screenSize"

View File

@ -0,0 +1,110 @@
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 <http://www.gnu.org/licenses>. */
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
import app.fedilab.fedilabtube.client.APIResponse;
import app.fedilab.fedilabtube.client.entities.Instance;
import app.fedilab.fedilabtube.client.entities.InstanceParams;
import app.fedilab.fedilabtube.drawer.InstanceAdapter;
import app.fedilab.fedilabtube.viewmodel.InstancesVM;
import es.dmoral.toasty.Toasty;
public class InstancePickerActivity extends AppCompatActivity {
LinearLayoutManager mLayoutManager;
private RelativeLayout mainLoader, textviewNoAction;
private List<Instance> instances;
private InstanceAdapter instanceAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_instance_picker);
instances = new ArrayList<>();
RecyclerView lv_instances = findViewById(R.id.lv_instances);
mainLoader = findViewById(R.id.loader);
textviewNoAction = findViewById(R.id.no_action);
mainLoader.setVisibility(View.VISIBLE);
mainLoader.setVisibility(View.VISIBLE);
instanceAdapter = new InstanceAdapter(this.instances);
lv_instances.setAdapter(instanceAdapter);
mLayoutManager = new LinearLayoutManager(InstancePickerActivity.this);
lv_instances.setLayoutManager(mLayoutManager);
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle(R.string.instances_picker);
InstancesVM viewModel = new ViewModelProvider(InstancePickerActivity.this).get(InstancesVM.class);
viewModel.getInstances(new InstanceParams()).observe(InstancePickerActivity.this, this::manageVIewInstance);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void manageVIewInstance(APIResponse apiResponse) {
mainLoader.setVisibility(View.GONE);
//Discards 404 - error which can often happen due to toots which have been deleted
if (apiResponse.getError() != null) {
if (!apiResponse.getError().getError().startsWith("404 -") && !apiResponse.getError().getError().startsWith("501 -"))
Toasty.error(InstancePickerActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
return;
}
List<Instance> instances = apiResponse.getInstances();
if ((instances == null || instances.size() == 0)) {
textviewNoAction.setVisibility(View.VISIBLE);
return;
}
this.instances.addAll(instances);
instanceAdapter.notifyItemRangeInserted(0, instances.size());
}
}

View File

@ -298,6 +298,10 @@ public class MainActivity extends AppCompatActivity {
}).start());
alt_bld.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
alt_bld.setNeutralButton(R.string.help, (dialog, which) -> {
Intent intent = new Intent(MainActivity.this, InstancePickerActivity.class);
startActivity(intent);
});
AlertDialog alert = alt_bld.create();
alert.show();
}

View File

@ -0,0 +1,121 @@
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.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.client.entities.Instance;
import static app.fedilab.fedilabtube.MainActivity.peertubeInformation;
public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Instance> instances;
private Context context;
public InstanceAdapter(List<Instance> instances) {
this.instances = instances;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater layoutInflater = LayoutInflater.from(context);
return new PeertubeAdapter.ViewHolder(layoutInflater.inflate(R.layout.drawer_instance, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
final Instance instance = instances.get(position);
final ViewHolder holder = (ViewHolder) viewHolder;
holder.description.setText(instance.getShortDescription());
holder.name.setText(instance.getName());
holder.host.setText(instance.getHost());
StringBuilder categories = new StringBuilder();
if (instance.getCategories() != null && instance.getCategories().size() > 0) {
Iterator<Map.Entry<Integer, Integer>> it = instance.getCategories().entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Integer> pair = it.next();
LinkedHashMap<Integer, String> info_cat = peertubeInformation.getCategories();
categories.append(info_cat.get(pair.getKey())).append(" ");
it.remove();
}
}
holder.tags.setText(categories);
StringBuilder languages = new StringBuilder();
if (instance.getLanguages() != null && instance.getLanguages().size() > 0) {
Iterator<Map.Entry<Integer, String>> it = instance.getLanguages().entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> pair = it.next();
languages.append(pair.getKey()).append(" ");
it.remove();
}
}
holder.languages.setText(languages);
holder.sensitive_content.setText(context.getString(R.string.sensitive_content, instance.getDefaultNSFWPolicy()));
holder.followers_instance.setText(context.getString(R.string.followers_instance, String.valueOf(instance.getTotalInstanceFollowers())));
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return instances.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
Button pickup;
TextView name, host, description;
TextView tags, followers_instance, languages, sensitive_content;
public ViewHolder(@NonNull View itemView) {
super(itemView);
pickup = itemView.findViewById(R.id.pickup);
name = itemView.findViewById(R.id.name);
host = itemView.findViewById(R.id.host);
description = itemView.findViewById(R.id.description);
tags = itemView.findViewById(R.id.tags);
languages = itemView.findViewById(R.id.languages);
sensitive_content = itemView.findViewById(R.id.sensitive_content);
}
}
}

View File

@ -0,0 +1,59 @@
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.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import app.fedilab.fedilabtube.client.APIResponse;
import app.fedilab.fedilabtube.client.PeertubeAPI;
import app.fedilab.fedilabtube.client.entities.InstanceParams;
public class InstancesVM extends AndroidViewModel {
private MutableLiveData<APIResponse> apiResponseMutableLiveData;
public InstancesVM(@NonNull Application application) {
super(application);
}
public LiveData<APIResponse> getInstances(InstanceParams instanceParams) {
apiResponseMutableLiveData = new MutableLiveData<>();
get(instanceParams);
return apiResponseMutableLiveData;
}
private void get(InstanceParams instanceParams) {
Context _mContext = getApplication().getApplicationContext();
new Thread(() -> {
try {
APIResponse apiResponse = new PeertubeAPI(_mContext).getInstances(instanceParams);
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse);
mainHandler.post(myRunnable);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M21,6h-2v9L6,15v2c0,0.55 0.45,1 1,1h11l4,4L22,7c0,-0.55 -0.45,-1 -1,-1zM17,12L17,3c0,-0.55 -0.45,-1 -1,-1L3,2c-0.55,0 -1,0.45 -1,1v14l4,-4h10c0.55,0 1,-0.45 1,-1z" />
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M16,11c1.66,0 2.99,-1.34 2.99,-3S17.66,5 16,5c-1.66,0 -3,1.34 -3,3s1.34,3 3,3zM8,11c1.66,0 2.99,-1.34 2.99,-3S9.66,5 8,5C6.34,5 5,6.34 5,8s1.34,3 3,3zM8,13c-2.33,0 -7,1.17 -7,3.5L1,19h14v-2.5c0,-2.33 -4.67,-3.5 -7,-3.5zM16,13c-0.29,0 -0.62,0.02 -0.97,0.05 1.16,0.84 1.97,1.97 1.97,3.45L17,19h6v-2.5c0,-2.33 -4.67,-3.5 -7,-3.5z" />
</vector>

View File

@ -0,0 +1,84 @@
<?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>.
-->
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".InstancePickerActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/fab_margin">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/lv_instances"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" />
<RelativeLayout
android:id="@+id/no_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:padding="10dp"
android:text="@string/action_instance_empty_content"
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">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
</RelativeLayout>
<!-- Loader for next status -->
<RelativeLayout
android:id="@+id/loading_next_status"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom|center_horizontal"
android:gravity="bottom|center_horizontal"
android:visibility="gone">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:indeterminate="true" />
</RelativeLayout>
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.drawerlayout.widget.DrawerLayout>

View File

@ -23,7 +23,7 @@
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.fedilab.android.activities.PlaylistsActivity">
tools:context=".PlaylistsActivity">
<RelativeLayout
android:layout_width="match_parent"

View File

@ -0,0 +1,103 @@
<?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/main_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:divider="?android:dividerHorizontal"
android:gravity="bottom"
android:orientation="vertical"
android:showDividers="end">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/host"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@+id/name"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name" />
<TextView
android:id="@+id/tags"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/description" />
<TextView
android:id="@+id/followers_instance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:drawablePadding="10dp"
app:drawableStartCompat="@drawable/ic_baseline_group_24"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tags" />
<TextView
android:id="@+id/languages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:drawablePadding="10dp"
app:drawableStartCompat="@drawable/ic_baseline_forum_24"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/followers_instance" />
<TextView
android:id="@+id/sensitive_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/languages" />
<Button
android:id="@+id/pickup"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/pickup_instance"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/sensitive_content" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>