133 lines
4.7 KiB
Java
133 lines
4.7 KiB
Java
|
package com.keylesspalace.tusky;
|
||
|
|
||
|
import android.content.Context;
|
||
|
import android.content.Intent;
|
||
|
import android.content.SharedPreferences;
|
||
|
import android.graphics.drawable.Drawable;
|
||
|
import android.support.v4.content.ContextCompat;
|
||
|
import android.support.v4.widget.SwipeRefreshLayout;
|
||
|
import android.support.v7.app.AppCompatActivity;
|
||
|
import android.os.Bundle;
|
||
|
import android.support.v7.widget.DividerItemDecoration;
|
||
|
import android.support.v7.widget.LinearLayoutManager;
|
||
|
import android.support.v7.widget.RecyclerView;
|
||
|
import android.support.v7.widget.Toolbar;
|
||
|
import android.view.Menu;
|
||
|
import android.view.MenuItem;
|
||
|
import android.widget.Toast;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.util.List;
|
||
|
|
||
|
public class MainActivity extends AppCompatActivity implements FetchTimelineListener,
|
||
|
SwipeRefreshLayout.OnRefreshListener {
|
||
|
|
||
|
private String domain = null;
|
||
|
private String accessToken = null;
|
||
|
private SwipeRefreshLayout swipeRefreshLayout;
|
||
|
private RecyclerView recyclerView;
|
||
|
private TimelineAdapter adapter;
|
||
|
private LinearLayoutManager layoutManager;
|
||
|
private EndlessOnScrollListener scrollListener;
|
||
|
|
||
|
@Override
|
||
|
protected void onCreate(Bundle savedInstanceState) {
|
||
|
super.onCreate(savedInstanceState);
|
||
|
setContentView(R.layout.activity_main);
|
||
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||
|
setSupportActionBar(toolbar);
|
||
|
|
||
|
SharedPreferences preferences = getSharedPreferences(
|
||
|
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
||
|
domain = preferences.getString("domain", null);
|
||
|
accessToken = preferences.getString("accessToken", null);
|
||
|
assert(domain != null);
|
||
|
assert(accessToken != null);
|
||
|
|
||
|
// Setup the SwipeRefreshLayout.
|
||
|
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
|
||
|
swipeRefreshLayout.setOnRefreshListener(this);
|
||
|
// Setup the RecyclerView.
|
||
|
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
|
||
|
recyclerView.setHasFixedSize(true);
|
||
|
layoutManager = new LinearLayoutManager(this);
|
||
|
recyclerView.setLayoutManager(layoutManager);
|
||
|
DividerItemDecoration divider = new DividerItemDecoration(
|
||
|
this, layoutManager.getOrientation());
|
||
|
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.status_divider);
|
||
|
divider.setDrawable(drawable);
|
||
|
recyclerView.addItemDecoration(divider);
|
||
|
scrollListener = new EndlessOnScrollListener(layoutManager) {
|
||
|
@Override
|
||
|
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
|
||
|
TimelineAdapter adapter = (TimelineAdapter) view.getAdapter();
|
||
|
String fromId = adapter.getItem(adapter.getItemCount() - 1).getId();
|
||
|
sendFetchTimelineRequest(fromId);
|
||
|
}
|
||
|
};
|
||
|
recyclerView.addOnScrollListener(scrollListener);
|
||
|
adapter = new TimelineAdapter();
|
||
|
recyclerView.setAdapter(adapter);
|
||
|
|
||
|
sendFetchTimelineRequest();
|
||
|
}
|
||
|
|
||
|
private void sendFetchTimelineRequest(String fromId) {
|
||
|
new FetchTimelineTask(this, this, domain, accessToken, fromId).execute();
|
||
|
}
|
||
|
|
||
|
private void sendFetchTimelineRequest() {
|
||
|
sendFetchTimelineRequest(null);
|
||
|
}
|
||
|
|
||
|
public void onFetchTimelineSuccess(List<Status> statuses, boolean added) {
|
||
|
if (added) {
|
||
|
adapter.addItems(statuses);
|
||
|
} else {
|
||
|
adapter.update(statuses);
|
||
|
}
|
||
|
swipeRefreshLayout.setRefreshing(false);
|
||
|
}
|
||
|
|
||
|
public void onFetchTimelineFailure(IOException exception) {
|
||
|
Toast.makeText(this, R.string.error_fetching_timeline, Toast.LENGTH_SHORT).show();
|
||
|
swipeRefreshLayout.setRefreshing(false);
|
||
|
}
|
||
|
|
||
|
public void onRefresh() {
|
||
|
sendFetchTimelineRequest();
|
||
|
}
|
||
|
|
||
|
|
||
|
private void logOut() {
|
||
|
SharedPreferences preferences = getSharedPreferences(
|
||
|
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
||
|
SharedPreferences.Editor editor = preferences.edit();
|
||
|
editor.remove("domain");
|
||
|
editor.remove("accessToken");
|
||
|
editor.apply();
|
||
|
Intent intent = new Intent(this, SplashActivity.class);
|
||
|
startActivity(intent);
|
||
|
finish();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||
|
getMenuInflater().inflate(R.menu.main_toolbar, menu);
|
||
|
return super.onCreateOptionsMenu(menu);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||
|
switch (item.getItemId()) {
|
||
|
case R.id.action_logout: {
|
||
|
logOut();
|
||
|
return true;
|
||
|
}
|
||
|
default: {
|
||
|
return super.onOptionsItemSelected(item);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|