-
Notifications
You must be signed in to change notification settings - Fork 40
/
MainActivity.java
142 lines (123 loc) · 5.54 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* Copyright 2016 Esri
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*
* A copy of the license is available in the repository's
* https://github.com/Esri/arcgis-runtime-demos-android/blob/master/license.txt
*
* For information about licensing your deployed app, see
* https://developers.arcgis.com/android/guide/license-your-app.htm
*
*/
package com.esri.runtime.android.materialbasemaps.ui;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import com.esri.runtime.android.materialbasemaps.R;
import com.esri.runtime.android.materialbasemaps.model.BasemapAdapter;
import com.esri.runtime.android.materialbasemaps.model.BasemapClickListener;
import com.esri.runtime.android.materialbasemaps.model.BasemapItem;
import com.esri.runtime.android.materialbasemaps.model.PersistBasemapItem;
import com.esri.runtime.android.materialbasemaps.presenter.FetchBasemapsItemId;
import com.esri.runtime.android.materialbasemaps.presenter.OnTaskCompleted;
import com.esri.runtime.android.materialbasemaps.util.TaskExecutor;
public class MainActivity extends Activity{
private RecyclerView mRecyclerView;
private ProgressBar mProgressBar;
private BasemapAdapter mBasemapAdapter;
private ArrayList<BasemapItem> mBasemapList;
private String portalUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// inject our progress bar and recycler view
// ButterKnife.bind(this);
mRecyclerView = (RecyclerView) findViewById(R.id.list);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
portalUrl = getResources().getString(R.string.portal_url);
// array of basemap items to have available to load as basemaps
mBasemapList = new ArrayList<>();
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setHasFixedSize(true);
// create an instance of adapter
mBasemapAdapter = new BasemapAdapter(mBasemapList);
// click listener to send portal id to MapActivity
mBasemapAdapter.setOnBaseMapClickListener(new BasemapClickListener() {
@Override
public void onImageClick(int position, String itemId, String title) {
Context context = getApplicationContext();
sendBasemapItemInfo(context, itemId, title);
}
});
mRecyclerView.setAdapter(mBasemapAdapter);
// If basemap item is persisted do not go out to service to fetch them again
if(PersistBasemapItem.getInstance().storage.get("basemap-items") != null){
// populate basemapItems with persisted BasemapItems
ArrayList<BasemapItem> basemapItems = PersistBasemapItem.getInstance().storage.get("basemap-items");
mBasemapList.clear();
mBasemapList.addAll(basemapItems);
mBasemapAdapter.notifyDataSetChanged();
}else {
// turn on progress bar while searching basemaps
mProgressBar.setVisibility(View.VISIBLE);
// search and collect basemap portal ids on background thread
fetchBasemaps();
}
}
/**
* Retrieve basemaps portal item id to send to MapActivity
*/
private void fetchBasemaps(){
TaskExecutor.getInstance().getThreadPool().submit(new FetchBasemapsItemId(this, portalUrl, new OnTaskCompleted() {
@Override
public void processResults(ArrayList<BasemapItem> basemapItems) {
mProgressBar.setVisibility(View.INVISIBLE);
mBasemapList.clear();
mBasemapList.addAll(basemapItems);
mBasemapAdapter.notifyDataSetChanged();
PersistBasemapItem.getInstance().storage.put("basemap-items", basemapItems);
}
}));
}
/**
* Intent to send to MapActivity
*
* @param context application context
* @param portalId portal id representing the basemap to open
* @param title basemap title
*/
private void sendBasemapItemInfo(Context context, String portalId, String title){
Intent intent = new Intent(context, MapActivity.class);
intent.putExtra("portalUrl", portalUrl);
intent.putExtra("portalId", portalId);
intent.putExtra("title", title);
// create activity animation
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this, mRecyclerView, "title_transition");
startActivity(intent, options.toBundle());
}
@Override
protected void onRestart() {
super.onRestart();
if(mBasemapList.size() == 0){
fetchBasemaps();
}
}
}