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

176 lines
7.0 KiB
Java

/* Copyright 2018 Thomas Schneider
*
* This file is a part of Fedilab
*
* 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.
*
* Fedilab 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 Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
package app.fedilab.android.activities;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.ActionBar;
import androidx.core.content.ContextCompat;
import java.util.ArrayList;
import java.util.List;
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.RetrieveRelationshipAsyncTask;
import app.fedilab.android.asynctasks.RetrieveRemoteDataAsyncTask;
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;
import es.dmoral.toasty.Toasty;
/**
* Created by Thomas on 31/08/2018.
* About activity
*/
public class PartnerShipActivity extends BaseActivity implements OnRetrieveRemoteAccountInterface, OnRetrieveRelationshipInterface {
private final List<Account> mastohostAcct = new ArrayList<>();
private AccountSearchDevAdapter mastohostAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
switch (theme) {
case Helper.THEME_LIGHT:
setTheme(R.style.AppTheme_Fedilab);
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack);
break;
default:
setTheme(R.style.AppThemeDark);
}
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_partnership);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
assert inflater != null;
View view = inflater.inflate(R.layout.simple_bar, new LinearLayout(PartnerShipActivity.this), false);
view.setBackground(new ColorDrawable(ContextCompat.getColor(PartnerShipActivity.this, R.color.cyanea_primary)));
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);
toolbar_close.setOnClickListener(v -> finish());
toolbar_title.setText(R.string.action_partnership);
}
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);
mastohost.setOnClickListener(v -> {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://masto.host"));
startActivity(browserIntent);
});
setTitle(R.string.action_partnership);
lv_mastohost.setExpanded(true);
mastohostAdapter = new AccountSearchDevAdapter(mastohostAcct);
lv_mastohost.setAdapter(mastohostAdapter);
new RetrieveRemoteDataAsyncTask(PartnerShipActivity.this, "mastohost", "mastodon.social", PartnerShipActivity.this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onRetrieveRemoteAccount(Results results, boolean devAccount) {
if (results == null) {
Toasty.error(PartnerShipActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
return;
}
List<Account> accounts = results.getAccounts();
Account account;
if (accounts != null && accounts.size() > 0) {
account = accounts.get(0);
account.setFollowing(true);
if ("mastohost".equals(account.getUsername())) {
mastohostAcct.add(account);
mastohostAdapter.notifyDataSetChanged();
}
new RetrieveRelationshipAsyncTask(PartnerShipActivity.this, account.getId(), PartnerShipActivity.this);
}
}
@Override
public void onResume() {
super.onResume();
if (mastohostAcct != null) {
for (Account account : mastohostAcct) {
new RetrieveRelationshipAsyncTask(PartnerShipActivity.this, account.getId(), PartnerShipActivity.this);
}
}
}
@Override
public void onRetrieveRelationship(Relationship relationship, Error error) {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, "");
if (error != null) {
return;
}
for (int i = 0; i < mastohostAcct.size(); i++) {
if (mastohostAcct.get(i).getId() != null && mastohostAcct.get(i).getId().equals(relationship.getId())) {
mastohostAcct.get(i).setFollowing(relationship.isFollowing() || userId.trim().equals(relationship.getId()));
mastohostAdapter.notifyDataSetChanged();
break;
}
}
}
}