Luca448 commited on
Commit
90fdb53
·
1 Parent(s): d1f75f8

UI: Add pull-to-refresh and fix header padding

Browse files
Files changed (1) hide show
  1. src/views/news/news-view.js +104 -49
src/views/news/news-view.js CHANGED
@@ -45,17 +45,7 @@ export class NewsView {
45
 
46
  if (topicsRes.ok) this.topics = await topicsRes.json();
47
 
48
- const sixHours = 6 * 60 * 60 * 1000;
49
- if (!this.lastRun || (Date.now() - this.lastRun) > sixHours) {
50
- console.log('[NewsView] Triggering generation (lastRun > 6h)...');
51
- if (!this.isGenerating) {
52
- // Delay trigger slightly to allow DOM to render buttons first
53
- setTimeout(() => {
54
- const triggerBtn = document.getElementById('trigger-generation-btn-header') || document.getElementById('trigger-generation-btn');
55
- if (triggerBtn) triggerBtn.click();
56
- }, 100);
57
- }
58
- }
59
 
60
  } catch (e) {
61
  console.error('Error fetching news:', e);
@@ -67,7 +57,15 @@ export class NewsView {
67
 
68
  render() {
69
  this.container.innerHTML = `
70
- <div class="news-container fade-in" style="padding-top: calc(env(safe-area-inset-top, 20px) + 20px); min-height: 100%; padding-bottom: 32px;">
 
 
 
 
 
 
 
 
71
  <div style="padding: 0 24px; margin-bottom: 16px; display: flex; justify-content: space-between; align-items: flex-end;">
72
  <div>
73
  <h1 style="font-size: 32px; font-weight: 700; margin: 0; background: linear-gradient(to right, #fff, #a0a0a0); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">Entdecken</h1>
@@ -434,22 +432,15 @@ export class NewsView {
434
  if (this.isGenerating) return;
435
 
436
  this.isGenerating = true;
437
- triggerBtn.textContent = 'Generiere (das kann 10-20 Sekunden dauern)...';
438
  triggerBtn.style.opacity = '0.5';
439
 
440
  await fetch(getApiUrl('/api/news/generate'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ simulateError: false }) });
441
 
442
- // Poll every 5 seconds up to 36 times (3 minutes)
443
- let attempts = 0;
444
- const pollInterval = setInterval(async () => {
445
- attempts++;
446
- await this.fetchData();
447
- if (this.articles.length > 0 || attempts >= 36) {
448
- clearInterval(pollInterval);
449
- this.isGenerating = false;
450
- this.render(); // WICHTIG: UI aktualisieren
451
- }
452
- }, 5000);
453
  });
454
  }
455
 
@@ -459,21 +450,15 @@ export class NewsView {
459
  if (this.isGenerating) return;
460
 
461
  this.isGenerating = true;
462
- simBtn.textContent = 'Generiere... (Simulated Error) - Bitte warten...';
463
  simBtn.style.opacity = '0.5';
464
 
465
  await fetch(getApiUrl('/api/news/generate'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ simulateError: true }) });
466
 
467
- let attempts = 0;
468
- const pollInterval = setInterval(async () => {
469
- attempts++;
470
- await this.fetchData();
471
- if (this.articles.length > 0 || attempts >= 36) { // 3 minutes max
472
- clearInterval(pollInterval);
473
- this.isGenerating = false;
474
- this.render(); // WICHTIG: UI aktualisieren
475
- }
476
- }, 5000);
477
  });
478
  }
479
 
@@ -482,21 +467,15 @@ export class NewsView {
482
  headerTriggerBtn.addEventListener('click', async () => {
483
  if (this.isGenerating) return;
484
  this.isGenerating = true;
485
- headerTriggerBtn.style.transform = 'rotate(180deg)';
486
- headerTriggerBtn.style.opacity = '0.5';
487
 
488
- await fetch(getApiUrl('/api/news/generate'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ simulateError: false }) });
489
 
490
- let attempts = 0;
491
- const pollInterval = setInterval(async () => {
492
- attempts++;
493
- await this.fetchData();
494
- if (attempts >= 36) { // 3 minutes
495
- clearInterval(pollInterval);
496
- this.isGenerating = false;
497
- this.render();
498
- }
499
- }, 5000);
500
  });
501
  }
502
 
@@ -543,9 +522,85 @@ export class NewsView {
543
  }
544
  }
545
  }, {passive: true});
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
546
  }
547
 
548
- // Open Article
549
  document.querySelectorAll('.news-card').forEach(card => {
550
  card.addEventListener('click', (e) => {
551
  const id = e.currentTarget.dataset.id;
 
45
 
46
  if (topicsRes.ok) this.topics = await topicsRes.json();
47
 
48
+ // Auto-trigger on load removed as requested. User will pull-to-refresh.
 
 
 
 
 
 
 
 
 
 
49
 
50
  } catch (e) {
51
  console.error('Error fetching news:', e);
 
57
 
58
  render() {
59
  this.container.innerHTML = `
60
+ <div class="news-container fade-in" style="padding-top: calc(env(safe-area-inset-top, 40px) + 30px); min-height: 100%; padding-bottom: 32px; position: relative;">
61
+
62
+ <!-- Pull to Refresh Indicator -->
63
+ <div id="pull-to-refresh-indicator" style="position: absolute; top: calc(env(safe-area-inset-top, 20px)); left: 0; right: 0; display: flex; justify-content: center; align-items: center; transform: translateY(-40px) scale(0.8); transition: transform 0.2s, opacity 0.2s; opacity: 0; z-index: 100;">
64
+ <div style="background: var(--surface-2); padding: 8px; border-radius: 50%; box-shadow: 0 4px 12px rgba(0,0,0,0.3); border: 1px solid rgba(255,255,255,0.1); display: flex;">
65
+ <div class="spinner" style="width: 20px; height: 20px; border: 2px solid rgba(255,255,255,0.1); border-top-color: #c183f0; border-radius: 50%; animation: spin 1s linear infinite;"></div>
66
+ </div>
67
+ </div>
68
+
69
  <div style="padding: 0 24px; margin-bottom: 16px; display: flex; justify-content: space-between; align-items: flex-end;">
70
  <div>
71
  <h1 style="font-size: 32px; font-weight: 700; margin: 0; background: linear-gradient(to right, #fff, #a0a0a0); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">Entdecken</h1>
 
432
  if (this.isGenerating) return;
433
 
434
  this.isGenerating = true;
435
+ triggerBtn.textContent = 'Lädt neue Artikel... (Ziehe nach unten zum Checken)';
436
  triggerBtn.style.opacity = '0.5';
437
 
438
  await fetch(getApiUrl('/api/news/generate'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ simulateError: false }) });
439
 
440
+ setTimeout(() => {
441
+ this.isGenerating = false;
442
+ this.render();
443
+ }, 3000); // Reset UI after 3 seconds, user can pull to refresh to see new articles
 
 
 
 
 
 
 
444
  });
445
  }
446
 
 
450
  if (this.isGenerating) return;
451
 
452
  this.isGenerating = true;
453
+ simBtn.textContent = 'Simulation läuft...';
454
  simBtn.style.opacity = '0.5';
455
 
456
  await fetch(getApiUrl('/api/news/generate'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ simulateError: true }) });
457
 
458
+ setTimeout(() => {
459
+ this.isGenerating = false;
460
+ this.render();
461
+ }, 3000);
 
 
 
 
 
 
462
  });
463
  }
464
 
 
467
  headerTriggerBtn.addEventListener('click', async () => {
468
  if (this.isGenerating) return;
469
  this.isGenerating = true;
 
 
470
 
471
+ headerTriggerBtn.innerHTML = '<div class="spinner" style="width: 16px; height: 16px; border: 2px solid rgba(255,255,255,0.2); border-top-color: #fff; border-radius: 50%; animation: spin 1s linear infinite;"></div>';
472
 
473
+ await fetch(getApiUrl('/api/news/generate'), { method: 'POST', headers: { 'Content-Type': 'application/json' } });
474
+
475
+ setTimeout(() => {
476
+ this.isGenerating = false;
477
+ this.render();
478
+ }, 2000);
 
 
 
 
479
  });
480
  }
481
 
 
522
  }
523
  }
524
  }, {passive: true});
525
+
526
+ // Setup Pull-to-Refresh
527
+ const viewScrollContainer = this.container.closest('.view-container') || this.container;
528
+ const ptrIndicator = document.getElementById('pull-to-refresh-indicator');
529
+ const newsContent = this.container.querySelector('.news-container');
530
+
531
+ let ptrStartY = 0;
532
+ let ptrCurrentY = 0;
533
+ let isPtrPulling = false;
534
+ let isPtrRefreshing = false;
535
+
536
+ viewScrollContainer.addEventListener('touchstart', (e) => {
537
+ if (viewScrollContainer.scrollTop <= 5 && !isPtrRefreshing) {
538
+ ptrStartY = e.touches[0].clientY;
539
+ isPtrPulling = true;
540
+ newsContent.style.transition = 'none';
541
+ if (ptrIndicator) ptrIndicator.style.transition = 'none';
542
+ } else {
543
+ isPtrPulling = false;
544
+ }
545
+ }, {passive: true});
546
+
547
+ viewScrollContainer.addEventListener('touchmove', (e) => {
548
+ if (!isPtrPulling || isPtrRefreshing) return;
549
+ ptrCurrentY = e.touches[0].clientY;
550
+ const pullDistance = ptrCurrentY - ptrStartY;
551
+
552
+ if (pullDistance > 0 && viewScrollContainer.scrollTop <= 5) {
553
+ const translateY = Math.min(pullDistance * 0.4, 70);
554
+ newsContent.style.transform = `translateY(${translateY}px)`;
555
+ if (ptrIndicator) {
556
+ ptrIndicator.style.opacity = Math.min(translateY / 50, 1).toString();
557
+ ptrIndicator.style.transform = `translateY(${translateY - 40}px) scale(${Math.min(translateY / 50, 1)})`;
558
+ }
559
+ }
560
+ }, {passive: true});
561
+
562
+ viewScrollContainer.addEventListener('touchend', async () => {
563
+ if (!isPtrPulling || isPtrRefreshing) return;
564
+ isPtrPulling = false;
565
+ const pullDistance = ptrCurrentY - ptrStartY;
566
+
567
+ newsContent.style.transition = 'transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1)';
568
+ if (ptrIndicator) ptrIndicator.style.transition = 'transform 0.3s, opacity 0.3s';
569
+
570
+ if (pullDistance * 0.4 >= 55) {
571
+ isPtrRefreshing = true;
572
+ newsContent.style.transform = 'translateY(50px)';
573
+ if (ptrIndicator) {
574
+ ptrIndicator.style.transform = 'translateY(10px) scale(1)';
575
+ ptrIndicator.style.opacity = '1';
576
+ }
577
+
578
+ // Trigger API check / generation
579
+ await this.fetchData();
580
+ await fetch(getApiUrl('/api/news/generate'), { method: 'POST', headers: { 'Content-Type': 'application/json' } });
581
+
582
+ setTimeout(async () => {
583
+ // Check again after 1.5s to get any fast updates, then close
584
+ await this.fetchData();
585
+ newsContent.style.transform = 'translateY(0)';
586
+ if (ptrIndicator) {
587
+ ptrIndicator.style.transform = 'translateY(-40px) scale(0.8)';
588
+ ptrIndicator.style.opacity = '0';
589
+ }
590
+ setTimeout(() => { isPtrRefreshing = false; }, 300);
591
+ }, 1500);
592
+
593
+ } else {
594
+ newsContent.style.transform = 'translateY(0)';
595
+ if (ptrIndicator) {
596
+ ptrIndicator.style.transform = 'translateY(-40px) scale(0.8)';
597
+ ptrIndicator.style.opacity = '0';
598
+ }
599
+ }
600
+ });
601
  }
602
 
603
+ // Modal Events Article
604
  document.querySelectorAll('.news-card').forEach(card => {
605
  card.addEventListener('click', (e) => {
606
  const id = e.currentTarget.dataset.id;