Improving item activity code and toolbar status when no header image is available

This commit is contained in:
Shinokuni 2019-02-09 12:34:44 +00:00
parent b7ac444be0
commit 017ce251fa
3 changed files with 31 additions and 14 deletions

View File

@ -10,6 +10,7 @@ import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.util.Log; import android.util.Log;
import android.view.Gravity;
import android.view.View; import android.view.View;
import android.webkit.WebSettings; import android.webkit.WebSettings;
import android.webkit.WebView; import android.webkit.WebView;
@ -41,8 +42,12 @@ public class ItemActivity extends AppCompatActivity {
private RelativeLayout readTimeLayout; private RelativeLayout readTimeLayout;
private CollapsingToolbarLayout toolbarLayout; private CollapsingToolbarLayout toolbarLayout;
private Toolbar toolbar;
private ReadropsWebView webView; private ReadropsWebView webView;
public static final String ITEM_ID = "itemId";
public static final String IMAGE_URL = "imageUrl";
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -50,10 +55,10 @@ public class ItemActivity extends AppCompatActivity {
setContentView(R.layout.activity_item); setContentView(R.layout.activity_item);
Intent intent = getIntent(); Intent intent = getIntent();
int itemId = intent.getIntExtra("itemId", 0); int itemId = intent.getIntExtra(ITEM_ID, 0);
String imageUrl = intent.getStringExtra("imageUrl"); String imageUrl = intent.getStringExtra(IMAGE_URL);
Toolbar toolbar = findViewById(R.id.collasping_layout_toolbar); toolbar = findViewById(R.id.collasping_layout_toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
if (getSupportActionBar() != null) if (getSupportActionBar() != null)
@ -61,21 +66,29 @@ public class ItemActivity extends AppCompatActivity {
toolbarLayout = findViewById(R.id.collapsing_layout); toolbarLayout = findViewById(R.id.collapsing_layout);
AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout); AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout);
if (imageUrl == null)
appBarLayout.setExpanded(false);
ImageView imageView = findViewById(R.id.collapsing_layout_image); ImageView imageView = findViewById(R.id.collapsing_layout_image);
GlideApp.with(this)
.load(imageUrl)
.into(imageView);
webView = findViewById(R.id.item_webview); webView = findViewById(R.id.item_webview);
title = findViewById(R.id.activity_item_title); title = findViewById(R.id.activity_item_title);
author = findViewById(R.id.activity_item_author); author = findViewById(R.id.activity_item_author);
readTime = findViewById(R.id.activity_item_readtime); readTime = findViewById(R.id.activity_item_readtime);
readTimeLayout = findViewById(R.id.activity_item_readtime_layout); readTimeLayout = findViewById(R.id.activity_item_readtime_layout);
if (imageUrl == null) {
appBarLayout.setExpanded(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbarLayout.setTitleEnabled(false);
toolbar.setTitleTextColor(Color.WHITE);
} else {
appBarLayout.setExpanded(true);
toolbarLayout.setTitleEnabled(true);
GlideApp.with(this)
.load(imageUrl)
.into(imageView);
}
viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication()).create(ItemViewModel.class); viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication()).create(ItemViewModel.class);
viewModel.getItemById(itemId).observe(this, this::bindUI); viewModel.getItemById(itemId).observe(this, this::bindUI);
} }
@ -83,7 +96,11 @@ public class ItemActivity extends AppCompatActivity {
private void bindUI(ItemWithFeed itemWithFeed) { private void bindUI(ItemWithFeed itemWithFeed) {
Item item = itemWithFeed.getItem(); Item item = itemWithFeed.getItem();
toolbarLayout.setTitle(itemWithFeed.getFeedName()); if (item.getImageLink() == null)
toolbar.setTitle(itemWithFeed.getFeedName());
else
toolbarLayout.setTitle(itemWithFeed.getFeedName());
title.setText(item.getTitle()); title.setText(item.getTitle());
if (item.getAuthor() != null) { if (item.getAuthor() != null) {

View File

@ -127,8 +127,8 @@ public class MainActivity extends AppCompatActivity implements SimpleCallback, S
adapter = new MainItemListAdapter(GlideApp.with(this), preloadSizeProvider); adapter = new MainItemListAdapter(GlideApp.with(this), preloadSizeProvider);
adapter.setOnItemClickListener(itemWithFeed -> { adapter.setOnItemClickListener(itemWithFeed -> {
Intent intent = new Intent(this, ItemActivity.class); Intent intent = new Intent(this, ItemActivity.class);
intent.putExtra("itemId", itemWithFeed.getItem().getId()); intent.putExtra(ItemActivity.ITEM_ID, itemWithFeed.getItem().getId());
intent.putExtra("imageUrl", itemWithFeed.getItem().getImageLink()); intent.putExtra(ItemActivity.IMAGE_URL, itemWithFeed.getItem().getImageLink());
startActivity(intent); startActivity(intent);
}); });

View File

@ -32,6 +32,6 @@ public interface ItemDao {
@Insert @Insert
void insertAll(List<Item> items); void insertAll(List<Item> items);
@Query("Select title, Item.description, content, pub_date, author, 0 as color, read_time, name from Item Inner Join Feed on Item.feed_id = Feed.id And Item.id = :id") @Query("Select title, Item.description, content, pub_date, image_link, author, 0 as color, read_time, name from Item Inner Join Feed on Item.feed_id = Feed.id And Item.id = :id")
LiveData<ItemWithFeed> getItemById(int id); LiveData<ItemWithFeed> getItemById(int id);
} }