/* Copyright 2018 Thomas Schneider * * This file is a part of Mastalab * * 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. * * Mastalab 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 Mastalab; if not, * see . */ package fr.gouv.etalab.mastodon.activities; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.widget.Toolbar; 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.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import es.dmoral.toasty.Toasty; import fr.gouv.etalab.mastodon.R; import fr.gouv.etalab.mastodon.asynctasks.RetrieveRelationshipAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrieveRemoteDataAsyncTask; import fr.gouv.etalab.mastodon.client.Entities.Account; import fr.gouv.etalab.mastodon.client.Entities.Error; import fr.gouv.etalab.mastodon.client.Entities.Relationship; import fr.gouv.etalab.mastodon.client.Entities.Results; import fr.gouv.etalab.mastodon.drawers.AccountSearchDevAdapter; import fr.gouv.etalab.mastodon.helper.ExpandableHeightListView; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRelationshipInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRemoteAccountInterface; import static fr.gouv.etalab.mastodon.helper.Helper.THEME_LIGHT; /** * Created by Thomas on 31/08/2018. * About activity */ public class PartnerShipActivity extends BaseActivity implements OnRetrieveRemoteAccountInterface, OnRetrieveRelationshipInterface { private List mastohostAcct = new ArrayList<>(); private AccountSearchDevAdapter mastohostAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); switch (theme){ case Helper.THEME_LIGHT: setTheme(R.style.AppTheme); break; case Helper.THEME_DARK: setTheme(R.style.AppThemeDark); 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(Context.LAYOUT_INFLATER_SERVICE); assert inflater != null; @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.simple_bar, null); 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(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); toolbar_title.setText(R.string.action_partnership); if (theme == THEME_LIGHT){ Toolbar toolbar = actionBar.getCustomView().findViewById(R.id.toolbar); Helper.colorizeToolbar(toolbar, R.color.black, PartnerShipActivity.this); } } 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(new View.OnClickListener() { @Override public void onClick(View 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(PartnerShipActivity.this, mastohostAcct); lv_mastohost.setAdapter(mastohostAdapter); new RetrieveRemoteDataAsyncTask(getApplicationContext(), "mastohost", "mastodon.social", PartnerShipActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; default: return super.onOptionsItemSelected(item); } } @Override public void onRetrieveRemoteAccount(Results results) { SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); if( results == null){ Toasty.error(getApplicationContext(), getString(R.string.toast_error),Toast.LENGTH_LONG).show(); return; } List accounts = results.getAccounts(); Account account; if( accounts != null && accounts.size() > 0){ account = accounts.get(0); account.setFollowing(true); switch (account.getUsername()) { case "mastohost": mastohostAcct.add(account); mastohostAdapter.notifyDataSetChanged(); break; } new RetrieveRelationshipAsyncTask(getApplicationContext(), account.getId(),PartnerShipActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } } @Override public void onResume(){ super.onResume(); if( mastohostAcct != null){ for(Account account: mastohostAcct){ new RetrieveRelationshipAsyncTask(getApplicationContext(), account.getId(),PartnerShipActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } } } @Override public void onRetrieveRelationship(Relationship relationship, Error error) { SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.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; } } } }