fedilab-Android-App/app/src/main/java/app/fedilab/android/activities/PartnerShipActivity.java

176 lines
7.0 KiB
Java
Raw Normal View History

2018-09-01 08:13:22 +02:00
/* Copyright 2018 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2018-09-01 08:13:22 +02:00
*
* 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.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2018-09-01 08:13:22 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2018-09-01 08:13:22 +02:00
* see <http://www.gnu.org/licenses>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.activities;
2018-09-01 08:13:22 +02:00
import android.content.Intent;
import android.content.SharedPreferences;
2019-11-13 12:54:01 +01:00
import android.graphics.drawable.ColorDrawable;
2018-09-01 08:13:22 +02:00
import android.net.Uri;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
2018-11-03 12:06:44 +01:00
import android.view.LayoutInflater;
2018-09-01 08:13:22 +02:00
import android.view.MenuItem;
import android.view.View;
2018-11-03 12:06:44 +01:00
import android.view.ViewGroup;
2018-09-01 08:13:22 +02:00
import android.widget.ImageView;
2019-08-18 17:41:10 +02:00
import android.widget.LinearLayout;
2018-09-01 08:13:22 +02:00
import android.widget.TextView;
import android.widget.Toast;
2019-11-15 16:32:25 +01:00
import androidx.appcompat.app.ActionBar;
import androidx.core.content.ContextCompat;
2018-09-01 08:13:22 +02:00
import java.util.ArrayList;
import java.util.List;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.RetrieveRelationshipAsyncTask;
import app.fedilab.android.asynctasks.RetrieveRemoteDataAsyncTask;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.Entities.Error;
import app.fedilab.android.client.Entities.Relationship;
import app.fedilab.android.client.Entities.Results;
import app.fedilab.android.drawers.AccountSearchDevAdapter;
import app.fedilab.android.helper.ExpandableHeightListView;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.interfaces.OnRetrieveRelationshipInterface;
import app.fedilab.android.interfaces.OnRetrieveRemoteAccountInterface;
2019-11-15 16:32:25 +01:00
import es.dmoral.toasty.Toasty;
2018-11-03 12:06:44 +01:00
2018-09-01 08:13:22 +02:00
/**
* Created by Thomas on 31/08/2018.
* About activity
*/
public class PartnerShipActivity extends BaseActivity implements OnRetrieveRemoteAccountInterface, OnRetrieveRelationshipInterface {
2021-01-10 18:41:50 +01:00
private final List<Account> mastohostAcct = new ArrayList<>();
2018-09-01 08:13:22 +02:00
private AccountSearchDevAdapter mastohostAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2019-05-18 11:10:30 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
2018-09-01 08:13:22 +02:00
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-09-06 17:55:14 +02:00
switch (theme) {
2018-09-01 08:13:22 +02:00
case Helper.THEME_LIGHT:
2019-11-09 15:47:38 +01:00
setTheme(R.style.AppTheme_Fedilab);
2018-09-01 08:13:22 +02:00
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack);
break;
default:
setTheme(R.style.AppThemeDark);
}
2019-09-06 17:55:14 +02:00
if (getSupportActionBar() != null)
2018-09-01 08:13:22 +02:00
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_partnership);
2018-11-03 12:06:44 +01:00
ActionBar actionBar = getSupportActionBar();
2019-09-06 17:55:14 +02:00
if (actionBar != null) {
2019-05-18 11:10:30 +02:00
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
2018-11-03 12:06:44 +01:00
assert inflater != null;
2020-04-08 12:42:15 +02:00
View view = inflater.inflate(R.layout.simple_bar, new LinearLayout(PartnerShipActivity.this), false);
2019-11-15 19:36:39 +01:00
view.setBackground(new ColorDrawable(ContextCompat.getColor(PartnerShipActivity.this, R.color.cyanea_primary)));
2018-11-03 12:06:44 +01:00
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView toolbar_close = actionBar.getCustomView().findViewById(R.id.toolbar_close);
TextView toolbar_title = actionBar.getCustomView().findViewById(R.id.toolbar_title);
2020-03-08 09:15:12 +01:00
toolbar_close.setOnClickListener(v -> finish());
2018-11-03 12:06:44 +01:00
toolbar_title.setText(R.string.action_partnership);
}
2018-09-01 08:13:22 +02:00
TextView about_partnership = findViewById(R.id.about_partnership);
about_partnership.setMovementMethod(LinkMovementMethod.getInstance());
ExpandableHeightListView lv_mastohost = findViewById(R.id.lv_mastohost);
ImageView mastohost = findViewById(R.id.mastohost_logo);
2020-03-08 09:15:12 +01:00
mastohost.setOnClickListener(v -> {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://masto.host"));
startActivity(browserIntent);
2018-09-01 08:13:22 +02:00
});
setTitle(R.string.action_partnership);
lv_mastohost.setExpanded(true);
2019-08-19 09:11:23 +02:00
mastohostAdapter = new AccountSearchDevAdapter(mastohostAcct);
2018-09-01 08:13:22 +02:00
lv_mastohost.setAdapter(mastohostAdapter);
2021-01-19 17:43:51 +01:00
new RetrieveRemoteDataAsyncTask(PartnerShipActivity.this, "mastohost", "mastodon.social", PartnerShipActivity.this);
2019-09-06 17:55:14 +02:00
}
2018-09-01 08:13:22 +02:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
2020-03-08 09:15:12 +01:00
if (item.getItemId() == android.R.id.home) {
finish();
return true;
2018-09-01 08:13:22 +02:00
}
2020-03-08 09:15:12 +01:00
return super.onOptionsItemSelected(item);
2018-09-01 08:13:22 +02:00
}
@Override
2019-11-22 19:10:20 +01:00
public void onRetrieveRemoteAccount(Results results, boolean devAccount) {
2019-09-06 17:55:14 +02:00
if (results == null) {
2020-04-08 12:42:15 +02:00
Toasty.error(PartnerShipActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2018-09-01 08:13:22 +02:00
return;
}
List<Account> accounts = results.getAccounts();
Account account;
2019-09-06 17:55:14 +02:00
if (accounts != null && accounts.size() > 0) {
2018-09-01 08:13:22 +02:00
account = accounts.get(0);
account.setFollowing(true);
2020-03-08 09:15:12 +01:00
if ("mastohost".equals(account.getUsername())) {
mastohostAcct.add(account);
mastohostAdapter.notifyDataSetChanged();
2018-09-01 08:13:22 +02:00
}
2021-01-19 17:43:51 +01:00
new RetrieveRelationshipAsyncTask(PartnerShipActivity.this, account.getId(), PartnerShipActivity.this);
2018-09-01 08:13:22 +02:00
}
}
@Override
2019-09-06 17:55:14 +02:00
public void onResume() {
2018-09-01 08:13:22 +02:00
super.onResume();
2019-09-06 17:55:14 +02:00
if (mastohostAcct != null) {
for (Account account : mastohostAcct) {
2021-01-19 17:43:51 +01:00
new RetrieveRelationshipAsyncTask(PartnerShipActivity.this, account.getId(), PartnerShipActivity.this);
2018-09-01 08:13:22 +02:00
}
}
}
@Override
public void onRetrieveRelationship(Relationship relationship, Error error) {
2019-05-18 11:10:30 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
2018-09-01 08:13:22 +02:00
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, "");
2019-09-06 17:55:14 +02:00
if (error != null) {
2018-09-01 08:13:22 +02:00
return;
}
2019-09-06 17:55:14 +02:00
for (int i = 0; i < mastohostAcct.size(); i++) {
if (mastohostAcct.get(i).getId() != null && mastohostAcct.get(i).getId().equals(relationship.getId())) {
2018-09-01 08:13:22 +02:00
mastohostAcct.get(i).setFollowing(relationship.isFollowing() || userId.trim().equals(relationship.getId()));
mastohostAdapter.notifyDataSetChanged();
break;
}
}
}
}