Advertisement
Guest User

Class MainActivity

a guest
Feb 23rd, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.77 KB | None | 0 0
  1. package com.totrail.totrail;
  2.  
  3. import android.app.Dialog;
  4. import android.app.ProgressDialog;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10.  
  11. import android.app.ProgressDialog;
  12. import android.content.DialogInterface;
  13. import android.content.DialogInterface.OnClickListener;
  14.  
  15. import androidx.appcompat.widget.Toolbar;
  16. import androidx.core.view.GravityCompat;
  17. import androidx.appcompat.app.ActionBarDrawerToggle;
  18.  
  19. import android.util.Log;
  20. import android.view.MenuItem;
  21.  
  22. import com.google.android.material.navigation.NavigationView;
  23. import com.google.gson.Gson;
  24. import com.totrail.totrail.Adapter.FeedAdapter;
  25. import com.totrail.totrail.Common.HTTPDataHandler;
  26. import com.totrail.totrail.Model.RSSObject;
  27.  
  28. import androidx.appcompat.app.AppCompatActivity;
  29. import androidx.drawerlayout.widget.DrawerLayout;
  30. import androidx.recyclerview.widget.LinearLayoutManager;
  31. import androidx.recyclerview.widget.RecyclerView;
  32.  
  33. import android.view.Menu;
  34.  
  35. public class MainActivity extends AppCompatActivity
  36.         implements NavigationView.OnNavigationItemSelectedListener {
  37.  
  38.     RecyclerView recyclerView;
  39.     RSSObject rssObject;
  40.  
  41.     private final String RSS_link="http://api.totrail.org:54961/feed";
  42.     private final String RSS_to_Json_API = "https://api.rss2json.com/v1/api.json?rss_url=";
  43.     public static final String APP_PREFERENCES = "voda_zahlebnulas-kolobok_povesilsa";
  44.     public static final String APP_PREFERENCES_TOKEN = "token"; // User's API token
  45.     public static final String APP_PREFERENCES_STARTED = "nonononono";
  46.     public String token = "waiting...";
  47.     SharedPreferences mSettings;
  48.     @Override
  49.     protected void onCreate(Bundle savedInstanceState) {
  50.         super.onCreate(savedInstanceState);
  51.         setContentView(R.layout.activity_main);
  52.         Toolbar toolbar = findViewById(R.id.toolbar);
  53.         toolbar.setTitle("Маршруты");
  54.         setSupportActionBar(toolbar);
  55.  
  56.         recyclerView = findViewById(R.id.recyclerView);
  57.         LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getBaseContext(),LinearLayoutManager.VERTICAL,false);
  58.         recyclerView.setLayoutManager(linearLayoutManager);
  59.  
  60.         loadRSS();
  61.         setSupportActionBar(toolbar);
  62. //        mAdView = findViewById(R.id.adView);
  63.         DrawerLayout drawer = findViewById(R.id.drawer_layout);
  64. //        NavigationView navigationView = findViewById(R.id.nav_view);
  65.         ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  66.                 this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  67.         drawer.addDrawerListener(toggle);
  68.         toggle.syncState();
  69. //        navigationView.setNavigationItemSelectedListener(this);
  70.         mSettings = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
  71.         mSettings.contains(APP_PREFERENCES_STARTED);// TODO: make very cool welcome screen
  72. // TODO: and here is too! :-)
  73.         if(mSettings.contains(APP_PREFERENCES_TOKEN)) {
  74.             token = mSettings.getString(APP_PREFERENCES_TOKEN, "");
  75.         } else {
  76.             token = "not_login_in";
  77.         }
  78.  
  79.     }
  80.  
  81.     private void loadRSS() {
  82.         AsyncTask<String, String, String> loadRSSAsync = new AsyncTask<String, String, String>() {
  83.  
  84.             ProgressDialog mDialog = new ProgressDialog(MainActivity.this);
  85.  
  86.             @Override
  87.             protected void onPreExecute() {
  88.                 mDialog.setMessage("Ждите...");
  89.                 mDialog.show();
  90.             }
  91.  
  92.             @Override
  93.             protected String doInBackground(String... params) {
  94.                 String result;
  95.                 HTTPDataHandler http = new HTTPDataHandler();
  96.                 result = http.GetHTTPData(params[0]);
  97.                 return result;
  98.             }
  99.  
  100.             @Override
  101.             protected void onPostExecute(String s) {
  102.                 mDialog.dismiss();
  103.                 Log.i("KEK", s == null ? "null" : s);
  104.                 rssObject = new Gson().fromJson(s, RSSObject.class);
  105.                 FeedAdapter adapter = new FeedAdapter(rssObject, getBaseContext());
  106.                 recyclerView.setAdapter(adapter);
  107.                 adapter.notifyDataSetChanged();
  108.             }
  109.         };
  110.         StringBuilder url_get_data = new StringBuilder(RSS_to_Json_API);
  111.         url_get_data.append(RSS_link);
  112.         loadRSSAsync.execute(url_get_data.toString());
  113.     }
  114.     @Override
  115.     public boolean onCreateOptionsMenu(Menu menu) {
  116.         getMenuInflater().inflate(R.menu.main_menu,menu);
  117.         return true;
  118.     }
  119.  
  120. //    @Override
  121. //    protected void onStop() {
  122. //        super.onStop();
  123. //        Dialog progressDialog = null;
  124. //        if ((progressDialog != null) && progressDialog.isShowing()) {
  125. //            progressDialog.dismiss();
  126. //            progressDialog = null;
  127. //        }
  128. //    }
  129.  
  130.     @Override
  131.     public boolean onOptionsItemSelected(MenuItem item) {
  132.         if (item.getItemId() == R.id.menu_refresh)
  133.             loadRSS();
  134.         return true;
  135.  
  136.     }
  137.     @Override
  138.     public boolean onNavigationItemSelected(MenuItem item) {
  139.         // Handle navigation view item clicks here.
  140.         int id = item.getItemId();
  141.  
  142.         if (id == R.id.nav_home) {
  143.             Intent intent = new Intent(this, loginActivity.class);
  144.             startActivity(intent);
  145.         } else if (id == R.id.nav_gallery) {
  146.  
  147.         } else if (id == R.id.nav_slideshow) {
  148.  
  149.         } else if (id == R.id.nav_tools) {
  150.  
  151.         } else if (id == R.id.nav_share) {
  152.  
  153.         } else if (id == R.id.nav_send) {
  154.  
  155.         }
  156.  
  157.         DrawerLayout drawer = findViewById(R.id.drawer_layout);
  158.         drawer.closeDrawer(GravityCompat.START);
  159.         return true;
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement